Bump.lua - how to change or display collision box?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Bump.lua - how to change or display collision box?
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.
Re: Bump.lua - how to change or display collision box?
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)':
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
Re: Bump.lua - how to change or display collision box?
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.
Re: Bump.lua - how to change or display collision box?
When you register the body, offset it by half the length and width.
Also do this for :update and similair functions.
When you try to :move a body, do it as follows:
This will always offset it properly.
Code: Select all
world:add(item, item.x - item.w/2, item.y - item.h/2, item.w, item.h
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
Re: Bump.lua - how to change or display collision box?
That makes sense but for some reason doesn't work that well.
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...?
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)
Re: Bump.lua - how to change or display collision box?
Code: Select all
local x, y, w, h = world:getRect(items[i])
love.graphics.rectangle("line", x-w/2, y-h/2, w, h)
Re: Bump.lua - how to change or display collision box?
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...Beelz wrote: ↑Fri Feb 02, 2018 2:10 amCode: Select all
local x, y, w, h = world:getRect(items[i]) love.graphics.rectangle("line", x-w/2, y-h/2, w, h)
Does anyone have the answer?
- 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?
Beelz already answered half of your question in his "funny" post, about how to display the rect of an item from bump's world.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.
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)
Me and my stuff True 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.
Re: Bump.lua - how to change or display collision box?
The reason the love.load didnt seem to make any change is because it is instantly changed in love.update.hasen wrote: ↑Thu Feb 01, 2018 1:34 pm That makes sense but for some reason doesn't work that well.
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...?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)
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
Re: Bump.lua - how to change or display collision box?
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...
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 208 guests