From e421ffaf88ce4c7bcbca4787522d975deb18588b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 02:22:49 +0100 Subject: [PATCH] Contact points report the normal impulse applied over the whole step Adds b3ManifoldPoint.stepNormalImpulse: the impulse each sub-step applied (accumulated after the relax pass, plus restitution's delta), summed over the step, in the scalar and SIMD solver paths. Exact only while RELAX_ITERATIONS is 1. GameCult fork for Ymir (lodur:ruling:ymir-box3d-fork); upstream is erincatto/box3d. Co-Authored-By: Claude Opus 5.5 --- include/box3d/types.h | 3 +++ src/contact_solver.c | 10 ++++++++++ src/contact_solver.h | 1 + 3 files changed, 14 insertions(+) diff --git a/include/box3d/types.h b/include/box3d/types.h index 2c75977..1e52e4d 100644 --- a/include/box3d/types.h +++ b/include/box3d/types.h @@ -2576,6 +2576,9 @@ typedef struct b3ManifoldPoint /// to identify speculative contact points that had an interaction in the time step. float totalNormalImpulse; + /// Sum over sub-steps of the impulse each sub-step applied, plus restitution: the step impulse. + float stepNormalImpulse; + /// Relative normal velocity pre-solve. Used for hit events. If the normal impulse is /// zero then there was no hit. Negative means shapes are approaching. float normalVelocity; diff --git a/src/contact_solver.c b/src/contact_solver.c index 501f858..3b43cac 100644 --- a/src/contact_solver.c +++ b/src/contact_solver.c @@ -196,6 +196,7 @@ void b3PrepareContacts_Mesh( b3SolverBlock block, b3StepContext* context ) cp->baseSeparation = mp->separation - b3Dot( b3Sub( cp->rB, cp->rA ), normal ); cp->normalImpulse = warmStartScale * mp->normalImpulse; cp->totalNormalImpulse = 0.0f; + cp->stepNormalImpulse = 0.0f; b3Vec3 rA = cp->rA; b3Vec3 rB = cp->rB; @@ -456,6 +457,7 @@ void b3SolveContacts_Mesh( b3SolverBlock block, b3StepContext* context, bool use deltaImpulse = newImpulse - cp->normalImpulse; cp->normalImpulse = newImpulse; cp->totalNormalImpulse += newImpulse; + if ( useBias == false ) cp->stepNormalImpulse += newImpulse; totalNormalImpulse += newImpulse; totalTwistLimit += cp->leverArm * cp->normalImpulse; @@ -650,6 +652,7 @@ void b3ApplyRestitution_Mesh( b3SolverBlock block, b3StepContext* context ) impulse = newImpulse - cp->normalImpulse; cp->normalImpulse = newImpulse; cp->totalNormalImpulse += impulse; + cp->stepNormalImpulse += impulse; // apply contact impulse b3Vec3 P = b3MulSV( impulse, normal ); @@ -754,6 +757,7 @@ void b3StoreImpulses_Mesh( b3SolverBlock block, b3StepContext* context, int work b3ManifoldPoint* mp = manifold->points + pointIndex; mp->normalImpulse = cp->normalImpulse; mp->totalNormalImpulse = cp->totalNormalImpulse; + mp->stepNormalImpulse = cp->stepNormalImpulse; mp->normalVelocity = cp->relativeVelocity; if ( checkHitEvents && flagged == false && @@ -1330,6 +1334,7 @@ typedef struct b3ContactConstraintPointWide b3FloatW baseSeparations; b3FloatW normalImpulses; b3FloatW totalNormalImpulses; + b3FloatW stepNormalImpulses; b3FloatW normalMasses; b3FloatW leverArms; b3FloatW relativeVelocities; @@ -1805,6 +1810,7 @@ void b3PrepareContacts_Convex( b3SolverBlock block, b3StepContext* context ) ( (float*)&cp->normalImpulses )[lane] = warmStartScale * mp->normalImpulse; ( (float*)&cp->totalNormalImpulses )[lane] = 0.0f; +( (float*)&cp->stepNormalImpulses )[lane] = 0.0f; b3Vec3 rnA = b3Cross( rA, normal ); b3Vec3 rnB = b3Cross( rB, normal ); @@ -1892,6 +1898,7 @@ void b3PrepareContacts_Convex( b3SolverBlock block, b3StepContext* context ) ( (float*)&cp->baseSeparations )[lane] = 0.0f; ( (float*)&cp->normalImpulses )[lane] = 0.0f; ( (float*)&cp->totalNormalImpulses )[lane] = 0.0f; +( (float*)&cp->stepNormalImpulses )[lane] = 0.0f; ( (float*)&cp->normalMasses )[lane] = 0.0f; ( (float*)&cp->relativeVelocities )[lane] = 0.0f; ( (float*)&cp->leverArms )[lane] = 0.0f; @@ -2051,6 +2058,7 @@ void b3SolveContacts_Convex( b3SolverBlock block, b3StepContext* context, bool u b3FloatW deltaImpulse = b3SubW( newImpulse, cp->normalImpulses ); cp->normalImpulses = newImpulse; cp->totalNormalImpulses = b3AddW( cp->totalNormalImpulses, newImpulse ); + if ( useBias == false ) cp->stepNormalImpulses = b3AddW( cp->stepNormalImpulses, newImpulse ); totalNormalImpulse = b3AddW( totalNormalImpulse, newImpulse ); totalTwistLimit = b3AddW( totalTwistLimit, b3MulW( cp->leverArms, newImpulse ) ); @@ -2232,6 +2240,7 @@ void b3ApplyRestitution_Convex( b3SolverBlock block, b3StepContext* context ) b3FloatW deltaImpulse = b3SubW( newImpulse, cp->normalImpulses ); cp->normalImpulses = newImpulse; cp->totalNormalImpulses = b3AddW( cp->totalNormalImpulses, deltaImpulse ); + cp->stepNormalImpulses = b3AddW( cp->stepNormalImpulses, deltaImpulse ); // Apply contact impulse b3Vec3W P = b3MulSVW( deltaImpulse, c->normal ); @@ -2335,6 +2344,7 @@ void b3StoreImpulses_Convex( b3SolverBlock block, b3StepContext* context, int wo b3ManifoldPoint* mp = m->points + pointIndex; mp->normalImpulse = normalImpulse[lane]; mp->totalNormalImpulse = totalNormalImpulse[lane]; + mp->stepNormalImpulse = ((const float*)&cp->stepNormalImpulses)[lane]; mp->normalVelocity = normalVelocity[lane]; } diff --git a/src/contact_solver.h b/src/contact_solver.h index fab17ee..547edac 100644 --- a/src/contact_solver.h +++ b/src/contact_solver.h @@ -13,6 +13,7 @@ typedef struct b3ManifoldConstraintPoint float relativeVelocity; float normalImpulse; float totalNormalImpulse; + float stepNormalImpulse; float normalMass; float leverArm; } b3ManifoldConstraintPoint;