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

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

Post by hasen »

Like this everything is fine:

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)
But changing it to this sends my player flying off to the left at lightspeed:

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.w/2, player.y + player.dy)
I guess this is happening because it's removing the player width in love.update which is updated every frame...?
User avatar
zorg
Party member
Posts: 3436
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 »

I mean, even if an object changes size, that should have zero impact on how much it moves (disregarding things like physics, of course) so whether an object is defined by (-1,-1,2,2) or (-2,-2,4,4), both being squares centered on 0,0 with size 1 and 2 respecively, if you move both with a delta of (3,0), both objects travelled the same distance, regardless of their size.

As for why the lightspeed thing happens, :move modifies the rects' coordinates; you subtracting player.w/2 each iteration does that, it's not needed there.
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.
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 9:57 am I mean, even if an object changes size, that should have zero impact on how much it moves (disregarding things like physics, of course) so whether an object is defined by (-1,-1,2,2) or (-2,-2,4,4), both being squares centered on 0,0 with size 1 and 2 respecively, if you move both with a delta of (3,0), both objects travelled the same distance, regardless of their size.

As for why the lightspeed thing happens, :move modifies the rects' coordinates; you subtracting player.w/2 each iteration does that, it's not needed there.
Ok I see, but I was just using Tjakka5's code and that's what happens. You do seem to have that in your code too though. This is your code:

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)
Based on this and Tjakka5's code I modified my code to this:

Code: Select all

player.x, player.y, cols, len = world:move(player, player.x + player.dx - player.w/2, player.y + player.dy)
Which sends my player off to the left at lightspeed.

Basically the collision box is justified left....still no idea how to centre it. Without altering the world:move the collision box is still hard justified left, even after the adjustment in the world:add.
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 »

The reason that your object is flying to the left is because you're setting the position directly, instead of cancelling the offset.
I explained this in my post.

Either way, here's a full snippet that should fully work:


Code: Select all

world = bump.newWorld(32)

local item = {
  x = 0, y = 0,
  w = 100, h = 100,
}

function love.load()
  world:add(item, item.x - item.w/2, item.h - item.w/2, w, h) -- We offset by half the dimensions, so the bounding box is centered on 'x' and 'y'
end

function love.update(dt)
  local newX, newY, cols, len = world:move(item, item.x - item.w/2, item.y - item.h/2) -- We offset again so the bounding box is still centered
  
  -- newX and newY is the top left point of the bounding box, so we have to offset it again to the center:
  item.x = newX + item.w/2
  item.y = newY + item.h/2
  
  -- We can even change the dimensions and it should stay nicely centered:
  item.w = (math.sin(love.timer.getTime()) + 1.1) * 50
  world:update(item, item.x, item.y, item.w, item.h)
end

function love.draw()
  -- Drawing our 'item':
  love.graphics.setColor(255, 255, 255)
  love.graphics.rectangle("fill", item.x, item.y, item.w, item.h)
  
  -- Drawing all objects in the world
  local objs, len = world:getItems()
  love.graphics.setColor(255, 0, 0)
  for i = 1, len do
    local x, y, w, h = world:getRect(objs[i])
    love.graphics.rectangle("line", x, y, w, h)
  end
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 »

Are you sure that code works? I still don't see how the code I was using is different to what you have there. Your code also seems to be with an unmovable 'item' whereas my code is about a 'player' that can move. Seems like we're talking about different things?
User avatar
zorg
Party member
Posts: 3436
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 »

This whole thread still seems way overcomplicated to me.

There were two questions at the start:
1. How to display the collision boxes, which was answered by the getRect method.
2. How to change the collision boxes, which was answered by the update method.

Bump internally stores rects as {left, top, width, height}, and not as {centerx, centery, halfwidth, halfheight} or whatever else.
Converting between those two representations is easy:

Code: Select all

function corner2center(l,t,w,h) return l+w/2, t+h/2, w/2, h/2 end
function center2corner(x,y,v,n) return x-v, y-n, v*2, n*2 end
But precisely because of that, it's pointless to overwrite the internal behaviour, in my opinion.

(The move method moves a collision box a set amount; moving is shape-definition-agnostic, i.e. it doesn't care how the rect is defined; moving a {-1,-1,2,2} and a {0,0,2,2} rect the same amount results in the same distance travelled.)
Last edited by zorg on Fri Feb 02, 2018 12:09 pm, edited 2 times in total.
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 »

I made a few mistakes in my code. For clarity I have made a .love demonstrating how it works.
You can also do the transformations with the functions Zorg provided.
Attachments
Centered Bump.love
(7.15 KiB) Downloaded 83 times
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 it seems what I was missing then was world:update which is not currently in my code at all. I only have world:add and world:move. So world:update is needed to fix the offset? Otherwise there's no need for it at all it seems.

Thanks for the code example Tjakka5, that was very useful.
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 »

world:update is needed for when you want to change the dimensions (Or move somewhere without collision).

What you were missing what cancelling the offset after calling world:move.
In your examples, you were setting the player position directly.
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 »

....
Last edited by hasen on Fri Feb 02, 2018 1:35 pm, edited 1 time in total.
Post Reply

Who is online

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