Contact friction hook also runs on recycled contacts
A recycled contact skips its update and with it the friction mix, so friction never followed the application's patches under a body at rest. With a hook installed the recycle branch mixes again and runs the hook (anchors are centre-relative there, in the cached frame); with no hook nothing changes. The hook body moves to b3ApplyContactFrictionHook. ContactFrictionRecycledTest fails without the recycle call. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2a43faeaec
commit
ebe346a925
4 changed files with 113 additions and 20 deletions
|
|
@ -607,6 +607,53 @@ static bool b3ComputeConvexManifold( b3World* world, int workerIndex, b3Contact*
|
|||
return true;
|
||||
}
|
||||
|
||||
// The per-contact friction hook, when one is installed: the mean manifold point in each body's frame (the anchors' frame, rotated back
|
||||
// by q and moved by the offset: zero for origin-relative anchors, the local centre for centre-relative ones), the bodies' user data and
|
||||
// the mixed friction. Convex contacts only.
|
||||
void b3ApplyContactFrictionHook( b3World* world, b3Contact* contact, const b3Shape* shapeA, const b3Shape* shapeB, b3Quat qA, b3Quat qB,
|
||||
b3Vec3 offsetA, b3Vec3 offsetB, float mixed )
|
||||
{
|
||||
if ( world->contactFrictionFcn == NULL )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
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 = b3Add( b3InvRotateVector( qA, b3MulSV( inv, sumA ) ), offsetA );
|
||||
b3Vec3 localB = b3Add( b3InvRotateVector( qB, b3MulSV( inv, sumB ) ), offsetB );
|
||||
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, mixed, world->contactFrictionContext );
|
||||
}
|
||||
|
||||
// A recycled contact skips its update, and with it the friction mix: with a hook installed, mix again and run the hook so the friction follows
|
||||
// the application's patches under a body at rest. Anchors are centre-relative here, in the frame the contact was cached in.
|
||||
void b3RefreshRecycledFriction( b3World* world, b3Contact* contact, const b3Shape* shapeA, const b3Shape* shapeB, b3Vec3 localCenterA,
|
||||
b3Vec3 localCenterB )
|
||||
{
|
||||
if ( world->contactFrictionFcn == NULL || ( contact->flags & b3_simMeshContact ) || contact->manifoldCount != 1 ||
|
||||
contact->manifolds[0].pointCount == 0 || shapeA->type == b3_compoundShape || shapeB->type == b3_compoundShape )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const b3SurfaceMaterial* materialA = b3GetShapeMaterials( shapeA );
|
||||
const b3SurfaceMaterial* materialB = b3GetShapeMaterials( shapeB );
|
||||
float mixed =
|
||||
world->frictionCallback( materialA->friction, materialA->userMaterialId, materialB->friction, materialB->userMaterialId );
|
||||
b3ApplyContactFrictionHook( world, contact, shapeA, shapeB, contact->cachedRotationA, contact->cachedRotationB, localCenterA,
|
||||
localCenterB, mixed );
|
||||
}
|
||||
|
||||
static bool b3UpdateConvexContact( b3World* world, int workerIndex, b3Contact* contact, b3Shape* shapeA, b3WorldTransform xfA,
|
||||
b3Shape* shapeB, b3WorldTransform xfB, bool flip, b3Arena arena )
|
||||
{
|
||||
|
|
@ -641,26 +688,7 @@ static bool b3UpdateConvexContact( b3World* world, int workerIndex, b3Contact* c
|
|||
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 );
|
||||
}
|
||||
b3ApplyContactFrictionHook( world, contact, shapeA, shapeB, xfA.q, xfB.q, b3Vec3_zero, b3Vec3_zero, contact->friction );
|
||||
contact->restitution = world->restitutionCallback( materialA->restitution, materialA->userMaterialId, materialB->restitution,
|
||||
materialB->userMaterialId );
|
||||
|
||||
|
|
|
|||
|
|
@ -173,6 +173,11 @@ void b3InitializeContactRegisters( void );
|
|||
void b3CreateContact( b3World* world, b3Shape* shapeA, b3Shape* shapeB, int childIndex );
|
||||
void b3DestroyContact( b3World* world, b3Contact* contact, bool wakeBodies );
|
||||
|
||||
void b3ApplyContactFrictionHook( b3World* world, b3Contact* contact, const b3Shape* shapeA, const b3Shape* shapeB, b3Quat qA, b3Quat qB,
|
||||
b3Vec3 offsetA, b3Vec3 offsetB, float mixed );
|
||||
void b3RefreshRecycledFriction( b3World* world, b3Contact* contact, const b3Shape* shapeA, const b3Shape* shapeB, b3Vec3 localCenterA,
|
||||
b3Vec3 localCenterB );
|
||||
|
||||
bool b3UpdateContact( b3World* world, int workerIndex, b3Contact* contact, b3Shape* shapeA, b3Vec3 localCenterA, b3WorldTransform xfA,
|
||||
b3Shape* shapeB, b3Vec3 localCenterB, b3WorldTransform xfB, bool isFast, b3Arena arena );
|
||||
|
||||
|
|
|
|||
|
|
@ -721,6 +721,7 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
|
|||
|
||||
// Contact is recycled. This also skips updating other aspects of the contact
|
||||
// such as material parameters.
|
||||
b3RefreshRecycledFriction( world, contact, shapeA, shapeB, bodySimA->localCenter, bodySimB->localCenter );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1049,6 +1049,7 @@ static float SlideSpeed( bool hooked )
|
|||
return speed;
|
||||
}
|
||||
|
||||
// Run by name: `test.exe WorldTest` (the unfiltered run stops at CompoundTest, which fails at fb719c4 as well: CompoundMaterialDedup).
|
||||
int ContactFrictionHookTest( void )
|
||||
{
|
||||
s_hookCalls = 0;
|
||||
|
|
@ -1064,6 +1065,63 @@ int ContactFrictionHookTest( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
static float SwitchHook( void* userDataA, void* userDataB, b3Vec3 localA, b3Vec3 localB, float mixed, void* context )
|
||||
{
|
||||
return *(bool*)context ? 0.0f : mixed;
|
||||
}
|
||||
|
||||
// A box rests for 30 ticks under no push, the hook's answer changes, then a push of 30 N (below mu m g = 48 N) runs for 30 ticks. The
|
||||
// contact is recycled all the while the box rests, so the answer reaches it only if the hook runs on recycled contacts too.
|
||||
static float PushedSpeed( bool slickWhileResting, bool slickWhilePushed )
|
||||
{
|
||||
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 };
|
||||
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 };
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull box = b3MakeCubeHull( 1.0f );
|
||||
shapeDef.density = 1.0f;
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
bool slick = slickWhileResting;
|
||||
b3World_SetContactFrictionCallback( worldId, SwitchHook, &slick );
|
||||
for ( int i = 0; i < 30; ++i )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
}
|
||||
|
||||
slick = slickWhilePushed;
|
||||
for ( int i = 0; i < 30; ++i )
|
||||
{
|
||||
b3Body_ApplyForceToCenter( bodyId, (b3Vec3){ 30.0f, 0.0f, 0.0f }, true );
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
}
|
||||
|
||||
float speed = b3Body_GetLinearVelocity( bodyId ).x;
|
||||
b3DestroyWorld( worldId );
|
||||
return speed;
|
||||
}
|
||||
|
||||
int ContactFrictionRecycledTest( void )
|
||||
{
|
||||
// Friction lowered under a box that has rested: it slides. Friction restored under a box that rested on the lowered value: it holds.
|
||||
ENSURE( PushedSpeed( false, true ) > 1.0f );
|
||||
ENSURE( PushedSpeed( true, false ) < 0.05f );
|
||||
ENSURE( PushedSpeed( false, false ) < 0.05f );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WorldTest( void )
|
||||
{
|
||||
RUN_SUBTEST( HelloWorld );
|
||||
|
|
@ -1086,6 +1144,7 @@ int WorldTest( void )
|
|||
RUN_SUBTEST( TestSetWorkerCount );
|
||||
RUN_SUBTEST( TestHullDatabase );
|
||||
RUN_SUBTEST( ContactFrictionHookTest );
|
||||
RUN_SUBTEST( ContactFrictionRecycledTest );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue