Body.inl 7.9 KB

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