Recursive AI

Combine two AABB bounding boxes in C#

OneMath is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

An axis-aligned bounding box (AABB) can enclose two child boxes even if they don’t touch.

Terminal
dotnet add package RecursiveAI.OneMath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath;
var left = new Bounds3D(new(1, 2, 3),
                        new(2, 4, 5));
var right = new Bounds3D(new(5, 1, 4),
                         new(6, 3, 7));
var parent = Bounds3D.Union(left, right);
Console.WriteLine($"Min: {parent.Min}");
Console.WriteLine($"Max: {parent.Max}");

Unzip the example, then dotnet run.

Text
Min: (1, 1, 3)
Max: (6, 4, 7)

The full program

C#
using RecursiveAI.OneMath;
var left = new Bounds3D(new(1, 2, 3),
                        new(2, 4, 5));
var right = new Bounds3D(new(5, 1, 4),
                         new(6, 3, 7));
var parent = Bounds3D.Union(left, right);
Console.WriteLine($"Min: {parent.Min}");
Console.WriteLine($"Max: {parent.Max}");
 
Console.WriteLine($"Children overlap: {left.Intersects(right)}");
Console.WriteLine($"Parent contains both: {parent.Contains(left) && parent.Contains(right)}");
Text
Min: (1, 1, 3)
Max: (6, 4, 7)
Children overlap: False
Parent contains both: True

If an input’s Min is greater than its Max on any axis, the box is empty (IsEmpty is true). Fix or skip reversed bounds before calling Union: it uses the corners as given, so a reversed box can still enlarge the result.

Variations

To combine a list of children, append this fold to the snippet at the top.

C#
var combined = Bounds3D.Empty;
foreach (var child in new[] { left, right })
    combined = Bounds3D.Union(combined, child);
Console.WriteLine($"Combined: {combined}");
Text
Combined: [(1, 1, 3) .. (6, 4, 7)]

The short answer

Call Bounds3D.Union(left, right) on two axis-aligned boxes in the same coordinate system. For each axis, the parent’s minimum is the lesser of the two child minima, and its maximum is the greater of the two child maxima. The result is one enclosing box, even when the inputs are disjoint.

How do you calculate a bounding box for multiple objects?

Compute each object’s bounds in the same coordinate system, then merge them one at a time. An AABB is three intervals, one each along X, Y, and Z. The X interval in this example starts at the left child’s minimum of 1 and ends at the right child’s maximum of 6. Y starts at the right child’s minimum of 1 and ends at the left child’s maximum of 4. The Z interval runs from 3 to 7.

Start an accumulator with Bounds3D.Empty so the first box supplies all six limits. Starting with default(Bounds3D) makes both corners (0, 0, 0): that’s a box containing the origin, so it drags the parent toward the origin even if all the objects are somewhere else. If there are no children, the accumulator remains empty.

Does an AABB union tell you whether the boxes overlap?

No. In the example, neither child contains (3, 2, 4), but the parent does. A single enclosing box spans the gap along X. It also encloses space elsewhere that isn’t part of either child. That’s useful for grouping objects in a spatial tree, but it isn’t a geometric union of their occupied regions or a test that they collide.

Use Intersects or Intersection for different questions: Intersects tells you whether the boxes share a point, including contact at a boundary; Intersection gives you their shared box, empty when they’re separated. Use Union when what you need is the parent’s outer limits.

All onemath recipes