More body flag fixes (#9)

better name handling
This commit is contained in:
Erin Catto 2026-05-29 21:35:38 -07:00 • committed by GitHub
commit 48d9f1dd5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 223 additions and 88 deletions

View file

@ -482,6 +482,16 @@ B3_API void b3Body_SetBullet( b3BodyId bodyId, bool flag );
/// Is this body a bullet?
B3_API bool b3Body_IsBullet( 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;
/// only contacts created after this call see the new value.
/// @see b3BodyDef::enableContactRecycling
B3_API void b3Body_EnableContactRecycling( b3BodyId bodyId, bool flag );
/// Is contact recycling enabled on this body?
B3_API bool b3Body_IsContactRecyclingEnabled( b3BodyId bodyId );
/// Enable/disable hit events on all shapes
/// @see b3ShapeDef::enableHitEvents
B3_API void b3Body_EnableHitEvents( b3BodyId bodyId, bool enableHitEvents );

View file

@ -86,7 +86,7 @@ B3_API float b3GetLengthUnitsPerMeter( void );
/// The time that a body must be still before it will go to sleep. In seconds.
#define B3_TIME_TO_SLEEP 0.5f
/// Maximum length of the body name. Must be at least 1. Includes null termination.
/// Maximum length of the body name. Can be 0 if you don't need names.
#ifndef B3_NAME_LENGTH
#define B3_NAME_LENGTH 18
#endif

View file

@ -108,7 +108,7 @@ Matrix4 Camera::GetWorldMatrix() const
return m;
}
static Matrix4 InvertOrtho( const Matrix4& m)
static Matrix4 InvertOrthonormal( const Matrix4& m)
{
// [ R | t ]
// M = [ --+-- ]
@ -123,7 +123,9 @@ static Matrix4 InvertOrtho( const Matrix4& m)
r.cx = {m.cx.x, m.cx.y, m.cx.z };
r.cy = {m.cy.x, m.cy.y, m.cy.z };
r.cz = {m.cz.x, m.cz.y, m.cz.z };
b3Matrix3 invR = b3InvertMatrix(r);
// todo just transpose in place into out matrix
b3Matrix3 invR = b3Transpose(r);
b3Vec3 t{m.cw.x, m.cw.y, m.cw.z };
b3Vec3 invRXT = invR * t;
@ -140,7 +142,7 @@ static Matrix4 InvertOrtho( const Matrix4& m)
Matrix4 Camera::GetViewMatrix() const
{
Matrix4 v = GetWorldMatrix();
v = InvertOrtho(v);
v = InvertOrthonormal(v);
return v;
}

View file

@ -733,44 +733,6 @@ void DrawHull( Scene* scene, const b3Transform& transform, const b3Hull* hull, b
}
}
void DrawEllipsoid( Scene* scene, const b3Transform& transform, b3Vec3 radius, b3HexColor color )
{
b3Vec3 ex = { 1.0f, 0.0, 0.0f };
b3Vec3 ey = { 0.0f, 1.0, 0.0f };
b3Vec3 ez = { 0.0f, 0.0, 1.0f };
for ( float angle = 0.0f; angle < 360.0f; angle += 5.0f )
{
float alpha1 = B3_DEG_TO_RAD * ( angle + 0.0f );
float alpha2 = B3_DEG_TO_RAD * ( angle + 5.0f );
// XY - Plane
{
b3Vec3 vertex1 = radius * ( b3Cos( alpha1 ) * ex + b3Sin( alpha1 ) * ey );
b3Vec3 vertex2 = radius * ( b3Cos( alpha2 ) * ex + b3Sin( alpha2 ) * ey );
vertex1 = b3TransformPoint( transform, vertex1 );
vertex2 = b3TransformPoint( transform, vertex2 );
DrawLine( scene, vertex1, vertex2, color );
}
// XZ - Plane
{
b3Vec3 vertex1 = radius * ( b3Cos( alpha1 ) * ex + b3Sin( alpha1 ) * ez );
b3Vec3 vertex2 = radius * ( b3Cos( alpha2 ) * ex + b3Sin( alpha2 ) * ez );
vertex1 = b3TransformPoint( transform, vertex1 );
vertex2 = b3TransformPoint( transform, vertex2 );
DrawLine( scene, vertex1, vertex2, color );
}
// YZ - Plane
{
b3Vec3 vertex1 = radius * ( b3Cos( alpha1 ) * ex + b3Sin( alpha1 ) * ez );
b3Vec3 vertex2 = radius * ( b3Cos( alpha2 ) * ey + b3Sin( alpha2 ) * ez );
vertex1 = b3TransformPoint( transform, vertex1 );
vertex2 = b3TransformPoint( transform, vertex2 );
DrawLine( scene, vertex1, vertex2, color );
}
}
}
void DrawTransform( Scene* scene, const b3Transform& transform, float length )
{
b3Vec3 p = transform.p;

View file

@ -28,7 +28,6 @@ void DrawCross( Scene* scene, b3Vec3 center, float radius, b3HexColor color );
void DrawSphere( Scene* scene, const b3Transform& transform, const b3Sphere& sphere, b3HexColor color );
void DrawCapsule( Scene* scene, const b3Transform& transform, const b3Capsule& capsule, b3HexColor color );
void DrawHull( Scene* scene, const b3Transform& transform, const b3Hull* hull, b3HexColor color, bool drawNormals );
void DrawEllipsoid( Scene* scene, const b3Transform& transform, b3Vec3 radius, b3HexColor color );
void DrawTransform( Scene* scene, const b3Transform& transform, float length );
// Debug points

View file

@ -21,6 +21,8 @@
#include <stddef.h>
#include <string.h>
_Static_assert( B3_NAME_LENGTH >= 0, "minimum name length" );
// Get a validated body from a world using an id.
b3Body* b3GetBodyFullId( b3World* world, b3BodyId bodyId )
{
@ -270,22 +272,16 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
if ( def->name )
{
int i = 0;
while ( i < B3_NAME_LENGTH - 1 && def->name[i] != 0 )
{
body->name[i] = def->name[i];
i += 1;
}
while ( i < B3_NAME_LENGTH )
{
body->name[i] = 0;
i += 1;
}
#if defined( _MSC_VER )
strncpy_s( body->name, B3_NAME_LENGTH + 1, def->name, B3_NAME_LENGTH );
#else
strncpy( body->name, def->name, B3_NAME_LENGTH );
body->name[B3_NAME_LENGTH] = 0;
#endif
}
else
{
memset( body->name, 0, B3_NAME_LENGTH * sizeof( char ) );
memset( body->name, 0, sizeof( body->name ) );
}
body->userData = def->userData;
@ -1661,22 +1657,16 @@ void b3Body_SetName( b3BodyId bodyId, const char* name )
if ( name )
{
int i = 0;
while ( i < B3_NAME_LENGTH - 1 && name[i] != 0 )
{
body->name[i] = name[i];
i += 1;
}
while ( i < B3_NAME_LENGTH )
{
body->name[i] = 0;
i += 1;
}
#if defined( _MSC_VER )
strncpy_s( body->name, B3_NAME_LENGTH + 1, name, B3_NAME_LENGTH );
#else
strncpy( body->name, name, B3_NAME_LENGTH );
body->name[B3_NAME_LENGTH] = 0;
#endif
}
else
{
memset( body->name, 0, B3_NAME_LENGTH * sizeof( char ) );
memset( body->name, 0, sizeof( body->name ) );
}
}
@ -1940,8 +1930,6 @@ void b3Body_EnableSleep( b3BodyId bodyId, bool enableSleep )
return;
}
world->locked = true;
b3Body* body = b3GetBodyFullId( world, bodyId );
bool flag = ( body->flags & b3_enableSleep ) == b3_enableSleep;
@ -1950,7 +1938,10 @@ void b3Body_EnableSleep( b3BodyId bodyId, bool enableSleep )
return;
}
world->locked = true;
body->flags = enableSleep ? body->flags | b3_enableSleep : body->flags & ~b3_enableSleep;
b3SyncBodyFlags( world, body );
if ( enableSleep == false )
{
@ -2221,25 +2212,54 @@ void b3Body_SetBullet( b3BodyId bodyId, bool flag )
return;
}
b3Body* body = b3GetBodyFullId( world, bodyId );
b3BodySim* bodySim = b3GetBodySim( world, body );
uint32_t newFlag = flag ? b3_isBullet : 0;
if ( flag )
b3Body* body = b3GetBodyFullId( world, bodyId );
if ( ( body->flags & b3_isBullet ) == newFlag )
{
bodySim->flags |= b3_isBullet;
}
else
{
bodySim->flags &= ~b3_isBullet;
return;
}
body->flags &= ~b3_isBullet;
body->flags |= newFlag;
b3SyncBodyFlags( world, body );
}
bool b3Body_IsBullet( b3BodyId bodyId )
{
b3World* world = b3GetWorld( bodyId.world0 );
b3Body* body = b3GetBodyFullId( world, bodyId );
b3BodySim* bodySim = b3GetBodySim( world, body );
return ( bodySim->flags & b3_isBullet ) != 0;
return ( body->flags & b3_isBullet ) != 0;
}
void b3Body_EnableContactRecycling( b3BodyId bodyId, bool flag )
{
b3World* world = b3GetUnlockedWorld( bodyId.world0 );
if ( world == NULL )
{
return;
}
uint32_t newFlag = flag ? b3_bodyEnableContactRecycling : 0;
b3Body* body = b3GetBodyFullId( world, bodyId );
if ( ( body->flags & b3_bodyEnableContactRecycling ) == newFlag )
{
return;
}
body->flags &= ~b3_bodyEnableContactRecycling;
body->flags |= newFlag;
b3SyncBodyFlags( world, body );
}
bool b3Body_IsContactRecyclingEnabled( b3BodyId bodyId )
{
b3World* world = b3GetWorld( bodyId.world0 );
b3Body* body = b3GetBodyFullId( world, bodyId );
return ( body->flags & b3_bodyEnableContactRecycling ) != 0;
}
void b3Body_EnableHitEvents( b3BodyId bodyId, bool enableHitEvents )

View file

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

View file

@ -512,7 +512,7 @@ int b3BroadPhase_GetShapeIndex( b3BroadPhase* bp, int proxyKey )
return (int)b3DynamicTree_GetUserData( bp->trees + typeIndex, proxyId );
}
void b3ValidateBroadphase( const b3BroadPhase* bp )
void b3ValidateBroadPhase( const b3BroadPhase* bp )
{
b3DynamicTree_Validate( bp->trees + b3_dynamicBody );
b3DynamicTree_Validate( bp->trees + b3_kinematicBody );

View file

@ -61,7 +61,7 @@ int b3BroadPhase_GetShapeIndex( b3BroadPhase* bp, int proxyKey );
void b3UpdateBroadPhasePairs( b3World* world );
bool b3BroadPhase_TestOverlap( const b3BroadPhase* bp, int proxyKeyA, int proxyKeyB );
void b3ValidateBroadphase( const b3BroadPhase* bp );
void b3ValidateBroadPhase( const b3BroadPhase* bp );
void b3ValidateNoEnlarged( const b3BroadPhase* bp );
// This is what triggers new contact pairs to be created

View file

@ -2119,7 +2119,7 @@ void b3Solve( b3World* world, b3StepContext* stepContext )
}
}
b3ValidateBroadphase( &world->broadPhase );
b3ValidateBroadPhase( &world->broadPhase );
world->profile.refit = b3GetMilliseconds( refitTicks );
b3TracyCZoneEnd( refit_bvh );

View file

@ -618,7 +618,6 @@ static int TestCompoundHitEvents( void )
return 0;
}
#if 0
static int TestSetWorkerCount( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
@ -650,8 +649,8 @@ static int TestSetWorkerCount( void )
StepJunkyard( worldId, 5 );
b3World_SetWorkerCount( worldId, B2_MAX_WORKERS + 10 );
ENSURE( b3World_GetWorkerCount( worldId ) == B2_MAX_WORKERS );
b3World_SetWorkerCount( worldId, B3_MAX_WORKERS + 10 );
ENSURE( b3World_GetWorkerCount( worldId ) == B3_MAX_WORKERS );
StepJunkyard( worldId, 2 );
@ -659,7 +658,6 @@ static int TestSetWorkerCount( void )
return 0;
}
#endif
// This tests continuous collision and mesh contact stability
static int TestMeshDrop( void )
@ -740,6 +738,146 @@ static int TestOverflowColorPile( void )
return 0;
}
// Exposes that b3Body_EnableSleep mutates body->flags but never syncs
// bodySim->flags / bodyState->flags. When a body created with
// enableSleep=false is later flipped on, body->flags gains b3_enableSleep
// but bodySim/bodyState do not. The flag-sync assertion in
// b3ValidateSolverSets then fires on the next world step.
//
// Under Debug + BOX3D_VALIDATE this crashes; after b3Body_EnableSleep
// calls b3SyncBodyFlags the test runs cleanly.
static int EnableSleepFlagSyncTest( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.enableSleep = false;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
ENSURE( b3Body_IsSleepEnabled( bodyId ) == false );
b3Body_EnableSleep( bodyId, true );
ENSURE( b3Body_IsSleepEnabled( bodyId ) == true );
b3World_Step( worldId, 1.0f / 60.0f, 4 );
b3DestroyWorld( worldId );
return 0;
}
// Exposes that b3Body_SetBullet writes only bodySim->flags without
// touching body->flags, while b3SyncBodyFlags overwrites bodySim from
// body->flags. Any subsequent b3Body_SetMotionLocks or b3Body_SetType
// silently wipes (or re-asserts) the bullet bit.
//
// Reproducer A: create with isBullet=false, SetBullet(true), then
// SetMotionLocks. b3Body_IsBullet returns false today (wiped); should be
// true after fix.
//
// Reproducer B: create with isBullet=true, SetBullet(false), then
// SetMotionLocks. b3Body_IsBullet returns true today (re-asserted); should
// be false after fix.
static int SetBulletDriftTest( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.isBullet = false;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
ENSURE( b3Body_IsBullet( bodyId ) == false );
b3Body_SetBullet( bodyId, true );
ENSURE( b3Body_IsBullet( bodyId ) == true );
b3MotionLocks locks = { 0 };
locks.linearX = true;
b3Body_SetMotionLocks( bodyId, locks );
ENSURE( b3Body_IsBullet( bodyId ) == true );
}
{
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.isBullet = true;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
ENSURE( b3Body_IsBullet( bodyId ) == true );
b3Body_SetBullet( bodyId, false );
ENSURE( b3Body_IsBullet( bodyId ) == false );
b3MotionLocks locks = { 0 };
locks.linearX = true;
b3Body_SetMotionLocks( bodyId, locks );
ENSURE( b3Body_IsBullet( bodyId ) == false );
}
b3DestroyWorld( worldId );
return 0;
}
// Regression: b3Body_EnableSleep used to set world->locked = true before checking
// for a no-op change, then early-return without unlocking. The next mutator call
// would then trip the assert in b3GetUnlockedWorld.
static int EnableSleepNoopUnlockTest( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
bodyDef.enableSleep = true;
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
// No-op: enableSleep is already true. Must not leak the world lock.
b3Body_EnableSleep( bodyId, true );
// Would assert in b3GetUnlockedWorld if the lock had leaked.
b3Body_EnableSleep( bodyId, false );
ENSURE( b3Body_IsSleepEnabled( bodyId ) == false );
b3DestroyWorld( worldId );
return 0;
}
static int EnableContactRecyclingTest( void )
{
b3WorldDef worldDef = b3DefaultWorldDef();
b3WorldId worldId = b3CreateWorld( &worldDef );
b3BodyDef bodyDef = b3DefaultBodyDef();
bodyDef.type = b3_dynamicBody;
// Default is enabled
b3BodyId bodyA = b3CreateBody( worldId, &bodyDef );
ENSURE( b3Body_IsContactRecyclingEnabled( bodyA ) == true );
b3Body_EnableContactRecycling( bodyA, false );
ENSURE( b3Body_IsContactRecyclingEnabled( bodyA ) == false );
b3Body_EnableContactRecycling( bodyA, true );
ENSURE( b3Body_IsContactRecyclingEnabled( bodyA ) == true );
// Per-def opt-out at creation
bodyDef.enableContactRecycling = false;
b3BodyId bodyB = b3CreateBody( worldId, &bodyDef );
ENSURE( b3Body_IsContactRecyclingEnabled( bodyB ) == false );
// Stepping after toggling must not trip the flag-sync validator
b3World_Step( worldId, 1.0f / 60.0f, 4 );
b3DestroyWorld( worldId );
return 0;
}
int WorldTest( void )
{
RUN_SUBTEST( HelloWorld );
@ -754,7 +892,11 @@ int WorldTest( void )
RUN_SUBTEST( TestCompoundHitEvents );
RUN_SUBTEST( TestMeshDrop );
RUN_SUBTEST( TestOverflowColorPile );
// RUN_SUBTEST( TestSetWorkerCount );
RUN_SUBTEST( SetBulletDriftTest );
RUN_SUBTEST( EnableSleepFlagSyncTest );
RUN_SUBTEST( EnableSleepNoopUnlockTest );
RUN_SUBTEST( EnableContactRecyclingTest );
RUN_SUBTEST( TestSetWorkerCount );
return 0;
}