OffsetCenterOfMassShape.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  5. #include <Physics/Collision/CollisionDispatch.h>
  6. #include <Physics/Collision/RayCast.h>
  7. #include <Physics/Collision/ShapeCast.h>
  8. #include <Physics/Collision/TransformedShape.h>
  9. #include <Core/StreamIn.h>
  10. #include <Core/StreamOut.h>
  11. #include <ObjectStream/TypeDeclarations.h>
  12. namespace JPH {
  13. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(OffsetCenterOfMassShapeSettings)
  14. {
  15. JPH_ADD_BASE_CLASS(OffsetCenterOfMassShapeSettings, DecoratedShapeSettings)
  16. JPH_ADD_ATTRIBUTE(OffsetCenterOfMassShapeSettings, mOffset)
  17. }
  18. ShapeSettings::ShapeResult OffsetCenterOfMassShapeSettings::Create() const
  19. {
  20. if (mCachedResult.IsEmpty())
  21. Ref<Shape> shape = new OffsetCenterOfMassShape(*this, mCachedResult);
  22. return mCachedResult;
  23. }
  24. OffsetCenterOfMassShape::OffsetCenterOfMassShape(const OffsetCenterOfMassShapeSettings &inSettings, ShapeResult &outResult) :
  25. DecoratedShape(EShapeSubType::OffsetCenterOfMass, inSettings, outResult),
  26. mOffset(inSettings.mOffset)
  27. {
  28. if (outResult.HasError())
  29. return;
  30. outResult.Set(this);
  31. }
  32. AABox OffsetCenterOfMassShape::GetLocalBounds() const
  33. {
  34. AABox bounds = mInnerShape->GetLocalBounds();
  35. bounds.mMin -= mOffset;
  36. bounds.mMax -= mOffset;
  37. return bounds;
  38. }
  39. AABox OffsetCenterOfMassShape::GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
  40. {
  41. return mInnerShape->GetWorldSpaceBounds(inCenterOfMassTransform * Mat44::sTranslation(-mOffset), inScale);
  42. }
  43. TransformedShape OffsetCenterOfMassShape::GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const
  44. {
  45. // We don't use any bits in the sub shape ID
  46. outRemainder = inSubShapeID;
  47. TransformedShape ts(inPositionCOM - inRotation * mOffset, inRotation, mInnerShape, BodyID());
  48. ts.SetShapeScale(inScale);
  49. return ts;
  50. }
  51. Vec3 OffsetCenterOfMassShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const
  52. {
  53. // Transform surface position to local space and pass call on
  54. return mInnerShape->GetSurfaceNormal(inSubShapeID, inLocalSurfacePosition + mOffset);
  55. }
  56. void OffsetCenterOfMassShape::GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy) const
  57. {
  58. mInnerShape->GetSubmergedVolume(inCenterOfMassTransform * Mat44::sTranslation(-mOffset), inScale, inSurface, outTotalVolume, outSubmergedVolume, outCenterOfBuoyancy);
  59. }
  60. #ifdef JPH_DEBUG_RENDERER
  61. void OffsetCenterOfMassShape::Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
  62. {
  63. mInnerShape->Draw(inRenderer, inCenterOfMassTransform * Mat44::sTranslation(-mOffset), inScale, inColor, inUseMaterialColors, inDrawWireframe);
  64. }
  65. void OffsetCenterOfMassShape::DrawGetSupportFunction(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const
  66. {
  67. mInnerShape->DrawGetSupportFunction(inRenderer, inCenterOfMassTransform * Mat44::sTranslation(-mOffset), inScale, inColor, inDrawSupportDirection);
  68. }
  69. void OffsetCenterOfMassShape::DrawGetSupportingFace(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
  70. {
  71. mInnerShape->DrawGetSupportingFace(inRenderer, inCenterOfMassTransform * Mat44::sTranslation(-mOffset), inScale);
  72. }
  73. #endif // JPH_DEBUG_RENDERER
  74. bool OffsetCenterOfMassShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  75. {
  76. // Transform the ray to local space
  77. RayCast ray = inRay;
  78. ray.mOrigin += mOffset;
  79. return mInnerShape->CastRay(ray, inSubShapeIDCreator, ioHit);
  80. }
  81. void OffsetCenterOfMassShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) const
  82. {
  83. // Transform the ray to local space
  84. RayCast ray = inRay;
  85. ray.mOrigin += mOffset;
  86. return mInnerShape->CastRay(ray, inRayCastSettings, inSubShapeIDCreator, ioCollector);
  87. }
  88. void OffsetCenterOfMassShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const
  89. {
  90. // Pass the point on to the inner shape in local space
  91. mInnerShape->CollidePoint(inPoint + mOffset, inSubShapeIDCreator, ioCollector);
  92. }
  93. void OffsetCenterOfMassShape::CastShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector) const
  94. {
  95. // Transform the shape cast
  96. ShapeCast shape_cast = inShapeCast.PostTransformed(Mat44::sTranslation(mOffset));
  97. mInnerShape->CastShape(shape_cast, inShapeCastSettings, inScale, inShapeFilter, inCenterOfMassTransform2 * Mat44::sTranslation(-mOffset), inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  98. }
  99. void OffsetCenterOfMassShape::CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector) const
  100. {
  101. mInnerShape->CollectTransformedShapes(inBox, inPositionCOM - inRotation * mOffset, inRotation, inScale, inSubShapeIDCreator, ioCollector);
  102. }
  103. void OffsetCenterOfMassShape::TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const
  104. {
  105. mInnerShape->TransformShape(inCenterOfMassTransform * Mat44::sTranslation(-mOffset), ioCollector);
  106. }
  107. void OffsetCenterOfMassShape::sCollideOffsetCenterOfMassVsShape(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)
  108. {
  109. JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::OffsetCenterOfMass);
  110. const OffsetCenterOfMassShape *shape1 = static_cast<const OffsetCenterOfMassShape *>(inShape1);
  111. CollisionDispatch::sCollideShapeVsShape(shape1->mInnerShape, inShape2, inScale1, inScale2, inCenterOfMassTransform1 * Mat44::sTranslation(-shape1->mOffset), inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  112. }
  113. void OffsetCenterOfMassShape::sCollideShapeVsOffsetCenterOfMass(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)
  114. {
  115. JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::OffsetCenterOfMass);
  116. const OffsetCenterOfMassShape *shape2 = static_cast<const OffsetCenterOfMassShape *>(inShape2);
  117. CollisionDispatch::sCollideShapeVsShape(inShape1, shape2->mInnerShape, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2 * Mat44::sTranslation(-shape2->mOffset), inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  118. }
  119. void OffsetCenterOfMassShape::sCastOffsetCenterOfMassVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  120. {
  121. // Fetch offset center of mass shape from cast shape
  122. JPH_ASSERT(inShapeCast.mShape->GetSubType() == EShapeSubType::OffsetCenterOfMass);
  123. const OffsetCenterOfMassShape *shape1 = static_cast<const OffsetCenterOfMassShape *>(inShapeCast.mShape);
  124. // Transform the shape cast and update the shape
  125. ShapeCast shape_cast(shape1->mInnerShape, inShapeCast.mScale, inShapeCast.mCenterOfMassStart * Mat44::sTranslation(-shape1->mOffset), inShapeCast.mDirection);
  126. CollisionDispatch::sCastShapeVsShape(shape_cast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  127. }
  128. void OffsetCenterOfMassShape::SaveBinaryState(StreamOut &inStream) const
  129. {
  130. DecoratedShape::SaveBinaryState(inStream);
  131. inStream.Write(mOffset);
  132. }
  133. void OffsetCenterOfMassShape::RestoreBinaryState(StreamIn &inStream)
  134. {
  135. DecoratedShape::RestoreBinaryState(inStream);
  136. inStream.Read(mOffset);
  137. }
  138. void OffsetCenterOfMassShape::sRegister()
  139. {
  140. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::OffsetCenterOfMass);
  141. f.mConstruct = []() -> Shape * { return new OffsetCenterOfMassShape; };
  142. f.mColor = Color::sCyan;
  143. for (EShapeSubType s : sAllSubShapeTypes)
  144. {
  145. CollisionDispatch::sRegisterCollideShape(EShapeSubType::OffsetCenterOfMass, s, sCollideOffsetCenterOfMassVsShape);
  146. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::OffsetCenterOfMass, sCollideShapeVsOffsetCenterOfMass);
  147. }
  148. CollisionDispatch::sRegisterCastShape(EShapeSubType::OffsetCenterOfMass, sCastOffsetCenterOfMassVsShape);
  149. }
  150. } // JPH