Bump.lua - how to change or display collision box?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Bump.lua - how to change or display collision box?

Post by hasen »

I couldn't see in the documentation how to change or display the collision boxes. I realise there is width in the world:add function but that doesn't seem to be centred so it ends up with only one side getting smaller. Not sure how to change the anchor point or what I need to do for that.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Bump.lua - how to change or display collision box?

Post by Tjakka5 »

You can use 'world:update(item, x,y,<w>,<h>)' to set position and dimensions without collision detection.
For displaying the bounding boxes you can use 'world:getItems()' and 'world:getRect(item)':

Code: Select all

local items, len = world:getItems()
for i = 1, len do
  local x, y, w, h = world:getRect(items[i])
  love.graphics.rectangle("line", x, y, w, h)
end
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Bump.lua - how to change or display collision box?

Post by hasen »

Ok thanks, that's great, I have the bounding box displayed now. The problem is the collision box is still not centred. If I change the width it's aligned to the left just I suspected it was? How can I place it in the centre....don't see why I'd ever want it to the left anyway but that seems to be the default.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Bump.lua - how to change or display collision box?

Post by Tjakka5 »

When you register the body, offset it by half the length and width.

Code: Select all

world:add(item, item.x - item.w/2, item.y - item.h/2, item.w, item.h
Also do this for :update and similair functions.

When you try to :move a body, do it as follows:

Code: Select all

local newX, newY, cols, len = world:move(item, item.x - item.w/2, item.y - item.h/2)
item.x = newX + item.w/2
item.y = newY + item.h/2
This will always offset it properly.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Bump.lua - how to change or display collision box?

Post by hasen »

That makes sense but for some reason doesn't work that well.

Code: Select all

function love.load()
world:add(player, player.x - player.w/2, player.y, player.w/2, player.h)

function love.update(dt)
world:move(player, player.x + player.dx, player.y + player.dy)
Is this what you mean? How would I alter the move function in update though? I couldn't seem to get that to work and noticed the change in love.load didn't seem to change the position of the collision box...?
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Bump.lua - how to change or display collision box?

Post by Beelz »

Code: Select all

local x, y, w, h = world:getRect(items[i])
love.graphics.rectangle("line", x-w/2, y-h/2, w, h)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Bump.lua - how to change or display collision box?

Post by hasen »

Beelz wrote: Fri Feb 02, 2018 2:10 am

Code: Select all

local x, y, w, h = world:getRect(items[i])
love.graphics.rectangle("line", x-w/2, y-h/2, w, h)
This code is even funnier than the code in your signature. Surely this would only change the size of the rectangle drawn and not actually affect the collision box...

Does anyone have the answer?
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Bump.lua - how to change or display collision box?

Post by zorg »

From kikito's bump page on github: https://github.com/kikito/bump.lua wrote: Changing the position and dimensions of items in the world

world:update(item, x,y,<w>,<h>)

Even if your "player" has attributes like player.x and player.y, changing those will not automatically change them inside world. update is one of the ways to do so: it changes the rect representing item inside world.

item must be something previously inserted in the world with world:add(item, l,t,w,h). Otherwise, world:update will raise an error.
x,y,w,h the new dimensions of item. x and y are mandatory. w and h will default to the values the world already had for item.
Beelz already answered half of your question in his "funny" post, about how to display the rect of an item from bump's world.

The above quote shows how to properly modify the collision rects though; since you want them "centered", you'll need to math out how much you'll need to mess with all 4 of the values. (If the width gets reduced by 50% let's say, you'd need to move the x (left) position to the right by 25% of the width, accounting for a decrease on both left and right side of the newly sized object.)

Edit: Just noticed Tjakka5 already provided the same solution... welp; let me edit my answer to answer your last question instead:

Code: Select all

function love.update(dt)
local newX, newY, cols, len = world:move(item, item.x - item.w/2, item.y - item.h/2)
local _, _, iw, ih = world:getRect(item)
world:update(item, newX + iw/2, newX + ih/2)
didn't test it, but something like that may work... though at a first glance, it seems overcomplicated to me.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Bump.lua - how to change or display collision box?

Post by Tjakka5 »

hasen wrote: Thu Feb 01, 2018 1:34 pm That makes sense but for some reason doesn't work that well.

Code: Select all

function love.load()
world:add(player, player.x - player.w/2, player.y, player.w/2, player.h)

function love.update(dt)
world:move(player, player.x + player.dx, player.y + player.dy)
Is this what you mean? How would I alter the move function in update though? I couldn't seem to get that to work and noticed the change in love.load didn't seem to change the position of the collision box...?
The reason the love.load didnt seem to make any change is because it is instantly changed in love.update.
That is because your love.update code not account for the dimensions.

Code: Select all

function love.update(dt)
  world:move(player, player.x + player.dx - player.w/2, player.y + player.dy - player.h/2)
end
You also should not need to offset it in your collision box rendering code; it would just display a false collision box.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Bump.lua - how to change or display collision box?

Post by hasen »

zorg wrote: Fri Feb 02, 2018 8:27 am Beelz already answered half of your question in his "funny" post, about how to display the rect of an item from bump's world.
Not really, he answered a question already answered and he suggested to change the size of the rendered rectangle which wouldn't be any use because it would be drawing a rectangle for the collision box...which was different to the actual size of the collision box...
Tjakka5 wrote: Fri Feb 02, 2018 8:40 am You also should not need to offset it in your collision box rendering code; it would just display a false collision box.
Well yeah exactly, but that was what Beelz suggested, not me.

Thanks for your help, I'll try out your code and see how it works.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 205 guests