---
title: "Test 3D AABB overlap in C#"
url: https://recursiveai.org/onemath/cookbook/test-3d-aabb-overlap/
description: "Test two 3D axis-aligned bounding boxes for overlap in C#. See how OneMath handles face contact, separation, and boxes with no shared volume."
version: "1.0.0"
---

# Test 3D AABB overlap in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/test-3d-aabb-overlap/test-3d-aabb-overlap.zip)

For collision detection, check whether axis-aligned bounding boxes (AABBs) overlap.

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onemath/cookbook/test-3d-aabb-overlap/test-3d-aabb-overlap.zip), then `dotnet run`.

```text
True
```

The boxes meet at the face where X is 2.

## The full program

```csharp
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:

```text
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:

```csharp
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);
```

```text
False
```

## Related recipes

- [Calculate an axis-aligned bounding box](https://recursiveai.org/onemath/cookbook/calculate-axis-aligned-bounding-box/)
- [Classify line segment intersections](https://recursiveai.org/onemath/cookbook/classify-line-segment-intersection/)

## 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](https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_collision_detection#aabb_vs._aabb) and implemented in [OneMath’s bounds source](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/Bounds3D.cs#L99-L109).

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.
