Body.inl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. RMat44 Body::GetWorldTransform() const
  6. {
  7. JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read));
  8. return RMat44::sRotationTranslation(mRotation, mPosition).PreTranslated(-mShape->GetCenterOfMass());
  9. }
  10. RMat44 Body::GetCenterOfMassTransform() const
  11. {
  12. JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read));
  13. return RMat44::sRotationTranslation(mRotation, mPosition);
  14. }
  15. RMat44 Body::GetInverseCenterOfMassTransform() const
  16. {
  17. JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read));
  18. return RMat44::sInverseRotationTranslation(mRotation, mPosition);
  19. }
  20. inline bool Body::sFindCollidingPairsCanCollide(const Body &inBody1, const Body &inBody2)
  21. {
  22. // One of these conditions must be true
  23. // - One of the bodies must be dynamic to collide
  24. // - A kinematic object can collide with a sensor
  25. if ((!inBody1.IsDynamic() && !inBody2.IsDynamic())
  26. && !(inBody1.IsKinematic() && inBody2.IsSensor())
  27. && !(inBody2.IsKinematic() && inBody1.IsSensor()))
  28. return false;
  29. // Check that body 1 is active
  30. uint32 body1_index_in_active_bodies = inBody1.GetIndexInActiveBodiesInternal();
  31. JPH_ASSERT(!inBody1.IsStatic() && body1_index_in_active_bodies != Body::cInactiveIndex, "This function assumes that Body 1 is active");
  32. // If the pair A, B collides we need to ensure that the pair B, A does not collide or else we will handle the collision twice.
  33. // If A is the same body as B we don't want to collide (1)
  34. // If A is dynamic and B is static we should collide (2)
  35. // If A is dynamic / kinematic and B is dynamic / kinematic we should only collide if (kinematic vs kinematic is ruled out by the if above)
  36. // - A is active and B is not yet active (3)
  37. // - A is active and B will become active during this simulation step (4)
  38. // - A is active and B is active, we require a condition that makes A, B collide and B, A not (5)
  39. //
  40. // In order to implement this we use the index in the active body list and make use of the fact that
  41. // a body not in the active list has Body.Index = 0xffffffff which is the highest possible value for an uint32.
  42. //
  43. // Because we know that A is active we know that A.Index != 0xffffffff:
  44. // (1) Because A.Index != 0xffffffff, if A.Index = B.Index then A = B, so to collide A.Index != B.Index
  45. // (2) A.Index != 0xffffffff, B.Index = 0xffffffff (because it's static and cannot be in the active list), so to collide A.Index != B.Index
  46. // (3) A.Index != 0xffffffff, B.Index = 0xffffffff (because it's not yet active), so to collide A.Index != B.Index
  47. // (4) A.Index != 0xffffffff, B.Index = 0xffffffff currently. But it can activate during the Broad/NarrowPhase step at which point it
  48. // will be added to the end of the active list which will make B.Index > A.Index (this holds only true when we don't deactivate
  49. // bodies during the Broad/NarrowPhase step), so to collide A.Index < B.Index.
  50. // (5) As tie breaker we can use the same condition A.Index < B.Index to collide, this means that if A, B collides then B, A won't
  51. static_assert(Body::cInactiveIndex == 0xffffffff, "The algorithm below uses this value");
  52. if (body1_index_in_active_bodies >= inBody2.GetIndexInActiveBodiesInternal())
  53. return false;
  54. JPH_ASSERT(inBody1.GetID() != inBody2.GetID(), "Read the comment above, A and B are the same body which should not be possible!");
  55. // Bodies in the same group don't collide
  56. if (!inBody1.GetCollisionGroup().CanCollide(inBody2.GetCollisionGroup()))
  57. return false;
  58. return true;
  59. }
  60. void Body::AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
  61. {
  62. JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::ReadWrite));
  63. // This used to use the equation: d/dt R(t) = 1/2 * w(t) * R(t) so that R(t + dt) = R(t) + 1/2 * w(t) * R(t) * dt
  64. // See: Appendix B of An Introduction to Physically Based Modeling: Rigid Body Simulation II-Nonpenetration Constraints
  65. // URL: https://www.cs.cmu.edu/~baraff/sigcourse/notesd2.pdf
  66. // But this is a first order approximation and does not work well for kinematic ragdolls that are driven to a new
  67. // pose if the poses differ enough. So now we split w(t) * dt into an axis and angle part and create a quaternion with it.
  68. // Note that the resulting quaternion is normalized since otherwise numerical drift will eventually make the rotation non-normalized.
  69. float len = inAngularVelocityTimesDeltaTime.Length();
  70. if (len > 1.0e-6f)
  71. {
  72. mRotation = (Quat::sRotation(inAngularVelocityTimesDeltaTime / len, len) * mRotation).Normalized();
  73. JPH_ASSERT(!mRotation.IsNaN());
  74. }
  75. }
  76. void Body::SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
  77. {
  78. JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::ReadWrite));
  79. // See comment at Body::AddRotationStep
  80. float len = inAngularVelocityTimesDeltaTime.Length();
  81. if (len > 1.0e-6f)
  82. {
  83. mRotation = (Quat::sRotation(inAngularVelocityTimesDeltaTime / len, -len) * mRotation).Normalized();
  84. JPH_ASSERT(!mRotation.IsNaN());
  85. }
  86. }
  87. Vec3 Body::GetWorldSpaceSurfaceNormal(const SubShapeID &inSubShapeID, RVec3Arg inPosition) const
  88. {
  89. RMat44 inv_com = GetInverseCenterOfMassTransform();
  90. return inv_com.Multiply3x3Transposed(mShape->GetSurfaceNormal(inSubShapeID, Vec3(inv_com * inPosition))).Normalized();
  91. }
  92. Mat44 Body::GetInverseInertia() const
  93. {
  94. JPH_ASSERT(IsDynamic());
  95. return GetMotionProperties()->GetInverseInertiaForRotation(Mat44::sRotation(mRotation));
  96. }
  97. void Body::AddForce(Vec3Arg inForce, RVec3Arg inPosition)
  98. {
  99. AddForce(inForce);
  100. AddTorque(Vec3(inPosition - mPosition).Cross(inForce));
  101. }
  102. void Body::AddImpulse(Vec3Arg inImpulse)
  103. {
  104. JPH_ASSERT(IsDynamic());
  105. SetLinearVelocityClamped(mMotionProperties->GetLinearVelocity() + inImpulse * mMotionProperties->GetInverseMass());
  106. }
  107. void Body::AddImpulse(Vec3Arg inImpulse, RVec3Arg inPosition)
  108. {
  109. JPH_ASSERT(IsDynamic());
  110. SetLinearVelocityClamped(mMotionProperties->GetLinearVelocity() + inImpulse * mMotionProperties->GetInverseMass());
  111. SetAngularVelocityClamped(mMotionProperties->GetAngularVelocity() + mMotionProperties->MultiplyWorldSpaceInverseInertiaByVector(mRotation, Vec3(inPosition - mPosition).Cross(inImpulse)));
  112. }
  113. void Body::AddAngularImpulse(Vec3Arg inAngularImpulse)
  114. {
  115. JPH_ASSERT(IsDynamic());
  116. SetAngularVelocityClamped(mMotionProperties->GetAngularVelocity() + mMotionProperties->MultiplyWorldSpaceInverseInertiaByVector(mRotation, inAngularImpulse));
  117. }
  118. void Body::GetSleepTestPoints(RVec3 *outPoints) const
  119. {
  120. JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read));
  121. // Center of mass is the first position
  122. outPoints[0] = mPosition;
  123. // The second and third position are on the largest axis of the bounding box
  124. Vec3 extent = mShape->GetLocalBounds().GetExtent();
  125. int lowest_component = extent.GetLowestComponentIndex();
  126. Mat44 rotation = Mat44::sRotation(mRotation);
  127. switch (lowest_component)
  128. {
  129. case 0:
  130. outPoints[1] = mPosition + extent.GetY() * rotation.GetColumn3(1);
  131. outPoints[2] = mPosition + extent.GetZ() * rotation.GetColumn3(2);
  132. break;
  133. case 1:
  134. outPoints[1] = mPosition + extent.GetX() * rotation.GetColumn3(0);
  135. outPoints[2] = mPosition + extent.GetZ() * rotation.GetColumn3(2);
  136. break;
  137. case 2:
  138. outPoints[1] = mPosition + extent.GetX() * rotation.GetColumn3(0);
  139. outPoints[2] = mPosition + extent.GetY() * rotation.GetColumn3(1);
  140. break;
  141. default:
  142. JPH_ASSERT(false);
  143. break;
  144. }
  145. }
  146. void Body::ResetSleepTestSpheres()
  147. {
  148. RVec3 points[3];
  149. GetSleepTestPoints(points);
  150. mMotionProperties->ResetSleepTestSpheres(points);
  151. }
  152. JPH_NAMESPACE_END