For collision detection, check whether axis-aligned bounding boxes (AABBs) overlap.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath; var a = new Bounds3D(new(0, 0, 0), new(2, 2, 2)); var b = new Bounds3D(new(2, 0, 0), new(4, 2, 2)); Console.WriteLine(a.Intersects(b));
Unzip the example, then dotnet run.
TrueThe boxes meet at the face where X is 2.
The full program
using RecursiveAI.OneMath; var a = new Bounds3D(new(0, 0, 0), new(2, 2, 2)); var overlap = new Bounds3D(new(1, 0, 0), new(3, 2, 2)); var face = new Bounds3D(new(2, 0, 0), new(4, 2, 2)); var separate = new Bounds3D(new(3, 0, 0), new(5, 2, 2)); Console.WriteLine($"Overlapping: {a.Intersects(overlap)}"); Console.WriteLine($"Touching: {a.Intersects(face)}"); Console.WriteLine($"Separated: {a.Intersects(separate)}"); Console.WriteLine($"Empty sentinel: {a.Intersects(Bounds3D.Empty)}");
dotnet run prints:
Overlapping: True Touching: True Separated: False Empty sentinel: False
The constructor does not sort corners; if your data can contain reversed bounds, reject boxes with IsEmpty before calling Intersects.
Variations
For ordinary finite boxes, test for positive shared volume rather than contact:
using RecursiveAI.OneMath; var a = new Bounds3D(new(0, 0, 0), new(2, 2, 2)); var b = new Bounds3D(new(2, 0, 0), new(4, 2, 2)); var shared = Bounds3D.Intersection(a, b); var size = shared.Size; Console.WriteLine(size.X > 0 && size.Y > 0 && size.Z > 0);
FalseRelated recipes
The short answer
Use a.Intersects(b) on two Bounds3D values with ordered corners in the same coordinate space. The method answers whether the boxes share any point, so a face, edge, or corner in common counts. A gap on just one axis is enough for False.
How does 3D AABB collision detection work?
Think of each box as three intervals, one per coordinate axis. For the two X intervals to meet, A’s minimum must be at or before B’s maximum, and B’s minimum at or before A’s maximum. Apply the same test on Y and Z; all three axes must pass. This is the interval test described in MDN’s 3D collision detection guide and implemented in OneMath’s bounds source.
In the separated case above, A stops at X = 2 and the other box starts at X = 3. Their Y and Z intervals match exactly, but they still share no point. In the overlap case, their shared X interval is 1 through 2. Because both boxes are axis-aligned, the shared X, Y, and Z intervals together describe another axis-aligned box.
When the boxes enclose shapes rather than represent the shapes themselves, an overlap only identifies a pair worth checking. Two meshes can occupy different parts of intersecting boxes without touching each other. Test both bounds in the same space: a box in an object’s local coordinates cannot be compared directly with another object’s world-space box. This test compares one pair of boxes at their current positions, not their paths between frames.
Do touching bounding boxes count as a collision?
For Intersects, yes. The comparisons include equality. When the two X intervals meet at 2, the shared X width is zero but the shared face still contains points. An edge has zero width on two axes; a point has zero width on all three. The same inclusive test covers all of them.
If your rule is that the boxes must share some interior volume, take their Intersection and require all three side lengths to be positive, as in the variation. Checking !shared.IsEmpty alone won’t make that distinction: a face is a nonempty intersection with zero thickness. The same distinction applies to zero-size inputs such as a box that represents a single point. Keep the inclusive test for finding contact; use the positive-size rule when contact alone must not count.