# AABB Collision Detection Confusion

I am trying to understand and implement AABB collision to learn simple 2D game physics. I'm following a tutorial and its method is a bit different to other websites. It uses the center of the object and its "half-size" to determine collision. This is its code:

public bool Overlaps(AABB other)
{
if ( Mathf.Abs(center.x - other.center.x) > halfSize.x + other.halfSize.x ) return false;
if ( Mathf.Abs(center.y - other.center.y) > halfSize.y + other.halfSize.y ) return false;
return true;
}


And this is the seemingly more popular option:

if (rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y) {
// collision detected!
}


Which one is more efficient? And for the first option, would I define the center and half-size like this:

AABB = new AABB(new Vector2(Width / 2, Height / 2), new Vector2(Width / 2, Height / 2));


Center and half-size method source: https://gamedevelopment.tutsplus.com/tutorials/basic-2d-platformer-physics-part-1--cms-25799

Position and width/height source: https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection

Efficiency of both depends on how do you use AABBs. Center/half-size pros:

• Easy and fast retrieval of AABB's position
• Collision detection may be easier to understand
• Easier to understand calculation of AABB's border position

Center/half-size cons:

• Each collision detection will have more operations (subtraction/addition)
• Multiplication when retrieving AABB's size

Position/size pros:

• Collision detection has less operations (which doesn't necessarily mean it will be faster, probably not much unless you do millions of them during single frame)

Position/size cons:

• Heavier calculation of AABB's center (division by 2, so nothing scary though).

If you often need to update/retrieve AABB's center position, first option might be faster. If not, second one. Best way would be to implement both and profile the application, or just use the one is easier for you to understand and use.

There's actually a third option: storing AABBs as mins and maxs. That is, a Vector2 as the minimum x and minimum y, and another as the maximum x and maximum y values that the AABB occupies.

This allows for the representation of the AABB as a pure math concept and to not be dependent on how the screen or world space is set up. It's more flexible but can require more work for the user to calculate things like camera bounds and such.

In terms of collision it is exactly the same as your "more popular" option 2.

• The downside of this approach is that you need more operations to update the position of AABB - calculate the size, update two vectors, and hope that size doesn't change too much due to floating point math magic. – crueltear Aug 9 at 6:02