Skip to content

Commit

Permalink
World API coverage (#859)
Browse files Browse the repository at this point in the history
Added unit test to test all world APIs. Fixed access to max speed.
  • Loading branch information
erincatto authored Dec 20, 2024
1 parent 92dc214 commit f377034
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ b2WorldId b2CreateWorld( const b2WorldDef* def )
world->gravity = def->gravity;
world->hitEventThreshold = def->hitEventThreshold;
world->restitutionThreshold = def->restitutionThreshold;
world->maxLinearVelocity = def->maximumLinearSpeed;
world->maxLinearSpeed = def->maximumLinearSpeed;
world->contactPushSpeed = def->contactPushSpeed;
world->contactHertz = def->contactHertz;
world->contactDampingRatio = def->contactDampingRatio;
Expand Down Expand Up @@ -757,7 +757,7 @@ void b2World_Step( b2WorldId worldId, float timeStep, int subStepCount )
context.jointSoftness = b2MakeSoft( jointHertz, world->jointDampingRatio, context.h );

context.restitutionThreshold = world->restitutionThreshold;
context.maxLinearVelocity = world->maxLinearVelocity;
context.maxLinearVelocity = world->maxLinearSpeed;
context.enableWarmStarting = world->enableWarmStarting;

// Update contacts
Expand Down Expand Up @@ -1739,9 +1739,9 @@ void b2World_SetJointTuning( b2WorldId worldId, float hertz, float dampingRatio
world->jointDampingRatio = b2ClampFloat( dampingRatio, 0.0f, FLT_MAX );
}

void b2World_SetMaximumLinearSpeed( b2WorldId worldId, float maximumLinearVelocity )
void b2World_SetMaximumLinearSpeed( b2WorldId worldId, float maximumLinearSpeed )
{
B2_ASSERT( b2IsValidFloat( maximumLinearVelocity ) && maximumLinearVelocity > 0.0f );
B2_ASSERT( b2IsValidFloat( maximumLinearSpeed ) && maximumLinearSpeed > 0.0f );

b2World* world = b2GetWorldFromId( worldId );
B2_ASSERT( world->locked == false );
Expand All @@ -1750,13 +1750,13 @@ void b2World_SetMaximumLinearSpeed( b2WorldId worldId, float maximumLinearVeloci
return;
}

world->maxLinearVelocity = maximumLinearVelocity;
world->maxLinearSpeed = maximumLinearSpeed;
}

float b2World_GetMaximumLinearVelocity( b2WorldId worldId )
float b2World_GetMaximumLinearSpeed( b2WorldId worldId )
{
b2World* world = b2GetWorldFromId( worldId );
return world->maxLinearVelocity;
return world->maxLinearSpeed;
}

b2Profile b2World_GetProfile( b2WorldId worldId )
Expand Down
2 changes: 1 addition & 1 deletion src/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ typedef struct b2World
b2Vec2 gravity;
float hitEventThreshold;
float restitutionThreshold;
float maxLinearVelocity;
float maxLinearSpeed;
float contactPushSpeed;
float contactHertz;
float contactDampingRatio;
Expand Down
84 changes: 84 additions & 0 deletions test/test_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT

#include "constants.h"
#include "core.h"
#include "test_macros.h"

#include "box2d/box2d.h"
Expand Down Expand Up @@ -340,6 +341,88 @@ int TestWorldRecycle( void )
return 0;
}

static bool CustomFilter( b2ShapeId shapeIdA, b2ShapeId shapeIdB, void* context )
{
(void)shapeIdA;
(void)shapeIdB;
ENSURE( context == NULL );
return true;
}

static bool PreSolveStatic( b2ShapeId shapeIdA, b2ShapeId shapeIdB, b2Manifold* manifold, void* context )
{
(void)shapeIdA;
(void)shapeIdB;
(void)manifold;
ENSURE( context == NULL );
return false;
}

// This test is here to ensure all API functions link correctly.
int TestWorldCoverage( void )
{
b2WorldDef worldDef = b2DefaultWorldDef();

b2WorldId worldId = b2CreateWorld( &worldDef );
ENSURE( b2World_IsValid( worldId ) );

b2World_EnableSleeping( worldId, true );
b2World_EnableSleeping( worldId, false );
bool flag = b2World_IsSleepingEnabled( worldId );
ENSURE( flag == false );

b2World_EnableContinuous( worldId, false );
b2World_EnableContinuous( worldId, true );
flag = b2World_IsContinuousEnabled( worldId );
ENSURE( flag == true );

b2World_SetRestitutionThreshold( worldId, 0.0f );
b2World_SetRestitutionThreshold( worldId, 2.0f );
float value = b2World_GetRestitutionThreshold( worldId );
ENSURE( value == 2.0f );

b2World_SetHitEventThreshold( worldId, 0.0f );
b2World_SetHitEventThreshold( worldId, 100.0f );
value = b2World_GetHitEventThreshold( worldId );
ENSURE( value == 100.0f );

b2World_SetCustomFilterCallback( worldId, CustomFilter, NULL );
b2World_SetPreSolveCallback( worldId, PreSolveStatic, NULL );

b2Vec2 g = { 1.0f, 2.0f };
b2World_SetGravity( worldId, g );
b2Vec2 v = b2World_GetGravity( worldId );
ENSURE( v.x == g.x );
ENSURE( v.y == g.y );

b2ExplosionDef explosionDef = b2DefaultExplosionDef();
b2World_Explode( worldId, &explosionDef );

b2World_SetContactTuning( worldId, 10.0f, 2.0f, 4.0f );
b2World_SetJointTuning( worldId, 10.0f, 2.0f );

b2World_SetMaximumLinearSpeed( worldId, 10.0f );
value = b2World_GetMaximumLinearSpeed( worldId );
ENSURE( value == 10.0f );

b2World_EnableWarmStarting( worldId, true );
flag = b2World_IsWarmStartingEnabled( worldId );
ENSURE( flag == true );

int count = b2World_GetAwakeBodyCount( worldId );
ENSURE( count == 0 );

b2World_SetUserData( worldId, &value );
void* userData = b2World_GetUserData( worldId );
ENSURE( userData == &value );

b2World_Step( worldId, 1.0f, 1 );

b2DestroyWorld( worldId );

return 0;
}

int WorldTest( void )
{
RUN_SUBTEST( TestForAmy );
Expand All @@ -348,6 +431,7 @@ int WorldTest( void )
RUN_SUBTEST( DestroyAllBodiesWorld );
RUN_SUBTEST( TestIsValid );
RUN_SUBTEST( TestWorldRecycle );
RUN_SUBTEST( TestWorldCoverage );

return 0;
}

0 comments on commit f377034

Please sign in to comment.