Missing functions (#21)

- add some missing functions
- added config.h for users that vendor Box3D
- joint unit tests
- improve joint limit consistency
- extend build matrix
This commit is contained in:
Erin Catto 2026-06-30 10:57:46 -07:00 • committed by GitHub
commit 8441b4a06d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 918 additions and 410 deletions

View file

@ -93,6 +93,7 @@ set(BOX3D_INCLUDE_FILES
../include/box3d/base.h
../include/box3d/box3d.h
../include/box3d/collision.h
../include/box3d/config.h
../include/box3d/constants.h
../include/box3d/id.h
../include/box3d/math_functions.h
@ -104,7 +105,7 @@ set(BOX3D_INCLUDE_FILES
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
add_library(box3d STATIC ${BOX3D_SOURCE_FILES} ${BOX3D_INCLUDE_FILES})
add_library(box3d ${BOX3D_SOURCE_FILES} ${BOX3D_INCLUDE_FILES})
# target_link_libraries(box3d PRIVATE eastl)

View file

@ -22,7 +22,7 @@
#include <stddef.h>
#include <string.h>
_Static_assert( B3_NAME_LENGTH >= 0, "minimum name length" );
_Static_assert( B3_BODY_NAME_LENGTH >= 0, "minimum name length" );
// Get a validated body from a world using an id.
b3Body* b3GetBodyFullId( b3World* world, b3BodyId bodyId )
@ -274,10 +274,10 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
if ( def->name )
{
#if defined( _MSC_VER )
strncpy_s( body->name, B3_NAME_LENGTH + 1, def->name, B3_NAME_LENGTH );
strncpy_s( body->name, B3_BODY_NAME_LENGTH + 1, def->name, B3_BODY_NAME_LENGTH );
#else
strncpy( body->name, def->name, B3_NAME_LENGTH );
body->name[B3_NAME_LENGTH] = 0;
strncpy( body->name, def->name, B3_BODY_NAME_LENGTH );
body->name[B3_BODY_NAME_LENGTH] = 0;
#endif
}
else
@ -1733,10 +1733,10 @@ void b3Body_SetName( b3BodyId bodyId, const char* name )
if ( name )
{
#if defined( _MSC_VER )
strncpy_s( body->name, B3_NAME_LENGTH + 1, name, B3_NAME_LENGTH );
strncpy_s( body->name, B3_BODY_NAME_LENGTH + 1, name, B3_BODY_NAME_LENGTH );
#else
strncpy( body->name, name, B3_NAME_LENGTH );
body->name[B3_NAME_LENGTH] = 0;
strncpy( body->name, name, B3_BODY_NAME_LENGTH );
body->name[B3_BODY_NAME_LENGTH] = 0;
#endif
}
else

View file

@ -123,7 +123,7 @@ typedef struct b3Body
// Used to check for invalid b3BodyId
uint16_t generation;
char name[B3_NAME_LENGTH + 1];
char name[B3_BODY_NAME_LENGTH + 1];
} b3Body;
// Body State

View file

@ -24,7 +24,7 @@ static inline uint32_t b3CTZ32( uint32_t block )
// This function doesn't need to be fast, so using the Ivy Bridge fallback.
static inline uint32_t b3CLZ32( uint32_t value )
{
#if 0
#if 1
// Use BSR (Bit Scan Reverse) which is available on Ivy Bridge
unsigned long index;

View file

@ -581,9 +581,6 @@ b3JointId b3CreatePrismaticJoint( b3WorldId worldId, const b3PrismaticJointDef*
b3JointId b3CreateRevoluteJoint( b3WorldId worldId, const b3RevoluteJointDef* def )
{
B3_CHECK_JOINT_DEF( def );
B3_ASSERT( def->lowerAngle <= def->upperAngle );
B3_ASSERT( def->lowerAngle >= -0.99f * B3_PI );
B3_ASSERT( def->upperAngle <= 0.99f * B3_PI );
b3World* world = b3GetUnlockedWorldFromId( worldId );
if (world == NULL)
@ -599,10 +596,12 @@ b3JointId b3CreateRevoluteJoint( b3WorldId worldId, const b3RevoluteJointDef* de
joint->revoluteJoint.hertz = def->hertz;
joint->revoluteJoint.dampingRatio = def->dampingRatio;
joint->revoluteJoint.targetAngle = b3ClampFloat( def->targetAngle, -B3_PI, B3_PI );
joint->revoluteJoint.lowerAngle = b3MinFloat( def->lowerAngle, def->upperAngle );
joint->revoluteJoint.upperAngle = b3MaxFloat( def->lowerAngle, def->upperAngle );
joint->revoluteJoint.lowerAngle = b3ClampFloat( joint->revoluteJoint.lowerAngle, -B3_PI, B3_PI );
joint->revoluteJoint.upperAngle = b3ClampFloat( joint->revoluteJoint.upperAngle, -B3_PI, B3_PI );
float lowerAngle = b3MinFloat( def->lowerAngle, def->upperAngle );
float upperAngle = b3MaxFloat( def->lowerAngle, def->upperAngle );
joint->revoluteJoint.lowerAngle = b3ClampFloat( lowerAngle, -0.99f * B3_PI, 0.99f * B3_PI );
joint->revoluteJoint.upperAngle = b3ClampFloat( upperAngle, -0.99f * B3_PI, 0.99f * B3_PI );
joint->revoluteJoint.maxMotorTorque = def->maxMotorTorque;
joint->revoluteJoint.motorSpeed = def->motorSpeed;
joint->revoluteJoint.enableSpring = def->enableSpring;
@ -617,9 +616,6 @@ b3JointId b3CreateRevoluteJoint( b3WorldId worldId, const b3RevoluteJointDef* de
b3JointId b3CreateSphericalJoint( b3WorldId worldId, const b3SphericalJointDef* def )
{
B3_CHECK_JOINT_DEF( def );
B3_ASSERT( def->lowerTwistAngle <= def->upperTwistAngle );
B3_ASSERT( def->lowerTwistAngle >= -0.99f * B3_PI );
B3_ASSERT( def->upperTwistAngle <= 0.99f * B3_PI );
B3_ASSERT( 0.0f <= def->coneAngle && def->coneAngle <= 0.99f * B3_PI );
B3_ASSERT( b3IsValidQuat( def->targetRotation ) );
@ -638,10 +634,12 @@ b3JointId b3CreateSphericalJoint( b3WorldId worldId, const b3SphericalJointDef*
joint->sphericalJoint.dampingRatio = def->dampingRatio;
joint->sphericalJoint.targetRotation = def->targetRotation;
joint->sphericalJoint.coneAngle = b3ClampFloat( def->coneAngle, 0.0f, 0.5f * B3_PI );
joint->sphericalJoint.lowerTwistAngle = b3MinFloat( def->lowerTwistAngle, def->upperTwistAngle );
joint->sphericalJoint.upperTwistAngle = b3MaxFloat( def->lowerTwistAngle, def->upperTwistAngle );
joint->sphericalJoint.lowerTwistAngle = b3ClampFloat( joint->sphericalJoint.lowerTwistAngle, -B3_PI, B3_PI );
joint->sphericalJoint.upperTwistAngle = b3ClampFloat( joint->sphericalJoint.upperTwistAngle, -B3_PI, B3_PI );
float lowerAngle = b3MinFloat( def->lowerTwistAngle, def->upperTwistAngle );
float upperAngle = b3MaxFloat( def->lowerTwistAngle, def->upperTwistAngle );
joint->sphericalJoint.lowerTwistAngle = b3ClampFloat( lowerAngle, -0.99f * B3_PI, 0.99f * B3_PI );
joint->sphericalJoint.upperTwistAngle = b3ClampFloat( upperAngle, -0.99f * B3_PI, 0.99f * B3_PI );
joint->sphericalJoint.maxMotorTorque = def->maxMotorTorque;
joint->sphericalJoint.motorVelocity = def->motorVelocity;
joint->sphericalJoint.enableSpring = def->enableSpring;
@ -1156,6 +1154,9 @@ static b3Vec3 b3GetJointConstraintTorque( b3World* world, b3Joint* joint )
switch ( joint->type )
{
case b3_parallelJoint:
return b3GetParallelJointTorque( world, base );
case b3_distanceJoint:
return b3Vec3_zero;

View file

@ -5,7 +5,7 @@
typedef struct b3World b3World;
// Callback invoked by b2ParallelFor to process a range of items. May be called
// Callback invoked by b3ParallelFor to process a range of items. May be called
// multiple times per worker: work is divided into blocks that workers claim
// atomically, so a worker that finishes early picks up the next unclaimed
// block instead of sitting idle. workerIndex is the worker identity and is

View file

@ -2203,6 +2203,28 @@ void* b3World_GetUserData( b3WorldId worldId )
return world->userData;
}
void b3World_SetFrictionCallback( b3WorldId worldId, b3FrictionCallback* callback )
{
b3World* world = b3GetUnlockedWorldFromId( worldId );
if ( world == NULL )
{
return;
}
world->frictionCallback = callback != NULL ? callback : b3DefaultFrictionCallback;
}
void b3World_SetRestitutionCallback( b3WorldId worldId, b3RestitutionCallback* callback )
{
b3World* world = b3GetUnlockedWorldFromId( worldId );
if ( world == NULL )
{
return;
}
world->restitutionCallback = callback != NULL ? callback : b3DefaultRestitutionCallback;
}
void b3World_SetWorkerCount( b3WorldId worldId, int count )
{
b3World* world = b3GetUnlockedWorldFromId( worldId );

View file

@ -8,12 +8,16 @@
#include <stdbool.h>
#include <stdint.h>
// Software prefetch hint. _MM_HINT_T0 brings the line into all cache levels.
// MSVC exposes _mm_prefetch via <intrin.h>; clang/gcc accept it via <xmmintrin.h>
// on x86 and provide __builtin_prefetch elsewhere.
// Software prefetch hint. T0 brings the line into all cache levels.
// On x86 MSVC exposes _mm_prefetch, ARM MSVC uses __prefetch instead.
// clang/gcc provide __builtin_prefetch on every target.
#if defined( B3_COMPILER_MSVC )
#include <intrin.h>
#if defined( B3_CPU_X86_X64 )
#define b3Prefetch( addr ) _mm_prefetch( (const char*)( addr ), _MM_HINT_T0 )
#else
#define b3Prefetch( addr ) __prefetch( (const void*)( addr ) )
#endif
#elif defined( B3_COMPILER_CLANG ) || defined( B3_COMPILER_GCC )
#define b3Prefetch( addr ) __builtin_prefetch( (const void*)( addr ), 0, 3 )
#else

View file

@ -223,6 +223,47 @@ float b3PrismaticJoint_GetMaxMotorForce( b3JointId jointId )
return base->prismaticJoint.maxMotorForce;
}
float b3PrismaticJoint_GetMotorForce( b3JointId jointId )
{
b3World* world = b3GetWorld( jointId.world0 );
b3JointSim* base = b3GetJointSimCheckType( jointId, b3_prismaticJoint );
return world->inv_h * base->prismaticJoint.motorImpulse;
}
float b3PrismaticJoint_GetSpeed( b3JointId jointId )
{
b3World* world = b3GetWorld( jointId.world0 );
b3JointSim* base = b3GetJointSimCheckType( jointId, b3_prismaticJoint );
b3Body* bodyA = b3Array_Get( world->bodies, base->bodyIdA );
b3Body* bodyB = b3Array_Get( world->bodies, base->bodyIdB );
b3BodySim* bodySimA = b3GetBodySim( world, bodyA );
b3BodySim* bodySimB = b3GetBodySim( world, bodyB );
b3BodyState* stateA = b3GetBodyState( world, bodyA );
b3BodyState* stateB = b3GetBodyState( world, bodyB );
b3Quat qA = bodySimA->transform.q;
b3Quat qB = bodySimB->transform.q;
b3Vec3 axisA = b3RotateVector( qA, b3RotateVector( base->localFrameA.q, b3Vec3_axisX ) );
b3Vec3 rA = b3RotateVector( qA, b3Sub( base->localFrameA.p, bodySimA->localCenter ) );
b3Vec3 rB = b3RotateVector( qB, b3Sub( base->localFrameB.p, bodySimB->localCenter ) );
// Difference the centers in double so the speed stays exact far from the origin.
b3Vec3 d = b3Add( b3SubPos( bodySimB->center, bodySimA->center ), b3Sub( rB, rA ) );
b3Vec3 vA = stateA ? stateA->linearVelocity : b3Vec3_zero;
b3Vec3 vB = stateB ? stateB->linearVelocity : b3Vec3_zero;
b3Vec3 wA = stateA ? stateA->angularVelocity : b3Vec3_zero;
b3Vec3 wB = stateB ? stateB->angularVelocity : b3Vec3_zero;
b3Vec3 vRel = b3Sub( b3Add( vB, b3Cross( wB, rB ) ), b3Add( vA, b3Cross( wA, rA ) ) );
// The axis moves with body A, so account for its rotation.
float speed = b3Dot( d, b3Cross( wA, axisA ) ) + b3Dot( axisA, vRel );
return speed;
}
b3Vec3 b3GetPrismaticJointForce( b3World* world, b3JointSim* base )
{
b3WorldTransform transformA = b3GetBodyTransform( world, base->bodyIdA );

View file

@ -938,7 +938,7 @@ void b3RecInternTag( b3Recording* rec, uint64_t key, uint64_t id, const char* na
tag->key = key;
tag->id = id;
int n = 0;
while ( name != NULL && name[n] != '\0' && n < B3_NAME_LENGTH )
while ( name != NULL && name[n] != '\0' && n < B3_BODY_NAME_LENGTH )
{
tag->name[n] = name[n];
n++;

View file

@ -120,7 +120,7 @@ typedef struct b3RecTag
{
uint64_t key; // hash of (id, name)
uint64_t id; // entity/actor id
char name[B3_NAME_LENGTH + 1]; // query label
char name[B3_BODY_NAME_LENGTH + 1]; // query label
} b3RecTag;
// User-owned recording buffer. The world appends into it while active; the host saves and
@ -356,7 +356,7 @@ void b3RecWriteRegistry( b3Recording* rec );
uint64_t b3HashQueryTag( uint64_t id, const char* name );
// Record a key->(id, name) mapping once, deduped by key (a repeated key keeps its first id/name).
// The name is clamped to B3_NAME_LENGTH.
// The name is clamped to B3_BODY_NAME_LENGTH.
void b3RecInternTag( b3Recording* rec, uint64_t key, uint64_t id, const char* name );
// Intern each large geometry kind and return a stable u32 id for use in create ops.

View file

@ -385,9 +385,9 @@ const char* b3RecR_STR( b3RecReader* rdr )
}
int n = (int)len;
if ( n > B3_NAME_LENGTH )
if ( n > B3_BODY_NAME_LENGTH )
{
n = B3_NAME_LENGTH;
n = B3_BODY_NAME_LENGTH;
}
b3RecRdrCheck( rdr, (int)len );
if ( rdr->ok && n > 0 )
@ -2332,7 +2332,7 @@ static void b3RecLoadTags( b3RecReader* rdr, const uint8_t* rp, const uint8_t* d
{
break;
}
int n = len > B3_NAME_LENGTH ? B3_NAME_LENGTH : (int)len;
int n = len > B3_BODY_NAME_LENGTH ? B3_BODY_NAME_LENGTH : (int)len;
tags[loaded].key = key;
tags[loaded].id = id;
if ( n > 0 )

View file

@ -91,7 +91,7 @@ typedef struct b3RecReader
int matScratchCap;
// Scratch for string reads: rotating slots, valid until the next 4 STR reads
char strBufs[4][B3_NAME_LENGTH + 1];
char strBufs[4][B3_BODY_NAME_LENGTH + 1];
int strNext;
// Preloaded geometry registry

View file

@ -73,18 +73,16 @@ float b3RevoluteJoint_GetUpperLimit( b3JointId jointId )
void b3RevoluteJoint_SetLimits( b3JointId jointId, float lowerLimitRadians, float upperLimitRadians )
{
B3_ASSERT( lowerLimitRadians <= upperLimitRadians );
B3_ASSERT( b3IsValidFloat( lowerLimitRadians ) && b3IsValidFloat( upperLimitRadians ) );
b3World* world = b3GetWorld( jointId.world0 );
B3_REC( world, RevoluteJointSetLimits, jointId, lowerLimitRadians, upperLimitRadians );
float lowerAngle = b3ClampFloat( lowerLimitRadians, -B3_PI, B3_PI );
float upperAngle = b3ClampFloat( upperLimitRadians, -B3_PI, B3_PI );
float lowerAngle = b3MinFloat( lowerLimitRadians, upperLimitRadians );
float upperAngle = b3MaxFloat( lowerLimitRadians, upperLimitRadians );
b3JointSim* base = b3GetJointSimCheckType( jointId, b3_revoluteJoint );
base->revoluteJoint.lowerAngle = lowerAngle;
base->revoluteJoint.upperAngle = upperAngle;
base->revoluteJoint.lowerImpulse = 0.0f;
base->revoluteJoint.upperImpulse = 0.0f;
base->revoluteJoint.lowerAngle = b3ClampFloat( lowerAngle, -0.99f * B3_PI, 0.99f * B3_PI );
base->revoluteJoint.upperAngle = b3ClampFloat( upperAngle, -0.99f * B3_PI, 0.99f * B3_PI );
}
float b3RevoluteJoint_GetAngle( b3JointId jointId )
@ -219,6 +217,13 @@ float b3RevoluteJoint_GetMaxMotorTorque( b3JointId jointId )
return base->revoluteJoint.maxMotorTorque;
}
float b3RevoluteJoint_GetMotorTorque( b3JointId jointId )
{
b3World* world = b3GetWorld( jointId.world0 );
b3JointSim* base = b3GetJointSimCheckType( jointId, b3_revoluteJoint );
return world->inv_h * base->revoluteJoint.motorImpulse;
}
b3Vec3 b3GetRevoluteJointForce( b3World* world, b3JointSim* base )
{
b3Vec3 force = b3MulSV( world->inv_h, base->revoluteJoint.linearImpulse );

View file

@ -1821,7 +1821,7 @@ void b3Solve( b3World* world, b3StepContext* stepContext )
}
}
// The calling thread of b2World_Step also enters b2SolverTask as worker 0 and races for the
// The calling thread of b3World_Step also enters b3SolverTask as worker 0 and races for the
// orchestrator slot via the CAS inside. This guarantees progress even when the user's task
// system can't run the queued worker 0 promptly: it might schedule out of order, have fewer
// threads than workerCount, or invert priority by parking the calling thread in finishTaskFcn.

View file

@ -249,7 +249,7 @@ typedef struct b3StepContext
char padding2[64];
// Race flag claimed by whichever runner reaches b3SolverTask with workerIndex 0 first.
// The calling thread of b2World_Step also races for this slot so the orchestrator can
// The calling thread of b3World_Step also races for this slot so the orchestrator can
// always make progress, regardless of how the user's task system schedules tasks (out
// of order, fewer threads than workers, or synchronously inside enqueueTaskFcn). The
// loser of the race no-ops as workerIndex 0.

View file

@ -103,14 +103,13 @@ void b3SphericalJoint_SetTwistLimits( b3JointId jointId, float lowerLimitRadians
B3_ASSERT( b3IsValidFloat( lowerLimitRadians ) && b3IsValidFloat( upperLimitRadians ) );
b3World* world = b3GetWorld( jointId.world0 );
B3_REC( world, SphericalJointSetTwistLimits, jointId, lowerLimitRadians, upperLimitRadians );
float lowerTwistAngle = b3MinFloat( lowerLimitRadians, upperLimitRadians );
float upperTwistAngle = b3MaxFloat( lowerLimitRadians, upperLimitRadians );
lowerTwistAngle = b3ClampFloat( lowerTwistAngle, -B3_PI, B3_PI );
upperTwistAngle = b3ClampFloat( upperTwistAngle, -B3_PI, B3_PI );
float lowerAngle = b3MinFloat( lowerLimitRadians, upperLimitRadians );
float upperAngle = b3MaxFloat( lowerLimitRadians, upperLimitRadians );
b3JointSim* base = b3GetJointSimCheckType( jointId, b3_sphericalJoint );
base->sphericalJoint.lowerTwistAngle = lowerTwistAngle;
base->sphericalJoint.upperTwistAngle = upperTwistAngle;
base->sphericalJoint.lowerTwistAngle = b3ClampFloat( lowerAngle, -0.99f * B3_PI, 0.99f * B3_PI );
base->sphericalJoint.upperTwistAngle = b3ClampFloat( upperAngle, -0.99f * B3_PI, 0.99f * B3_PI );
}
float b3SphericalJoint_GetTwistAngle( b3JointId jointId )
@ -245,6 +244,13 @@ float b3SphericalJoint_GetMaxMotorTorque( b3JointId jointId )
return base->sphericalJoint.maxMotorTorque;
}
b3Vec3 b3SphericalJoint_GetMotorTorque( b3JointId jointId )
{
b3World* world = b3GetWorld( jointId.world0 );
b3JointSim* base = b3GetJointSimCheckType( jointId, b3_sphericalJoint );
return b3MulSV( world->inv_h, base->sphericalJoint.motorImpulse );
}
b3Vec3 b3GetSphericalJointForce( b3World* world, b3JointSim* base )
{
b3Vec3 force = b3MulSV( world->inv_h, base->sphericalJoint.linearImpulse );

View file

@ -140,6 +140,13 @@ b3DebugDraw b3DefaultDebugDraw( void )
draw.DrawBoxFcn = b3EmptyDrawBox;
draw.DrawStringFcn = b3EmptyDrawString;
// Not too small, not too big.
float h = 100.0f * b3GetLengthUnitsPerMeter();
draw.drawingBounds = (b3AABB){
.lowerBound = { -h, -h, -h },
.upperBound = { h, h, h },
};
draw.jointScale = 1.0f;
draw.forceScale = 1.0f;