ScaledShape.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Collision/Shape/ScaledShape.h>
  5. #include <Physics/Collision/RayCast.h>
  6. #include <Physics/Collision/ShapeCast.h>
  7. #include <Physics/Collision/TransformedShape.h>
  8. #include <Physics/Collision/CollisionDispatch.h>
  9. #include <ObjectStream/TypeDeclarations.h>
  10. #include <Core/StreamIn.h>
  11. #include <Core/StreamOut.h>
  12. namespace JPH {
  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::GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy) const
  64. {
  65. mInnerShape->GetSubmergedVolume(inCenterOfMassTransform, inScale * mScale, inSurface, outTotalVolume, outSubmergedVolume, outCenterOfBuoyancy);
  66. }
  67. #ifdef JPH_DEBUG_RENDERER
  68. void ScaledShape::Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
  69. {
  70. mInnerShape->Draw(inRenderer, inCenterOfMassTransform, inScale * mScale, inColor, inUseMaterialColors, inDrawWireframe);
  71. }
  72. void ScaledShape::DrawGetSupportFunction(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const
  73. {
  74. mInnerShape->DrawGetSupportFunction(inRenderer, inCenterOfMassTransform, inScale * mScale, inColor, inDrawSupportDirection);
  75. }
  76. void ScaledShape::DrawGetSupportingFace(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
  77. {
  78. mInnerShape->DrawGetSupportingFace(inRenderer, inCenterOfMassTransform, inScale * mScale);
  79. }
  80. #endif // JPH_DEBUG_RENDERER
  81. bool ScaledShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  82. {
  83. Vec3 inv_scale = mScale.Reciprocal();
  84. RayCast scaled_ray { inv_scale * inRay.mOrigin, inv_scale * inRay.mDirection };
  85. return mInnerShape->CastRay(scaled_ray, inSubShapeIDCreator, ioHit);
  86. }
  87. void ScaledShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) const
  88. {
  89. Vec3 inv_scale = mScale.Reciprocal();
  90. RayCast scaled_ray { inv_scale * inRay.mOrigin, inv_scale * inRay.mDirection };
  91. return mInnerShape->CastRay(scaled_ray, inRayCastSettings, inSubShapeIDCreator, ioCollector);
  92. }
  93. void ScaledShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const
  94. {
  95. Vec3 inv_scale = mScale.Reciprocal();
  96. mInnerShape->CollidePoint(inv_scale * inPoint, inSubShapeIDCreator, ioCollector);
  97. }
  98. void ScaledShape::CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector) const
  99. {
  100. mInnerShape->CollectTransformedShapes(inBox, inPositionCOM, inRotation, inScale * mScale, inSubShapeIDCreator, ioCollector);
  101. }
  102. void ScaledShape::TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const
  103. {
  104. mInnerShape->TransformShape(inCenterOfMassTransform * Mat44::sScale(mScale), ioCollector);
  105. }
  106. void ScaledShape::SaveBinaryState(StreamOut &inStream) const
  107. {
  108. DecoratedShape::SaveBinaryState(inStream);
  109. inStream.Write(mScale);
  110. }
  111. void ScaledShape::RestoreBinaryState(StreamIn &inStream)
  112. {
  113. DecoratedShape::RestoreBinaryState(inStream);
  114. inStream.Read(mScale);
  115. }
  116. float ScaledShape::GetVolume() const
  117. {
  118. return abs(mScale.GetX() * mScale.GetY() * mScale.GetZ()) * mInnerShape->GetVolume();
  119. }
  120. bool ScaledShape::IsValidScale(Vec3Arg inScale) const
  121. {
  122. return mInnerShape->IsValidScale(inScale * mScale);
  123. }
  124. 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)
  125. {
  126. JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::Scaled);
  127. const ScaledShape *shape1 = static_cast<const ScaledShape *>(inShape1);
  128. CollisionDispatch::sCollideShapeVsShape(shape1->GetInnerShape(), inShape2, inScale1 * shape1->GetScale(), inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  129. }
  130. 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)
  131. {
  132. JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::Scaled);
  133. const ScaledShape *shape2 = static_cast<const ScaledShape *>(inShape2);
  134. CollisionDispatch::sCollideShapeVsShape(inShape1, shape2->GetInnerShape(), inScale1, inScale2 * shape2->GetScale(), inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  135. }
  136. 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)
  137. {
  138. JPH_ASSERT(inShapeCast.mShape->GetSubType() == EShapeSubType::Scaled);
  139. const ScaledShape *shape = static_cast<const ScaledShape *>(inShapeCast.mShape);
  140. ShapeCast scaled_cast(shape->GetInnerShape(), inShapeCast.mScale * shape->GetScale(), inShapeCast.mCenterOfMassStart, inShapeCast.mDirection);
  141. CollisionDispatch::sCastShapeVsShape(scaled_cast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  142. }
  143. 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)
  144. {
  145. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::Scaled);
  146. const ScaledShape *shape = static_cast<const ScaledShape *>(inShape);
  147. CollisionDispatch::sCastShapeVsShape(inShapeCast, inShapeCastSettings, shape->mInnerShape, inScale * shape->mScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  148. }
  149. void ScaledShape::sRegister()
  150. {
  151. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::Scaled);
  152. f.mConstruct = []() -> Shape * { return new ScaledShape; };
  153. f.mColor = Color::sYellow;
  154. for (EShapeSubType s : sAllSubShapeTypes)
  155. {
  156. CollisionDispatch::sRegisterCollideShape(EShapeSubType::Scaled, s, sCollideScaledVsShape);
  157. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::Scaled, sCollideShapeVsScaled);
  158. CollisionDispatch::sRegisterCastShape(EShapeSubType::Scaled, s, sCastScaledVsShape);
  159. CollisionDispatch::sRegisterCastShape(s, EShapeSubType::Scaled, sCastShapeVsScaled);
  160. }
  161. }
  162. } // JPH