@@ -53,16 +53,17 @@ class AxisConstraintPart
5353 //
5454 // Euler velocity integration:
5555 // v' = v + M^-1 P
56+ Vec3 impulse = inLambda * inWorldSpaceAxis;
5657 if (ioBody1.IsDynamic ())
5758 {
5859 MotionProperties *mp1 = ioBody1.GetMotionPropertiesUnchecked ();
59- mp1->SubLinearVelocityStep ((inLambda * mp1->GetInverseMass ()) * inWorldSpaceAxis );
60+ mp1->SubLinearVelocityStep (mp1->GetInverseMass () * impulse );
6061 mp1->SubAngularVelocityStep (inLambda * Vec3::sLoadFloat3Unsafe (mInvI1_R1PlusUxAxis ));
6162 }
6263 if (ioBody2.IsDynamic ())
6364 {
6465 MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked ();
65- mp2->AddLinearVelocityStep ((inLambda * mp2->GetInverseMass ()) * inWorldSpaceAxis );
66+ mp2->AddLinearVelocityStep (mp2->GetInverseMass () * impulse );
6667 mp2->AddAngularVelocityStep (inLambda * Vec3::sLoadFloat3Unsafe (mInvI2_R2xAxis ));
6768 }
6869 return true ;
@@ -271,29 +272,32 @@ class AxisConstraintPart
271272 // / @param inMaxLambda Maximum value of constraint impulse to apply (N s)
272273 inline bool SolveVelocityConstraint (Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
273274 {
274- const MotionProperties *mp1 = ioBody1.GetMotionPropertiesUnchecked ();
275- const MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked ();
276-
277275 // Calculate jacobian multiplied by linear velocity
278- float jv ;
276+ Vec3 acc ;
279277 if (!ioBody1.IsStatic ())
280278 {
279+ const MotionProperties *mp1 = ioBody1.GetMotionPropertiesUnchecked ();
280+
281281 if (!ioBody2.IsStatic ())
282- jv = inWorldSpaceAxis.Dot (mp1->GetLinearVelocity () - mp2->GetLinearVelocity ());
282+ {
283+ const MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked ();
284+ acc = inWorldSpaceAxis * (mp1->GetLinearVelocity () - mp2->GetLinearVelocity ());
285+ acc -= Vec3::sLoadFloat3Unsafe (mR2xAxis ) * mp2->GetAngularVelocity ();
286+ }
283287 else
284- jv = inWorldSpaceAxis.Dot (mp1->GetLinearVelocity ());
288+ acc = inWorldSpaceAxis * mp1->GetLinearVelocity ();
289+
290+ acc += Vec3::sLoadFloat3Unsafe (mR1PlusUxAxis ) * mp1->GetAngularVelocity ();
285291 }
286292 else
287293 {
288294 JPH_ASSERT (!ioBody2.IsStatic ());
289- jv = inWorldSpaceAxis.Dot (-mp2->GetLinearVelocity ());
295+ const MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked ();
296+ acc = inWorldSpaceAxis * (-mp2->GetLinearVelocity ());
297+ acc -= Vec3::sLoadFloat3Unsafe (mR2xAxis ) * mp2->GetAngularVelocity ();
290298 }
291299
292- // Calculate jacobian multiplied by angular velocity
293- if (!ioBody1.IsStatic ())
294- jv += Vec3::sLoadFloat3Unsafe (mR1PlusUxAxis ).Dot (mp1->GetAngularVelocity ());
295- if (!ioBody2.IsStatic ())
296- jv -= Vec3::sLoadFloat3Unsafe (mR2xAxis ).Dot (mp2->GetAngularVelocity ());
300+ float jv = acc.ReduceSum ();
297301
298302 // Lagrange multiplier is:
299303 //
@@ -345,14 +349,15 @@ class AxisConstraintPart
345349 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
346350 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
347351 // integrate + a position integrate and then discard the velocity change.
352+ Vec3 impulse = lambda * inWorldSpaceAxis;
348353 if (ioBody1.IsDynamic ())
349354 {
350- ioBody1.SubPositionStep ((lambda * ioBody1.GetMotionProperties ()->GetInverseMass ()) * inWorldSpaceAxis );
355+ ioBody1.SubPositionStep (ioBody1.GetMotionPropertiesUnchecked ()->GetInverseMass () * impulse );
351356 ioBody1.SubRotationStep (lambda * Vec3::sLoadFloat3Unsafe (mInvI1_R1PlusUxAxis ));
352357 }
353358 if (ioBody2.IsDynamic ())
354359 {
355- ioBody2.AddPositionStep ((lambda * ioBody2.GetMotionProperties ()->GetInverseMass ()) * inWorldSpaceAxis );
360+ ioBody2.AddPositionStep (ioBody2.GetMotionPropertiesUnchecked ()->GetInverseMass () * impulse );
356361 ioBody2.AddRotationStep (lambda * Vec3::sLoadFloat3Unsafe (mInvI2_R2xAxis ));
357362 }
358363 return true ;
0 commit comments