Page 1 of 1

AABB Collision help?

Posted: Sun Feb 21, 2021 1:38 am
by Teo434
So collision is something I've struggled on since I started coding in love, now for the jam, I (at this point, surprisingly :roll:) can't seem to get it. I've looked online for help, but most of them either: Tell the user to use bump.lua, or just outright don't work for me.

The code that checks the collision at the moment is this:

Code: Select all

local collisionData = mapManager.getCollisionData(currentMap) 

local px1, py1 = p.pos.x, p.pos.y
local px2, py2 = px1 + p.size.x, py1 + p.size.y
for i, obj in pairs(collisionData) do
	local bx1, by1 = obj.x, obj.y
	local bx2, by2 = bx1 * tileSize, by1 * tileSize

	if (px1 < bx2) and (px1 > bx2) and (py1 < by2) and (py1 > by2) then
		-- My issue is here, this print line never gets triggered.
		print("collides.")
	end
end
Now for some extra context:
- "tileSize" is well, the size for tiles, a number.
- "getCollisionData" returns a table containing an X and a Y value for all of the solid objects
- "p" is the player, and like any other object has an X, Y, and size values.

I apologize if I missed out on something, I am very tired of working on collisions :ehem:
Also, I'd rather make my own system than use bump, for the learning experience mostly. Thanks!

Re: AABB Collision help?

Posted: Sun Feb 21, 2021 3:15 am
by togFox
I'm not at my PC so brief suggestion:

PRINT those values to console and if necessary, for debugging purposes, spilt that IF statement into two and check which one is failing.

Re: AABB Collision help?

Posted: Sun Feb 21, 2021 3:52 am
by Teo434
togFox wrote: Sun Feb 21, 2021 3:15 am PRINT those values to console and if necessary, for debugging purposes, spilt that IF statement into two and check which one is failing.
Hello! sorry for the late response. The values return "true false true false" even on solids

EDIT: I've also realized I'm mutliplying bx2 and by2 by tileSize instead of adding tileSize to their values.

Re: AABB Collision help?

Posted: Sun Feb 21, 2021 8:08 am
by togFox
If you print px1, bx2, px1 and by2 then do you get values you expect?

Re: AABB Collision help?

Posted: Sun Feb 21, 2021 6:52 pm
by Teo434
Sorry for the late response, I've tweaked some values and now it prints propperly, however the moving part dosen't seem to be working.

I am unable to provide the code right now, will do in a bit

Re: AABB Collision help?

Posted: Sun Feb 21, 2021 7:22 pm
by Teo434
Here's the code:

Code: Select all

if (px1 < bx2) and (px2 > bx1) and (py1 < by2) and (py2 > by1) then
	if dir == "left"  then p.pos.x = bx2 end
	if dir == "right" then p.pos.x = bx1 end
	if dir == "up"    then p.pos.y = by2 end
	if dir == "down"  then p.pos.y = by1 end
end
"dir" is the direction the player is holding.

Re: AABB Collision help?

Posted: Sun Feb 21, 2021 8:35 pm
by darkfrei
Teo434 wrote: Sun Feb 21, 2021 1:38 am The code that checks the collision at the moment is this:

Code: Select all

	local bx2, by2 = bx1 * tileSize, by1 * tileSize
Why not (here plus, not multiplicate)?

Code: Select all

	local bx2, by2 = bx1 + tileSize, by1 + tileSize

Re: AABB Collision help?

Posted: Sun Feb 21, 2021 10:37 pm
by Teo434
Yeah, I noticed that in a reply avobe, I've fixed that now

Re: AABB Collision help?

Posted: Mon Mar 01, 2021 3:26 am
by RNavega

Code: Select all

if (px1 < bx2) and (px1 > bx2)
Hi. How can px1 be both smaller and bigger than the same value?

I think you meant...

Code: Select all

if (px1 >= bx1) and (px1 <= bx2)