diff --git a/include/box3d/box3d.h b/include/box3d/box3d.h index 2275663..e06bb5d 100644 --- a/include/box3d/box3d.h +++ b/include/box3d/box3d.h @@ -167,6 +167,21 @@ B3_API void b3World_SetPreSolveCallback( b3WorldId worldId, b3PreSolveFcn* fcn, /// @see b3WorldDef B3_API void b3World_SetGravity( b3WorldId worldId, b3Vec3 gravity ); +/// GameCult fork: an external constraint solver run inside the soft step. Called on the solver's +/// orchestrating thread subStepCount times per b3World_Step, after the biased solve and before +/// positions integrate (also when no body is awake). Not recorded. +B3_API void b3World_SetSubStepCallback( b3WorldId worldId, b3SubStepFcn* fcn, void* context ); + +/// GameCult fork, valid only inside the substep callback: the body's centre of mass and rotation at +/// the start of this substep and its velocity after the biased solve. Returns false (and zero +/// velocity) for a body the solver does not move this step: static or asleep. +B3_API bool b3Body_GetSubStepState( b3BodyId bodyId, b3Pos* center, b3Quat* rotation, b3Vec3* linearVelocity, + b3Vec3* angularVelocity ); + +/// GameCult fork, valid only inside the substep callback: a velocity change from a linear impulse at +/// the centre of mass and an angular impulse, on an awake body. Never wakes a body. +B3_API void b3Body_ApplySubStepImpulse( b3BodyId bodyId, b3Vec3 linearImpulse, b3Vec3 angularImpulse ); + /// Get the gravity vector B3_API b3Vec3 b3World_GetGravity( b3WorldId worldId ); diff --git a/include/box3d/types.h b/include/box3d/types.h index 1e52e4d..020e17f 100644 --- a/include/box3d/types.h +++ b/include/box3d/types.h @@ -94,6 +94,9 @@ typedef bool b3PreSolveFcn( b3ShapeId shapeIdA, b3ShapeId shapeIdB, b3Pos point, /// @ingroup world typedef bool b3OverlapResultFcn( b3ShapeId shapeId, void* context ); +/// GameCult fork: see b3World_SetSubStepCallback +typedef void b3SubStepFcn( b3WorldId worldId, int subStepIndex, float h, void* context ); + /// Prototype callback for ray casts. /// Called for each shape found in the query. You control how the ray cast /// proceeds by returning a float: diff --git a/src/body.c b/src/body.c index aa4e91e..37ed757 100644 --- a/src/body.c +++ b/src/body.c @@ -1119,6 +1119,44 @@ b3Vec3 b3Body_GetLinearVelocity( b3BodyId bodyId ) return b3Vec3_zero; } +bool b3Body_GetSubStepState( b3BodyId bodyId, b3Pos* center, b3Quat* rotation, b3Vec3* linearVelocity, + b3Vec3* angularVelocity ) +{ + b3World* world = b3GetWorld( bodyId.world0 ); + B3_ASSERT( world->inSubStep ); + b3Body* body = b3GetBodyFullId( world, bodyId ); + b3BodySim* sim = b3GetBodySim( world, body ); + b3BodyState* state = b3GetBodyState( world, body ); + if ( state == NULL ) + { + *center = sim->center; + *rotation = sim->transform.q; + *linearVelocity = b3Vec3_zero; + *angularVelocity = b3Vec3_zero; + return false; + } + *center = b3OffsetPos( sim->center, state->deltaPosition ); + *rotation = b3NormalizeQuat( b3MulQuat( state->deltaRotation, sim->transform.q ) ); + *linearVelocity = state->linearVelocity; + *angularVelocity = state->angularVelocity; + return true; +} + +void b3Body_ApplySubStepImpulse( b3BodyId bodyId, b3Vec3 linearImpulse, b3Vec3 angularImpulse ) +{ + b3World* world = b3GetWorld( bodyId.world0 ); + B3_ASSERT( world->inSubStep ); + b3Body* body = b3GetBodyFullId( world, bodyId ); + b3BodyState* state = b3GetBodyState( world, body ); + if ( state == NULL ) + { + return; + } + b3BodySim* sim = b3GetBodySim( world, body ); + state->linearVelocity = b3MulAdd( state->linearVelocity, sim->invMass, linearImpulse ); + state->angularVelocity = b3Add( state->angularVelocity, b3MulMV( sim->invInertiaWorld, angularImpulse ) ); +} + b3Vec3 b3Body_GetAngularVelocity( b3BodyId bodyId ) { b3World* world = b3GetWorld( bodyId.world0 ); diff --git a/src/physics_world.c b/src/physics_world.c index 7f5630b..fcb068a 100644 --- a/src/physics_world.c +++ b/src/physics_world.c @@ -3333,6 +3333,14 @@ void b3World_SetGravity( b3WorldId worldId, b3Vec3 gravity ) world->gravity = gravity; } +void b3World_SetSubStepCallback( b3WorldId worldId, b3SubStepFcn* fcn, void* context ) +{ + b3World* world = b3GetWorldFromId( worldId ); + B3_ASSERT( world->locked == false ); + world->subStepFcn = fcn; + world->subStepContext = context; +} + b3Vec3 b3World_GetGravity( b3WorldId worldId ) { b3World* world = b3GetWorldFromId( worldId ); diff --git a/src/physics_world.h b/src/physics_world.h index 8387e2a..1ab9309 100644 --- a/src/physics_world.h +++ b/src/physics_world.h @@ -275,6 +275,11 @@ typedef struct b3World // not a real mutex. This should have minimal performance impact. bool locked; + // GameCult fork: the substep callback, and whether it is running + b3SubStepFcn* subStepFcn; + void* subStepContext; + bool inSubStep; + // Non-NULL while a recording session is active. Set by b3World_StartRecording, // cleared by b3World_StopRecording. Hooks in mutators check this before writing. struct b3Recording* recording; @@ -285,6 +290,11 @@ typedef struct b3World bool inUse; } b3World; +static inline b3WorldId b3MakeWorldIdFromWorld( b3World* world ) +{ + return (b3WorldId){ (uint16_t)( world->worldId + 1 ), world->generation }; +} + b3World* b3GetUnlockedWorldFromId( b3WorldId id ); b3World* b3GetWorldFromId( b3WorldId id ); diff --git a/src/solver.c b/src/solver.c index 5e208b6..562d7c5 100644 --- a/src/solver.c +++ b/src/solver.c @@ -1269,6 +1269,15 @@ static void b3SolverTask( void* taskContext ) profile->solveImpulses += b3GetMillisecondsAndReset( &ticks ); + // GameCult fork: the external solver's substep, on this thread while the workers wait + b3World* world = context->world; + if ( world->subStepFcn != NULL ) + { + world->inSubStep = true; + world->subStepFcn( b3MakeWorldIdFromWorld( world ), subStepIndex, context->h, world->subStepContext ); + world->inSubStep = false; + } + // Integrate positions B3_ASSERT( stages[iterationStageIndex].type == b3_stageIntegratePositions ); syncBits = ( bodySyncIndex << 16 ) | iterationStageIndex; @@ -1431,6 +1440,16 @@ void b3Solve( b3World* world, b3StepContext* stepContext ) int awakeBodyCount = awakeSet->bodySims.count; if ( awakeBodyCount == 0 ) { + // GameCult fork: the external solver still runs its substeps + if ( world->subStepFcn != NULL ) + { + world->inSubStep = true; + for ( int i = 0; i < stepContext->subStepCount; ++i ) + { + world->subStepFcn( b3MakeWorldIdFromWorld( world ), i, stepContext->h, world->subStepContext ); + } + world->inSubStep = false; + } b3ValidateNoEnlarged( &world->broadPhase ); return; }