ScaledShape.cpp 10 KB

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