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

@ -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 );