AABB Collision help?
Posted: Sun Feb 21, 2021 1:38 am
So collision is something I've struggled on since I started coding in love, now for the jam, I (at this point, surprisingly ) 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:
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
Also, I'd rather make my own system than use bump, for the learning experience mostly. Thanks!
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
- "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
Also, I'd rather make my own system than use bump, for the learning experience mostly. Thanks!