ScaledShape.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/ScaledShape.h>
  6. #include <Jolt/Physics/Collision/Shape/ScaleHelpers.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/Physics/Collision/CollisionDispatch.h>
  11. #include <Jolt/ObjectStream/TypeDeclarations.h>
  12. #include <Jolt/Core/StreamIn.h>
  13. #include <Jolt/Core/StreamOut.h>
  14. JPH_NAMESPACE_BEGIN
  15. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(ScaledShapeSettings)
  16. {
  17. JPH_ADD_BASE_CLASS(ScaledShapeSettings, DecoratedShapeSettings)
  18. JPH_ADD_ATTRIBUTE(ScaledShapeSettings, mScale)
  19. }
  20. ShapeSettings::ShapeResult ScaledShapeSettings::Create() const
  21. {
  22. if (mCachedResult.IsEmpty())
  23. Ref<Shape> shape = new ScaledShape(*this, mCachedResult);
  24. return mCachedResult;
  25. }
  26. ScaledShape::ScaledShape(const ScaledShapeSettings &inSettings, ShapeResult &outResult) :
  27. DecoratedShape(EShapeSubType::Scaled, inSettings, outResult),
  28. mScale(inSettings.mScale)
  29. {
  30. if (outResult.HasError())
  31. return;
  32. if (ScaleHelpers::IsZeroScale(inSettings.mScale))
  33. {
  34. outResult.SetError("Can't use zero scale!");
  35. return;
  36. }
  37. outResult.Set(this);
  38. }
  39. MassProperties ScaledShape::GetMassProperties() const
  40. {
  41. MassProperties p = mInnerShape->GetMassProperties();
  42. p.Scale(mScale);
  43. return p;
  44. }
  45. AABox ScaledShape::GetLocalBounds() const
  46. {
  47. return mInnerShape->GetLocalBounds().Scaled(mScale);
  48. }
  49. AABox ScaledShape::GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
  50. {
  51. return mInnerShape->GetWorldSpaceBounds(inCenterOfMassTransform, inScale * mScale);
  52. }
  53. TransformedShape ScaledShape::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(RVec3(inPositionCOM), inRotation, mInnerShape, BodyID());
  58. ts.SetShapeScale(inScale * mScale);
  59. return ts;
  60. }
  61. Vec3 ScaledShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const
  62. {
  63. // Transform the surface point to local space and pass the query on
  64. Vec3 normal = mInnerShape->GetSurfaceNormal(inSubShapeID, inLocalSurfacePosition / mScale);
  65. // Need to transform the plane normals using inScale
  66. // Transforming a direction with matrix M is done through multiplying by (M^-1)^T
  67. // In this case M is a diagonal matrix with the scale vector, so we need to multiply our normal by 1 / scale and renormalize afterwards
  68. return (normal / mScale).Normalized();
  69. }
  70. void ScaledShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
  71. {
  72. mInnerShape->GetSupportingFace(inSubShapeID, inDirection, inScale * mScale, inCenterOfMassTransform, outVertices);
  73. }
  74. void ScaledShape::GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const
  75. {
  76. mInnerShape->GetSubmergedVolume(inCenterOfMassTransform, inScale * mScale, inSurface, outTotalVolume, outSubmergedVolume, outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, inBaseOffset));
  77. }
  78. #ifdef JPH_DEBUG_RENDERER
  79. void ScaledShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
  80. {
  81. mInnerShape->Draw(inRenderer, inCenterOfMassTransform, inScale * mScale, inColor, inUseMaterialColors, inDrawWireframe);
  82. }
  83. void ScaledShape::DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const
  84. {
  85. mInnerShape->DrawGetSupportFunction(inRenderer, inCenterOfMassTransform, inScale * mScale, inColor, inDrawSupportDirection);
  86. }
  87. void ScaledShape::DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
  88. {
  89. mInnerShape->DrawGetSupportingFace(inRenderer, inCenterOfMassTransform, inScale * mScale);
  90. }
  91. #endif // JPH_DEBUG_RENDERER
  92. bool ScaledShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  93. {
  94. Vec3 inv_scale = mScale.Reciprocal();
  95. RayCast scaled_ray { inv_scale * inRay.mOrigin, inv_scale * inRay.mDirection };
  96. return mInnerShape->CastRay(scaled_ray, inSubShapeIDCreator, ioHit);
  97. }
  98. void ScaledShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  99. {
  100. // Test shape filter
  101. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  102. return;
  103. Vec3 inv_scale = mScale.Reciprocal();
  104. RayCast scaled_ray { inv_scale * inRay.mOrigin, inv_scale * inRay.mDirection };
  105. return mInnerShape->CastRay(scaled_ray, inRayCastSettings, inSubShapeIDCreator, ioCollector, inShapeFilter);
  106. }
  107. void ScaledShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  108. {
  109. // Test shape filter
  110. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  111. return;
  112. Vec3 inv_scale = mScale.Reciprocal();
  113. mInnerShape->CollidePoint(inv_scale * inPoint, inSubShapeIDCreator, ioCollector, inShapeFilter);
  114. }
  115. void ScaledShape::CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const
  116. {
  117. mInnerShape->CollideSoftBodyVertices(inCenterOfMassTransform, inScale * mScale, inVertices, inNumVertices, inCollidingShapeIndex);
  118. }
  119. void ScaledShape::CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  120. {
  121. // Test shape filter
  122. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  123. return;
  124. mInnerShape->CollectTransformedShapes(inBox, inPositionCOM, inRotation, inScale * mScale, inSubShapeIDCreator, ioCollector, inShapeFilter);
  125. }
  126. void ScaledShape::TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const
  127. {
  128. mInnerShape->TransformShape(inCenterOfMassTransform * Mat44::sScale(mScale), ioCollector);
  129. }
  130. void ScaledShape::SaveBinaryState(StreamOut &inStream) const
  131. {
  132. DecoratedShape::SaveBinaryState(inStream);
  133. inStream.Write(mScale);
  134. }
  135. void ScaledShape::RestoreBinaryState(StreamIn &inStream)
  136. {
  137. DecoratedShape::RestoreBinaryState(inStream);
  138. inStream.Read(mScale);
  139. }
  140. float ScaledShape::GetVolume() const
  141. {
  142. return abs(mScale.GetX() * mScale.GetY() * mScale.GetZ()) * mInnerShape->GetVolume();
  143. }
  144. bool ScaledShape::IsValidScale(Vec3Arg inScale) const
  145. {
  146. return mInnerShape->IsValidScale(inScale * mScale);
  147. }
  148. Vec3 ScaledShape::MakeScaleValid(Vec3Arg inScale) const
  149. {
  150. return mInnerShape->MakeScaleValid(mScale * inScale) / mScale;
  151. }
  152. void ScaledShape::sCollideScaledVsShape(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)
  153. {
  154. JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::Scaled);
  155. const ScaledShape *shape1 = static_cast<const ScaledShape *>(inShape1);
  156. CollisionDispatch::sCollideShapeVsShape(shape1->GetInnerShape(), inShape2, inScale1 * shape1->GetScale(), inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);
  157. }
  158. void ScaledShape::sCollideShapeVsScaled(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)
  159. {
  160. JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::Scaled);
  161. const ScaledShape *shape2 = static_cast<const ScaledShape *>(inShape2);
  162. CollisionDispatch::sCollideShapeVsShape(inShape1, shape2->GetInnerShape(), inScale1, inScale2 * shape2->GetScale(), inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector, inShapeFilter);
  163. }
  164. void ScaledShape::sCastScaledVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  165. {
  166. JPH_ASSERT(inShapeCast.mShape->GetSubType() == EShapeSubType::Scaled);
  167. const ScaledShape *shape = static_cast<const ScaledShape *>(inShapeCast.mShape);
  168. ShapeCast scaled_cast(shape->GetInnerShape(), inShapeCast.mScale * shape->GetScale(), inShapeCast.mCenterOfMassStart, inShapeCast.mDirection);
  169. CollisionDispatch::sCastShapeVsShapeLocalSpace(scaled_cast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  170. }
  171. void ScaledShape::sCastShapeVsScaled(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  172. {
  173. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::Scaled);
  174. const ScaledShape *shape = static_cast<const ScaledShape *>(inShape);
  175. CollisionDispatch::sCastShapeVsShapeLocalSpace(inShapeCast, inShapeCastSettings, shape->mInnerShape, inScale * shape->mScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  176. }
  177. void ScaledShape::sRegister()
  178. {
  179. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::Scaled);
  180. f.mConstruct = []() -> Shape * { return new ScaledShape; };
  181. f.mColor = Color::sYellow;
  182. for (EShapeSubType s : sAllSubShapeTypes)
  183. {
  184. CollisionDispatch::sRegisterCollideShape(EShapeSubType::Scaled, s, sCollideScaledVsShape);
  185. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::Scaled, sCollideShapeVsScaled);
  186. CollisionDispatch::sRegisterCastShape(EShapeSubType::Scaled, s, sCastScaledVsShape);
  187. CollisionDispatch::sRegisterCastShape(s, EShapeSubType::Scaled, sCastShapeVsScaled);
  188. }
  189. }
  190. JPH_NAMESPACE_END