ScaledShape.cpp 10 KB

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