Sarah Yoo

Player and Blocks Collision Check

In many games, the visible shape of objects is too complex to use for collision detection; a mesh can have hundreds or thousands of vertices, which makes precise checks extremely expensive.

Instead, simplified collision shapes, such as rectangles, circles, spheres, and capsules, are used. These are basic geometries that approximate the real shape of the object, thereby easier to work with and more efficient to compute.

To check collision between the player and blocks, of course using box-shaped hitbox is a perfect fit. This hitbox is typically called Axis-Aligned Bounding Box (AABB).

AABB Collision Detection

AABB

AABB collision detection is one of the simple and widely used methods for collision detection in 2D and 3D games. It checks whether two rectangular (or cuboid) boxes are overlapping. AABB algorithm is fast because you only need to compare position and size along each axis. So, it can be used for voxel-based worlds where voxels lay static.

However, AABB algorithm is not optimal for dealing with quickly-moving objects.

Problems of AABB Collision Detection

AABB algorithm works best when it is used for static or slow-moving objects.

A major problem of AABB is that an object can pass through another without triggering a collision. This is called tunneling, which occurs when an object moves too fastly and/or the program is running at a low frame rate.

To avoid this, we need to somehow predict where the box travelled between each frame. This concept is called swept.

Swept AABB Collision Detection

Swept AABB algorithm addresses this tunneling effect by considering the movement of objects over a time interval. It calculates a collision time and normal (the unit vector that is perpendicular to the surface of the AABB where the collision occurs).

The remaining time after a collision (1 - collision time) is used to process collision responses like sliding and bouncing. A normal vector indicates the direction in which the collision response should be applied to separate the colliding objects.

  • See the implementation of Swepth AABB algorithm (in 2D space) here.
  • You can also find my implementation in my voxel engine here.