RotatedTranslatedShape.cpp 11 KB

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