Contact friction hook: a per-contact friction override after the mix
b3ContactFrictionFcn is called from b3UpdateConvexContact right after the friction mix with the mean manifold point in each body's frame, both bodies' user data and the mixed value. Convex contacts only. Test: ContactFrictionHookTest. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
fb719c49c0
commit
2a43faeaec
6 changed files with 117 additions and 0 deletions
|
|
@ -239,6 +239,10 @@ B3_API void* b3World_GetUserData( b3WorldId worldId );
|
|||
/// Set the friction callback. Passing NULL resets to default.
|
||||
B3_API void b3World_SetFrictionCallback( b3WorldId worldId, b3FrictionCallback* callback );
|
||||
|
||||
/// Set the per-contact friction hook (GameCult fork). Passing NULL removes it. The hook is called on worker threads, so the
|
||||
/// context must be read-only for the duration of a step.
|
||||
B3_API void b3World_SetContactFrictionCallback( b3WorldId worldId, b3ContactFrictionFcn* fcn, void* context );
|
||||
|
||||
/// Set the restitution callback. Passing NULL resets to default.
|
||||
B3_API void b3World_SetRestitutionCallback( b3WorldId worldId, b3RestitutionCallback* callback );
|
||||
|
||||
|
|
|
|||
|
|
@ -53,6 +53,14 @@ typedef void b3DestroyDebugShapeCallback( void* userShape, void* userContext );
|
|||
/// @ingroup world
|
||||
typedef float b3FrictionCallback( float frictionA, uint64_t userMaterialIdA, float frictionB, uint64_t userMaterialIdB );
|
||||
|
||||
/// Optional per-contact friction override (GameCult fork). Called from b3UpdateConvexContact right after the friction mix,
|
||||
/// from a worker thread, with the mean of the manifold points expressed in each body's frame, the bodies' user data, and the
|
||||
/// mixed friction. Return the friction to use for this contact this update. Convex contacts only; mesh contacts are not hooked.
|
||||
/// @warning Must be pure and read-only: no Box3D calls, no writes to application state.
|
||||
/// @ingroup world
|
||||
typedef float b3ContactFrictionFcn( void* bodyUserDataA, void* bodyUserDataB, b3Vec3 localPointA, b3Vec3 localPointB, float mixed,
|
||||
void* context );
|
||||
|
||||
/// Optional restitution mixing callback. This intentionally provides no context objects because this is called
|
||||
/// from a worker thread.
|
||||
/// @warning This function should not attempt to modify Box3D state or user application state.
|
||||
|
|
|
|||
|
|
@ -640,6 +640,27 @@ static bool b3UpdateConvexContact( b3World* world, int workerIndex, b3Contact* c
|
|||
// Keep these updated in case the values on the shapes are modified
|
||||
contact->friction =
|
||||
world->frictionCallback( materialA->friction, materialA->userMaterialId, materialB->friction, materialB->userMaterialId );
|
||||
|
||||
if ( world->contactFrictionFcn != NULL )
|
||||
{
|
||||
// Mean manifold point in each body's frame (anchors are relative to the body origin in world axes here).
|
||||
b3Manifold* manifold = contact->manifolds + 0;
|
||||
int pointCount = manifold->pointCount;
|
||||
b3Vec3 sumA = b3Vec3_zero;
|
||||
b3Vec3 sumB = b3Vec3_zero;
|
||||
for ( int i = 0; i < pointCount; ++i )
|
||||
{
|
||||
sumA = b3Add( sumA, manifold->points[i].anchorA );
|
||||
sumB = b3Add( sumB, manifold->points[i].anchorB );
|
||||
}
|
||||
float inv = 1.0f / (float)pointCount;
|
||||
b3Vec3 localA = b3InvRotateVector( xfA.q, b3MulSV( inv, sumA ) );
|
||||
b3Vec3 localB = b3InvRotateVector( xfB.q, b3MulSV( inv, sumB ) );
|
||||
void* userDataA = b3Array_Get( world->bodies, shapeA->bodyId )->userData;
|
||||
void* userDataB = b3Array_Get( world->bodies, shapeB->bodyId )->userData;
|
||||
contact->friction = world->contactFrictionFcn( userDataA, userDataB, localA, localB, contact->friction,
|
||||
world->contactFrictionContext );
|
||||
}
|
||||
contact->restitution = world->restitutionCallback( materialA->restitution, materialA->userMaterialId, materialB->restitution,
|
||||
materialB->userMaterialId );
|
||||
|
||||
|
|
|
|||
|
|
@ -2214,6 +2214,18 @@ void b3World_SetFrictionCallback( b3WorldId worldId, b3FrictionCallback* callbac
|
|||
world->frictionCallback = callback != NULL ? callback : b3DefaultFrictionCallback;
|
||||
}
|
||||
|
||||
void b3World_SetContactFrictionCallback( b3WorldId worldId, b3ContactFrictionFcn* fcn, void* context )
|
||||
{
|
||||
b3World* world = b3GetUnlockedWorldFromId( worldId );
|
||||
if ( world == NULL )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
world->contactFrictionFcn = fcn;
|
||||
world->contactFrictionContext = context;
|
||||
}
|
||||
|
||||
void b3World_SetRestitutionCallback( b3WorldId worldId, b3RestitutionCallback* callback )
|
||||
{
|
||||
b3World* world = b3GetUnlockedWorldFromId( worldId );
|
||||
|
|
|
|||
|
|
@ -231,6 +231,8 @@ typedef struct b3World
|
|||
float contactRecycleDistance;
|
||||
|
||||
b3FrictionCallback* frictionCallback;
|
||||
b3ContactFrictionFcn* contactFrictionFcn;
|
||||
void* contactFrictionContext;
|
||||
b3RestitutionCallback* restitutionCallback;
|
||||
|
||||
uint16_t generation;
|
||||
|
|
|
|||
|
|
@ -995,6 +995,75 @@ static int TestExplosion( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int s_hookCalls;
|
||||
static int s_hookBad;
|
||||
static float ZeroFrictionHook( void* userDataA, void* userDataB, b3Vec3 localA, b3Vec3 localB, float mixed, void* context )
|
||||
{
|
||||
// Ground is body A (user data 1), box is body B (user data 2); the box rests 1 m above its ground contact point.
|
||||
s_hookCalls += 1;
|
||||
if ( userDataA != (void*)(intptr_t)1 || userDataB != (void*)(intptr_t)2 || b3AbsFloat( localB.y + 1.0f ) > 0.1f ||
|
||||
b3AbsFloat( mixed - 0.6f ) > 1.0e-5f || context != &s_hookCalls )
|
||||
{
|
||||
s_hookBad += 1;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
static float SlideSpeed( bool hooked )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.gravity = (b3Vec3){ 0.0f, -10.0f, 0.0f };
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef groundDef = b3DefaultBodyDef();
|
||||
groundDef.position = (b3Pos){ 0.0f, -10.0f, 0.0f };
|
||||
groundDef.userData = (void*)(intptr_t)1;
|
||||
b3BodyId groundId = b3CreateBody( worldId, &groundDef );
|
||||
b3BoxHull groundBox = b3MakeBoxHull( 50.0f, 10.0f, 50.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.friction = 0.6f;
|
||||
b3CreateHullShape( groundId, &shapeDef, &groundBox.base );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = (b3Pos){ 0.0f, 1.0f, 0.0f };
|
||||
bodyDef.linearVelocity = (b3Vec3){ 5.0f, 0.0f, 0.0f };
|
||||
bodyDef.userData = (void*)(intptr_t)2;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull box = b3MakeCubeHull( 1.0f );
|
||||
shapeDef.density = 1.0f;
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
if ( hooked )
|
||||
{
|
||||
b3World_SetContactFrictionCallback( worldId, ZeroFrictionHook, &s_hookCalls );
|
||||
}
|
||||
|
||||
for ( int i = 0; i < 60; ++i )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
}
|
||||
|
||||
float speed = b3Body_GetLinearVelocity( bodyId ).x;
|
||||
b3DestroyWorld( worldId );
|
||||
return speed;
|
||||
}
|
||||
|
||||
int ContactFrictionHookTest( void )
|
||||
{
|
||||
s_hookCalls = 0;
|
||||
s_hookBad = 0;
|
||||
float dry = SlideSpeed( false );
|
||||
ENSURE( s_hookCalls == 0 );
|
||||
ENSURE( dry < 0.5f );
|
||||
|
||||
float slick = SlideSpeed( true );
|
||||
ENSURE( s_hookCalls > 0 );
|
||||
ENSURE( s_hookBad == 0 );
|
||||
ENSURE( slick > 4.9f );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WorldTest( void )
|
||||
{
|
||||
RUN_SUBTEST( HelloWorld );
|
||||
|
|
@ -1016,6 +1085,7 @@ int WorldTest( void )
|
|||
RUN_SUBTEST( EnableContactRecyclingTest );
|
||||
RUN_SUBTEST( TestSetWorkerCount );
|
||||
RUN_SUBTEST( TestHullDatabase );
|
||||
RUN_SUBTEST( ContactFrictionHookTest );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue