Body.inl 7.6 KB

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