RotatedTranslatedShape.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  6. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  7. #include <Jolt/Physics/Collision/RayCast.h>
  8. #include <Jolt/Physics/Collision/ShapeCast.h>
  9. #include <Jolt/Physics/Collision/TransformedShape.h>
  10. #include <Jolt/Core/StreamIn.h>
  11. #include <Jolt/Core/StreamOut.h>
  12. #include <Jolt/ObjectStream/TypeDeclarations.h>
  13. JPH_NAMESPACE_BEGIN
  14. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(RotatedTranslatedShapeSettings)
  15. {
  16. JPH_ADD_BASE_CLASS(RotatedTranslatedShapeSettings, DecoratedShapeSettings)
  17. JPH_ADD_ATTRIBUTE(RotatedTranslatedShapeSettings, mPosition)
  18. JPH_ADD_ATTRIBUTE(RotatedTranslatedShapeSettings, mRotation)
  19. }
  20. ShapeSettings::ShapeResult RotatedTranslatedShapeSettings::Create() const
  21. {
  22. if (mCachedResult.IsEmpty())
  23. Ref<Shape> shape = new RotatedTranslatedShape(*this, mCachedResult);
  24. return mCachedResult;
  25. }
  26. RotatedTranslatedShape::RotatedTranslatedShape(const RotatedTranslatedShapeSettings &inSettings, ShapeResult &outResult) :
  27. DecoratedShape(EShapeSubType::RotatedTranslated, inSettings, outResult)
  28. {
  29. if (outResult.HasError())
  30. return;
  31. // Calculate center of mass position
  32. mCenterOfMass = inSettings.mPosition + inSettings.mRotation * mInnerShape->GetCenterOfMass();
  33. // Store rotation (position is always zero because we center around the center of mass)
  34. mRotation = inSettings.mRotation;
  35. mIsRotationIdentity = mRotation.IsClose(Quat::sIdentity());
  36. outResult.Set(this);
  37. }
  38. RotatedTranslatedShape::RotatedTranslatedShape(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape) :
  39. DecoratedShape(EShapeSubType::RotatedTranslated, inShape)
  40. {
  41. // Calculate center of mass position
  42. mCenterOfMass = inPosition + inRotation * mInnerShape->GetCenterOfMass();
  43. // Store rotation (position is always zero because we center around the center of mass)
  44. mRotation = inRotation;
  45. mIsRotationIdentity = mRotation.IsClose(Quat::sIdentity());
  46. }
  47. MassProperties RotatedTranslatedShape::GetMassProperties() const
  48. {
  49. // Rotate inertia of child into place
  50. MassProperties p = mInnerShape->GetMassProperties();
  51. p.Rotate(Mat44::sRotation(mRotation));
  52. return p;
  53. }
  54. AABox RotatedTranslatedShape::GetLocalBounds() const
  55. {
  56. return mInnerShape->GetLocalBounds().Transformed(Mat44::sRotation(mRotation));
  57. }
  58. AABox RotatedTranslatedShape::GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
  59. {
  60. Mat44 transform = inCenterOfMassTransform * Mat44::sRotation(mRotation);
  61. return mInnerShape->GetWorldSpaceBounds(transform, TransformScale(inScale));
  62. }
  63. TransformedShape RotatedTranslatedShape::GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const
  64. {
  65. // We don't use any bits in the sub shape ID
  66. outRemainder = inSubShapeID;
  67. TransformedShape ts(RVec3(inPositionCOM), inRotation * mRotation, mInnerShape, BodyID());
  68. ts.SetShapeScale(TransformScale(inScale));
  69. return ts;
  70. }
  71. Vec3 RotatedTranslatedShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const
  72. {
  73. // Transform surface position to local space and pass call on
  74. Mat44 transform = Mat44::sRotation(mRotation.Conjugated());
  75. Vec3 normal = mInnerShape->GetSurfaceNormal(inSubShapeID, transform * inLocalSurfacePosition);
  76. // Transform normal to this shape's space
  77. return transform.Multiply3x3Transposed(normal);
  78. }
  79. void RotatedTranslatedShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
  80. {
  81. Mat44 transform = Mat44::sRotation(mRotation);
  82. mInnerShape->GetSupportingFace(inSubShapeID, transform.Multiply3x3Transposed(inDirection), TransformScale(inScale), inCenterOfMassTransform * transform, outVertices);
  83. }
  84. void RotatedTranslatedShape::GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const
  85. {
  86. // Get center of mass transform of child
  87. Mat44 transform = inCenterOfMassTransform * Mat44::sRotation(mRotation);
  88. // Recurse to child
  89. mInnerShape->GetSubmergedVolume(transform, TransformScale(inScale), inSurface, outTotalVolume, outSubmergedVolume, outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, inBaseOffset));
  90. }
  91. #ifdef JPH_DEBUG_RENDERER
  92. void RotatedTranslatedShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
  93. {
  94. mInnerShape->Draw(inRenderer, inCenterOfMassTransform * Mat44::sRotation(mRotation), TransformScale(inScale), inColor, inUseMaterialColors, inDrawWireframe);
  95. }
  96. void RotatedTranslatedShape::DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const
  97. {
  98. mInnerShape->DrawGetSupportFunction(inRenderer, inCenterOfMassTransform * Mat44::sRotation(mRotation), TransformScale(inScale), inColor, inDrawSupportDirection);
  99. }
  100. void RotatedTranslatedShape::DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
  101. {
  102. mInnerShape->DrawGetSupportingFace(inRenderer, inCenterOfMassTransform * Mat44::sRotation(mRotation), TransformScale(inScale));
  103. }
  104. #endif // JPH_DEBUG_RENDERER
  105. bool RotatedTranslatedShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  106. {
  107. // Transform the ray
  108. Mat44 transform = Mat44::sRotation(mRotation.Conjugated());
  109. RayCast ray = inRay.Transformed(transform);
  110. return mInnerShape->CastRay(ray, inSubShapeIDCreator, ioHit);
  111. }
  112. void RotatedTranslatedShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  113. {
  114. // Test shape filter
  115. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  116. return;
  117. // Transform the ray
  118. Mat44 transform = Mat44::sRotation(mRotation.Conjugated());
  119. RayCast ray = inRay.Transformed(transform);
  120. return mInnerShape->CastRay(ray, inRayCastSettings, inSubShapeIDCreator, ioCollector, inShapeFilter);
  121. }
  122. void RotatedTranslatedShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  123. {
  124. // Test shape filter
  125. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  126. return;
  127. // Transform the point
  128. Mat44 transform = Mat44::sRotation(mRotation.Conjugated());
  129. mInnerShape->CollidePoint(transform * inPoint, inSubShapeIDCreator, ioCollector, inShapeFilter);
  130. }
  131. void RotatedTranslatedShape::CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  132. {
  133. // Test shape filter
  134. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  135. return;
  136. mInnerShape->CollectTransformedShapes(inBox, inPositionCOM, inRotation * mRotation, TransformScale(inScale), inSubShapeIDCreator, ioCollector, inShapeFilter);
  137. }
  138. void RotatedTranslatedShape::TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const
  139. {
  140. mInnerShape->TransformShape(inCenterOfMassTransform * Mat44::sRotation(mRotation), ioCollector);
  141. }
  142. void RotatedTranslatedShape::sCollideRotatedTranslatedVsShape(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter)
  143. {
  144. JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::RotatedTranslated);
  145. const RotatedTranslatedShape *shape1 = static_cast<const RotatedTranslatedShape *>(inShape1);
  146. // Get world transform of 1
  147. Mat44 transform1 = inCenterOfMassTransform1 * Mat44::sRotation(shape1->mRotation);
  148. CollisionDispatch::sCollideShapeVsShape(shape1->mInnerShape, inShape2, shape1->TransformScale(inScale1), inScale2, transform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);
  149. }
  150. void RotatedTranslatedShape::sCollideShapeVsRotatedTranslated(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter)
  151. {
  152. JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::RotatedTranslated);
  153. const RotatedTranslatedShape *shape2 = static_cast<const RotatedTranslatedShape *>(inShape2);
  154. // Get world transform of 2
  155. Mat44 transform2 = inCenterOfMassTransform2 * Mat44::sRotation(shape2->mRotation);
  156. CollisionDispatch::sCollideShapeVsShape(inShape1, shape2->mInnerShape, inScale1, shape2->TransformScale(inScale2), inCenterOfMassTransform1, transform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);
  157. }
  158. void RotatedTranslatedShape::sCastRotatedTranslatedVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  159. {
  160. // Fetch rotated translated shape from cast shape
  161. JPH_ASSERT(inShapeCast.mShape->GetSubType() == EShapeSubType::RotatedTranslated);
  162. const RotatedTranslatedShape *shape1 = static_cast<const RotatedTranslatedShape *>(inShapeCast.mShape);
  163. // Transform the shape cast and update the shape
  164. Mat44 transform = inShapeCast.mCenterOfMassStart * Mat44::sRotation(shape1->mRotation);
  165. Vec3 scale = shape1->TransformScale(inShapeCast.mScale);
  166. ShapeCast shape_cast(shape1->mInnerShape, scale, transform, inShapeCast.mDirection);
  167. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  168. }
  169. void RotatedTranslatedShape::sCastShapeVsRotatedTranslated(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  170. {
  171. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::RotatedTranslated);
  172. const RotatedTranslatedShape *shape = static_cast<const RotatedTranslatedShape *>(inShape);
  173. // Determine the local transform
  174. Mat44 local_transform = Mat44::sRotation(shape->mRotation);
  175. // Transform the shape cast
  176. ShapeCast shape_cast = inShapeCast.PostTransformed(local_transform.Transposed3x3());
  177. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, inShapeCastSettings, shape->mInnerShape, shape->TransformScale(inScale), inShapeFilter, inCenterOfMassTransform2 * local_transform, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  178. }
  179. void RotatedTranslatedShape::SaveBinaryState(StreamOut &inStream) const
  180. {
  181. DecoratedShape::SaveBinaryState(inStream);
  182. inStream.Write(mCenterOfMass);
  183. inStream.Write(mRotation);
  184. }
  185. void RotatedTranslatedShape::RestoreBinaryState(StreamIn &inStream)
  186. {
  187. DecoratedShape::RestoreBinaryState(inStream);
  188. inStream.Read(mCenterOfMass);
  189. inStream.Read(mRotation);
  190. mIsRotationIdentity = mRotation.IsClose(Quat::sIdentity());
  191. }
  192. bool RotatedTranslatedShape::IsValidScale(Vec3Arg inScale) const
  193. {
  194. if (!DecoratedShape::IsValidScale(inScale))
  195. return false;
  196. if (mIsRotationIdentity || ScaleHelpers::IsUniformScale(inScale))
  197. return mInnerShape->IsValidScale(inScale);
  198. if (!ScaleHelpers::CanScaleBeRotated(mRotation, inScale))
  199. return false;
  200. return mInnerShape->IsValidScale(ScaleHelpers::RotateScale(mRotation, inScale));
  201. }
  202. void RotatedTranslatedShape::sRegister()
  203. {
  204. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::RotatedTranslated);
  205. f.mConstruct = []() -> Shape * { return new RotatedTranslatedShape; };
  206. f.mColor = Color::sBlue;
  207. for (EShapeSubType s : sAllSubShapeTypes)
  208. {
  209. CollisionDispatch::sRegisterCollideShape(EShapeSubType::RotatedTranslated, s, sCollideRotatedTranslatedVsShape);
  210. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::RotatedTranslated, sCollideShapeVsRotatedTranslated);
  211. CollisionDispatch::sRegisterCastShape(EShapeSubType::RotatedTranslated, s, sCastRotatedTranslatedVsShape);
  212. CollisionDispatch::sRegisterCastShape(s, EShapeSubType::RotatedTranslated, sCastShapeVsRotatedTranslated);
  213. }
  214. }
  215. JPH_NAMESPACE_END