Skip to content

Instantly share code, notes, and snippets.

@999pingGG
999pingGG / collision_detection.c
Created June 20, 2023 17:48
AABB and sphere collision detection
// This file shows the algorithms for detecting 3D AABB-AABB, sphere-sphere and AABB-sphere collisions together with collision normal and penetration.
// - This was thought for use with an ECS system, specifically flecs. https://www.flecs.dev/flecs/
// - Based on https://gamedevelopment.tutsplus.com/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331t
// - This won't compile as-is, you have to provide your own math functions.
// - An AABB is represented by a center, min and max coordinates. The code assumes that an AABB's min corners coords are always less than the max corners.
// - A sphere is represented by a center and a radius.
// - Uses double-precision floating point numbers for translation, penetration, AABBs and sphere's radius. Single precision for everything else. You can change this easily.
// - Supports scale.
// - For AABBs, scale is applied to its extents.
// - For spheres, scale vector components are averaged and applied to the radius. That is, it works