RotatedTranslatedShape.cpp 13 KB

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