It seems that not too many people know this technique, though it is very simple.
It should take up roughly the same amount of time as 2 circle checks.
Here is AABB.class
public class AABB
{
protected Vector2Float pos, size;
public AABB(Vector2Float pos, Vector2Float size)
{
this.pos = pos;
this.size = size;
}
public static boolean collides(AABB a, AABB b)
{
if(Math.abs(a.pos.x - b.pos.x) < a.size.x + b.size.x)
{
if(Math.abs(a.pos.y - b.pos.y) < a.size.y + b.size.y)
{
return true;
}
}
return false;
}
public static boolean inside(AABB a, Vector2Float b)
{
if(Math.abs(a.pos.x - b.x) < a.size.x)
{
if(Math.abs(a.pos.y - b.y) < a.size.y)
{
return true;
}
}
return false;
}
}
And if you don’t have your own implementation or whatever, the simplest possible Vector2Float:
public class Vector2Float
{
public float x, y;
}
The method [icode]collides()[/icode], checks whether 2 AABBs are colliding, and [icode]inside()[/icode], checks whether a point is inside an AABB.
What you must remember, is that unlike most AABB implementations, the pos vector, is the centre of the AABB, and the size vector is half the actual size.
Enjoy!
(And could someone do a performance test with this test vs the normal rectangle collision test? :))