It looks like you're new here. If you want to get involved, click one of these buttons!
I have been using AABBs to find collisions in my games recently and have put together functions for handling them.
If you have any thoughts or suggestions please post them below. The code here is for anyone to use:
--AABB.lua
--------------------------------------------------------------------------------------------------
--[[
The AABB namespace contains functions for working with AABBs (axis aligned bounding boxes)
an AABB is required (and output) in the table format like so:
{ x = Xpos, y = Ypos, w = Width, h = Height}
the x and y are taken from the lower left corner of the AABB
--]]
--------------------------------------------------------------------------------------------------
AABB={}
AABB.pointIn=function(aabb,p)
--determines weather a point is within an AABB
return (p.x > aabb.x and p.x < aabb.x + aabb.w and p.y > aabb.y and p.y < aabb.y + aabb.h)
end
AABB.query=function(aabb1,aabb2)
--do two AABBs overlap?
if aabb1.x>aabb2.x+aabb2.w or aabb2.x>aabb1.x+aabb1.w or
aabb1.y>aabb2.y+aabb2.h or aabb2.y>aabb1.y+aabb1.h then
return false
end
return true
end
AABB.getOverlap=function(aabb1,aabb2)
--get the AABB which is the overlap between two other AABBs
local x1=math.max(aabb1.x,aabb2.x)
local y1=math.max(aabb1.y,aabb2.y)
local x2=math.min(aabb1.x+aabb1.w,aabb2.x+aabb2.w)
local y2=math.min(aabb1.y+aabb1.h,aabb2.y+aabb2.h)
if x1<=x2 and y1<=y2 then
return {x=x1,y=y1,w=x2-x1,h=y2-y1}
end
return false
end
AABB.get=function(verts,pad)
--return an AABB from a list of vec2 points
--pad os the padding around the edge default is 0
local minX,minY,maxX,maxY
for k,v in ipairs(verts) do
minX=math.min(v.x,minX or v.x)
minY=math.min(v.y,minY or v.y)
maxX=math.max(v.x,maxX or v.x)
maxY=math.max(v.y,maxY or v.y)
end
local x,y,w,h
x,y=minX,minY
w=maxX-minX
h=maxY-minY
if pad then
x = x - pad
y = y - pad
w = w + pad*2
h = h + pad*2
end
return {x=x,y=y,w=w,h=h}
end
Comments
This is quite useful thanks, how do you think it would work for a particle engine? I don't have my iPad with me atm so I can't test for myself, but have you done any speed/stress tests?
It can run about 2000 queries every frame and retain about 60 FPS:
Little particle field, touch to pull the box towards you. Getting around 20 fps so for 1000 particles thats very good. Updated code:
Yep that is very good, thanks for this example. I like the way the square is buffeted around by the particles
No problem this is a great library, I've updated the code to user the overlap function. It might be a bit slower, I don't know as I was running it on my macbook for a bit extra speed. It's like a snow plough now, follows basic (I mean very basic and inaccurate but workable) laws of particle resistance.
Another example,building on my last one, this one resolves collisions too:
@NatTheCoder It's @Coder 's AABB code (library) and my example using it.
this is nice. How does it compare to build in physics engine perf?
@Jmv38 there isn't a way to rotate the AABB in this code so you can't have a full fledged physics engine, but velocities and direction are good.
Rotate, hmm... Maybe that's next. Just rotate all the coordinates by the angle of the AABB, but what if there are two? (I guess axis-aligned means it doesn't rotate)
@Coder have fun with the algebra
)
woo! my favourite