SIMD hull collision (#93)

SIMD based hull collision inspired by example code from @cairnc in #54 

<img width="1676" height="1332" alt="image"
src="https://github.com/user-attachments/assets/58b137bd-bf7b-4c99-863c-64341b973a3d"
/>

- Fixed #78
- Fixed #85 and fixed CI to error on UB.
- Updated benchmarks
- Added AMD scalar benchmarks
- Added Neon benchmarks
- More determinism unit tests
- Sample leak detection
- Added b3Body_AllowFastRotation
- Added b3Body_IsFastRotationAllowed
- Bumped b3HullData version
- Bumped b3CompoundData version
This commit is contained in:
Erin Catto 2026-07-18 22:43:03 -07:00 • committed by GitHub
commit c37cfe4cd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 3898 additions and 1151 deletions

View file

@ -38,7 +38,7 @@
#ifdef __cplusplus
#define B3_API extern "C" BOX3D_EXPORT
#define B3_INLINE inline
#define B3_ALIGN_AS(N) alignas(N)
#if defined( _MSC_VER )
#define B3_FORCE_INLINE __forceinline
#elif defined( __GNUC__ ) || defined( __clang__ )
@ -52,6 +52,7 @@
#else
#define B3_API BOX3D_EXPORT
#define B3_INLINE static inline
#define B3_ALIGN_AS(N) _Alignas(N)
#if defined( _MSC_VER )
#define B3_FORCE_INLINE static __forceinline

View file

@ -524,31 +524,31 @@ B3_API b3WorldTransform b3Body_GetTransform( b3BodyId bodyId );
/// Set the world transform of a body. This acts as a teleport and is fairly expensive.
/// @note Generally you should create a body with the intended transform.
/// @see b3BodyDef::position and b3BodyDef::rotation
/// @see b3BodyDef::position and b3BodyDef::rotation.
B3_API void b3Body_SetTransform( b3BodyId bodyId, b3Pos position, b3Quat rotation );
/// Get a local point on a body given a world point
/// Get a local point on a body given a world point.
B3_API b3Vec3 b3Body_GetLocalPoint( b3BodyId bodyId, b3Pos worldPoint );
/// Get a world point on a body given a local point
/// Get a world point on a body given a local point.
B3_API b3Pos b3Body_GetWorldPoint( b3BodyId bodyId, b3Vec3 localPoint );
/// Get a local vector on a body given a world vector
/// Get a local vector on a body given a world vector.
B3_API b3Vec3 b3Body_GetLocalVector( b3BodyId bodyId, b3Vec3 worldVector );
/// Get a world vector on a body given a local vector
/// Get a world vector on a body given a local vector.
B3_API b3Vec3 b3Body_GetWorldVector( b3BodyId bodyId, b3Vec3 localVector );
/// Get the linear velocity of a body's center of mass. Usually in meters per second.
B3_API b3Vec3 b3Body_GetLinearVelocity( b3BodyId bodyId );
/// Get the angular velocity of a body in radians per second
/// Get the angular velocity of a body in radians per second.
B3_API b3Vec3 b3Body_GetAngularVelocity( b3BodyId bodyId );
/// Set the linear velocity of a body. Usually in meters per second.
/// Set the linear velocity of a body at the center of mass. Usually in meters per second.
B3_API void b3Body_SetLinearVelocity( b3BodyId bodyId, b3Vec3 linearVelocity );
/// Set the angular velocity of a body in radians per second
/// Set the angular velocity of a body in radians per second.
B3_API void b3Body_SetAngularVelocity( b3BodyId bodyId, b3Vec3 angularVelocity );
/// Set the velocity to reach the given transform after a given time step.
@ -710,6 +710,14 @@ B3_API void b3Body_SetBullet( b3BodyId bodyId, bool flag );
/// Is this body a bullet?
B3_API bool b3Body_IsBullet( b3BodyId bodyId );
/// Allow this body to rotate fast. Useful for axially symmetric bodies, such as vehicle wheels.
/// Normally rotation speed is clamped to improve CCD. However, this clamping is unnecessary for
/// bodies that only rotate fast around an axis of symmetry.
B3_API void b3Body_AllowFastRotation( b3BodyId bodyId, bool flag );
/// Is this body allowed to rotate fast?
B3_API bool b3Body_IsFastRotationAllowed( b3BodyId bodyId );
/// Enable or disable contact recycling for this body. Contact recycling is a performance optimization
/// that reuses contact manifolds when bodies move slightly. Disabling it can avoid ghost collisions
/// on characters at the cost of higher per-step work. Existing contacts retain their prior setting;
@ -778,6 +786,9 @@ B3_API int b3Body_CollideMover( b3BodyId bodyId, b3BodyPlaneResult* bodyPlanes,
* Functions to create, destroy, and access.
* Shapes bind raw geometry to bodies and hold material properties including friction and restitution.
* You may add multiple shapes to a single body. There are no hard limits on shape count per body.
*
* When you create a shape on a body the center of mass moves. This can lead to the body linear velocity
* changing if the angular velocity is non-zero.
* @{
*/

View file

@ -163,6 +163,17 @@ B3_INLINE const b3HullHalfEdge* b3GetHullEdges( const b3HullData* hull )
return (const b3HullHalfEdge*)( (intptr_t)hull + hull->edgeOffset );
}
/// Get read only hull planes.
B3_INLINE const b3Plane* b3GetHullPlanes( const b3HullData* hull )
{
if ( hull->planeOffset == 0 )
{
return NULL;
}
return (const b3Plane*)( (intptr_t)hull + hull->planeOffset );
}
/// Get read only hull faces.
B3_INLINE const b3HullFace* b3GetHullFaces( const b3HullData* hull )
{
@ -174,15 +185,30 @@ B3_INLINE const b3HullFace* b3GetHullFaces( const b3HullData* hull )
return (const b3HullFace*)( (intptr_t)hull + hull->faceOffset );
}
/// Get read only hull planes.
B3_INLINE const b3Plane* b3GetHullPlanes( const b3HullData* hull )
/// Get read only SOA vertices. This is an array of vertices with all x values,
/// y values, and z values as separate arrays. The array lengths are padded to
/// a multiple of 4. The padded values are repeats of the first value.
B3_INLINE const float* b3GetHullSoaVertices( const b3HullData* hull )
{
if ( hull->planeOffset == 0 )
if ( hull->soaVertexOffset == 0 )
{
return NULL;
}
return (const b3Plane*)( (intptr_t)hull + hull->planeOffset );
return (const float*)( (intptr_t)hull + hull->soaVertexOffset );
}
/// Get read only SOA unit normal vectors. This is an array of normals with all x values,
/// y values, and z values as separate arrays. The array lengths are padded to
/// a multiple of 4. The padded values are repeats of the first value.
B3_INLINE const float* b3GetHullSoaNormals( const b3HullData* hull )
{
if ( hull->soaNormalOffset == 0 )
{
return NULL;
}
return (const float*)( (intptr_t)hull + hull->soaNormalOffset );
}
/// Create a tessellated cylinder as a hull.
@ -426,14 +452,12 @@ B3_API b3CompoundData* b3CreateCompound( const b3CompoundDef* def );
/// Destroy a compound shape.
B3_API void b3DestroyCompound( b3CompoundData* compound );
/// If bytes is null then this returns the number of required bytes. This clones all the
/// data into the bytes buffer. This is expected to run offline or asynchronously.
/// This mutates the compound to nullify pointers, leaving the compound in an unusable state.
/// Cast the provided compound data to bytes, setting the internal pointers to null.
/// Use this before serializing the compound bytes.
B3_API uint8_t* b3ConvertCompoundToBytes( b3CompoundData* compound );
/// Convert bytes to compound. This does not clone. The bytes must remain in scope while the
/// compound is used. This is done to improve run-time performance and allow for instancing.
/// The bytes are mutated to fixup pointers.
/// Cast the provided bytes to compound data, setting up internal pointers.
/// Use this after de-serializing the compound bytes.
B3_API b3CompoundData* b3ConvertBytesToCompound( uint8_t* bytes, int byteCount );
/**@}*/ // compound

View file

@ -109,6 +109,23 @@ B3_API float b3GetStallThreshold( void );
/// The maximum number points to use for shape cast proxies (swept point cloud).
#define B3_MAX_SHAPE_CAST_POINTS 64
/// The number of iterations for gyroscopic torques.
#ifndef B3_GYROSCOPIC_ITERATIONS
#define B3_GYROSCOPIC_ITERATIONS 1
#endif
/// The maximum number of convex hull vertices. This is fixed for performance reasons.
#define B3_MAX_HULL_VERTICES 128
/// The maximum number of convex hull faces.
#define B3_MAX_HULL_FACES 128
/// The maximum number of convex hull edges. Full edges, not half-edges.
#define B3_MAX_HULL_EDGES 128
/// Relative tolerance used to determine if two edges are parallel.
#define B3_PARALLEL_EDGE_TOL 0.005f
/// These generous limits allow for easy hashing. See b3ShapePairKey.
#define B3_SHAPE_POWER 22
#define B3_CHILD_POWER ( 64 - 2 * B3_SHAPE_POWER )

View file

@ -1930,8 +1930,7 @@ typedef struct b3Capsule
* @{
*/
/// A hull vertex. Identified by a half-edge with this
/// vertex as its tail.
/// A hull vertex. Identified by a half-edge with this vertex as its tail.
typedef struct b3HullVertex
{
/// A half-edge that has this vertex as the origin
@ -1965,7 +1964,7 @@ typedef struct b3HullFace
} b3HullFace;
/// 64-bit hull version. Useful for validating serialized data.
#define B3_HULL_VERSION 0x9D4716CE3793900Eull
#define B3_HULL_VERSION 0xDA5150191B994C01ull
/// A convex hull.
/// @note This data structure has data hanging off the end and cannot be directly copied.
@ -2016,11 +2015,17 @@ typedef struct b3HullData
/// The face count. Hulls faces are convex polygons.
int faceCount;
/// Offset of the face plane array in bytes from the struct address.
int planeOffset;
/// Offset of the face array in bytes from the struct address.
int faceOffset;
/// Offset of the face plane array in bytes from the struct address.
int planeOffset;
/// Offset of structure of array (SOA) vertices
int soaVertexOffset;
/// Offset of structure of array (SOA) unit normal vectors
int soaNormalOffset;
/// Explicit padding. Hull identity is a content hash and memcmp over raw bytes,
/// so there must be no unnamed padding for struct copies to scramble.
@ -2035,9 +2040,15 @@ typedef struct b3BoxHull
b3HullVertex boxVertices[8]; ///< Box vertices.
b3Vec3 boxPoints[8]; ///< Box points.
b3HullHalfEdge boxEdges[24]; ///< Box half-edges.
b3HullFace boxFaces[6]; ///< Box faces.
uint8_t padding[2]; ///< Explicit padding, see b3HullData::padding.
b3Plane boxPlanes[6]; ///< Box face planes.
b3HullFace boxFaces[6]; ///< Box faces.
uint8_t padding[10]; ///< Explicit padding, see b3HullData::padding.
float vx[8]; ///< vertex x
float vy[8]; ///< vertex y
float vz[8]; ///< vertex z
float nx[8]; ///< normal x, padded to multiple of 4
float ny[8]; ///< normal y, padded to multiple of 4
float nz[8]; ///< normal z, padded to multiple of 4
} b3BoxHull;
/**@}*/ // hull
@ -2048,13 +2059,13 @@ typedef struct b3BoxHull
* @{
*/
/// This is used to create a re-usable collision mesh
/// This is used to create a re-usable collision mesh.
typedef struct b3MeshDef
{
/// Triangle vertices
b3Vec3* vertices;
/// Triangle vertex indices. 3 for each triangle.
/// Triangle vertex indices. 3 for each triangle. CCW winding.
int32_t* indices;
/// Triangle material index. 1 per triangle. Indexes into b3ShapeDef::materials.
@ -2408,7 +2419,7 @@ typedef struct b3CompoundDef
} b3CompoundDef;
/// The baked compound version depends on the tree, mesh, and hull versions.
#define B3_COMPOUND_VERSION ( 0x830778DB07086EB4ull ^ B3_DYNAMIC_TREE_VERSION ^ B3_MESH_VERSION ^ B3_HULL_VERSION )
#define B3_COMPOUND_VERSION ( 0xB11DCE70FAD5622Bull ^ B3_DYNAMIC_TREE_VERSION ^ B3_MESH_VERSION ^ B3_HULL_VERSION )
/// Meshes used in compounds have limited space for materials. If you have
/// a mesh with many materials, you can use it outside of the compound.
@ -2530,10 +2541,10 @@ typedef struct b3ChildShape
/// Tagged union.
union
{
b3Capsule capsule; ///< Capsule.
b3Capsule capsule; ///< Capsule.
const b3HullData* hull; ///< Hull.
b3Mesh mesh; ///< Mesh.
b3Sphere sphere; ///< Sphere.
b3Mesh mesh; ///< Mesh.
b3Sphere sphere; ///< Sphere.
};
/// Transform of the shape into compound local space.
@ -2943,12 +2954,12 @@ typedef struct b3DebugShape
/// Tagged union.
union
{
const b3Capsule* capsule; ///< Capsule shape.
const b3Capsule* capsule; ///< Capsule shape.
const b3CompoundData* compound; ///< Compound shape.
const b3HeightFieldData* heightField; ///< Height-field shape.
const b3HullData* hull; ///< Convex hull shape.
const b3Mesh* mesh; ///< Mesh shape with scale.
const b3Sphere* sphere; ///< Sphere shape.
const b3HullData* hull; ///< Convex hull shape.
const b3Mesh* mesh; ///< Mesh shape with scale.
const b3Sphere* sphere; ///< Sphere shape.
};
} b3DebugShape;