Hull sharing (#13)

Save memory when there are many duplicate hulls. Can improve performance
in some cases.

For consistency with b3MeshData, b3Hull is renamed to b3HullData.
This commit is contained in:
Erin Catto 2026-06-11 22:39:26 -07:00 • committed by GitHub
commit a252f03b19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
66 changed files with 881 additions and 549 deletions

View file

@ -63,8 +63,10 @@ capsule.radius = 0.25f;
### Convex Hulls
Box3D convex hulls are solid convex polyhedra represented by the `b3Hull` type. A shape is
convex when all line segments connecting two interior points remain inside the shape.
Box3D convex hulls are solid convex polyhedra. The geometry lives in a heavy, immutable
`b3HullData` object. The world shares identical hull data through a reference counted
database, so many shapes built from the same data hold one copy. A shape is convex when
all line segments connecting two interior points remain inside the shape.
<!-- TODO: 3D diagram needed -->
@ -77,7 +79,11 @@ b3BoxHull cube = b3MakeCubeHull(0.5f); // uniform half-width
```
`b3BoxHull` stores everything inline — do not call `b3DestroyHull` on one.
Its `.base` member is a `b3Hull` that you pass to shape creation functions.
Its `.base` member is the `b3HullData` that you pass to shape creation:
```c
b3CreateHullShape(bodyId, &shapeDef, &box.base);
```
For a box that is offset or rotated from the body origin:
@ -89,13 +95,14 @@ b3BoxHull rotatedBox = b3MakeTransformedBoxHull(0.5f, 1.0f, 0.5f, localTransform
For arbitrary convex geometry, provide a point cloud and let Box3D compute the hull:
```c
b3Hull* hull = b3CreateHull(points, pointCount, maxVertexCount);
if (hull == NULL)
b3HullData* data = b3CreateHull(points, pointCount, maxVertexCount);
if (data == NULL)
{
// degenerate input: coincident or coplanar points, or insufficient volume
}
// ... use hull ...
b3DestroyHull(hull);
b3CreateHullShape(bodyId, &shapeDef, data);
// The world keeps its own copy, so you may free yours immediately
b3DestroyHull(data);
```
`maxVertexCount` limits the output complexity; pass the same value as `pointCount`
@ -105,8 +112,8 @@ than four non-coplanar points, or nearly-zero volume). Always check before using
Box3D also provides helpers to create cylindrical and conical hulls:
```c
b3Hull* cylinder = b3CreateCylinder(height, radius, yOffset, sides);
b3Hull* cone = b3CreateCone(height, radius1, radius2, slices);
b3HullData* cylinder = b3CreateCylinder(height, radius, yOffset, sides);
b3HullData* cone = b3CreateCone(height, radius1, radius2, slices);
b3DestroyHull(cylinder);
b3DestroyHull(cone);
```

View file

@ -73,9 +73,9 @@ b3ShapeDef groundShapeDef = b3DefaultShapeDef();
b3CreateHullShape(groundId, &groundShapeDef, &groundBox.base);
```
The `.base` field holds the `b3Hull` that `b3CreateHullShape` expects. Box3D copies the
hull data into its internal structures, so `groundBox` does not need to outlive the
call. Do not call `b3DestroyHull` on a `b3BoxHull`; it is stack-allocated.
The `.base` field holds the `b3HullData` that `b3CreateHullShape` expects. Box3D copies
the hull data into a shared internal database, so `groundBox` does not need to outlive
the call. Do not call `b3DestroyHull` on a `b3BoxHull`; it is stack-allocated.
Box3D is tuned for meters, kilograms, and seconds, so the extents above are in meters.
The engine works best when objects are sized like real-world objects (a barrel is roughly