AABB Collision help?

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.
Post Reply
Teo434
Prole
Posts: 11
Joined: Mon Apr 27, 2020 12:21 am

AABB Collision help?

Post 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!
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: AABB Collision help?

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Teo434
Prole
Posts: 11
Joined: Mon Apr 27, 2020 12:21 am

Re: AABB Collision help?

Post 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.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: AABB Collision help?

Post by togFox »

If you print px1, bx2, px1 and by2 then do you get values you expect?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Teo434
Prole
Posts: 11
Joined: Mon Apr 27, 2020 12:21 am

Re: AABB Collision help?

Post 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
Teo434
Prole
Posts: 11
Joined: Mon Apr 27, 2020 12:21 am

Re: AABB Collision help?

Post 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.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: AABB Collision help?

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Teo434
Prole
Posts: 11
Joined: Mon Apr 27, 2020 12:21 am

Re: AABB Collision help?

Post by Teo434 »

Yeah, I noticed that in a reply avobe, I've fixed that now
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: AABB Collision help?

Post 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)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests