RotatedTranslatedShape.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/Shape/DecoratedShape.h>
  5. #include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
  6. JPH_NAMESPACE_BEGIN
  7. class CollideShapeSettings;
  8. /// Class that constructs a RotatedTranslatedShape
  9. class RotatedTranslatedShapeSettings final : public DecoratedShapeSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(RotatedTranslatedShapeSettings)
  13. /// Constructor
  14. RotatedTranslatedShapeSettings() = default;
  15. /// Construct with shape settings, can be serialized.
  16. RotatedTranslatedShapeSettings(Vec3Arg inPosition, QuatArg inRotation, const ShapeSettings *inShape) : DecoratedShapeSettings(inShape), mPosition(inPosition), mRotation(inRotation) { }
  17. /// Variant that uses a concrete shape, which means this object cannot be serialized.
  18. RotatedTranslatedShapeSettings(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape): DecoratedShapeSettings(inShape), mPosition(inPosition), mRotation(inRotation) { }
  19. // See: ShapeSettings
  20. virtual ShapeResult Create() const override;
  21. Vec3 mPosition; ///< Position of the sub shape
  22. Quat mRotation; ///< Rotation of the sub shape
  23. };
  24. /// A rotated translated shape will rotate and translate a child shape.
  25. /// Shifts the child object so that it is centered around the center of mass.
  26. class RotatedTranslatedShape final : public DecoratedShape
  27. {
  28. public:
  29. JPH_OVERRIDE_NEW_DELETE
  30. /// Constructor
  31. RotatedTranslatedShape() : DecoratedShape(EShapeSubType::RotatedTranslated) { }
  32. RotatedTranslatedShape(const RotatedTranslatedShapeSettings &inSettings, ShapeResult &outResult);
  33. RotatedTranslatedShape(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape);
  34. /// Access the rotation that is applied to the inner shape
  35. Quat GetRotation() const { return mRotation; }
  36. /// Access the translation that has been applied to the inner shape
  37. Vec3 GetPosition() const { return mCenterOfMass - mRotation.InverseRotate(mInnerShape->GetCenterOfMass()); }
  38. // See Shape::GetCenterOfMass
  39. virtual Vec3 GetCenterOfMass() const override { return mCenterOfMass; }
  40. // See Shape::GetLocalBounds
  41. virtual AABox GetLocalBounds() const override;
  42. // See Shape::GetWorldSpaceBounds
  43. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  44. // See Shape::GetInnerRadius
  45. virtual float GetInnerRadius() const override { return mInnerShape->GetInnerRadius(); }
  46. // See Shape::GetMassProperties
  47. virtual MassProperties GetMassProperties() const override;
  48. // See Shape::GetSubShapeTransformedShape
  49. virtual TransformedShape GetSubShapeTransformedShape(const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const override;
  50. // See Shape::GetSurfaceNormal
  51. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  52. // See Shape::GetSupportingFace
  53. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  54. // See Shape::GetSubmergedVolume
  55. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy) const override;
  56. #ifdef JPH_DEBUG_RENDERER
  57. // See Shape::Draw
  58. virtual void Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  59. // See Shape::DrawGetSupportFunction
  60. virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;
  61. // See Shape::DrawGetSupportingFace
  62. virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  63. #endif // JPH_DEBUG_RENDERER
  64. // See Shape::CastRay
  65. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  66. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  67. // See: Shape::CollidePoint
  68. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  69. // See Shape::CollectTransformedShapes
  70. virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override;
  71. // See Shape::TransformShape
  72. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  73. // See Shape::GetTrianglesStart
  74. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override { JPH_ASSERT(false, "Cannot call on non-leaf shapes, use CollectTransformedShapes to collect the leaves first!"); }
  75. // See Shape::GetTrianglesNext
  76. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override { JPH_ASSERT(false, "Cannot call on non-leaf shapes, use CollectTransformedShapes to collect the leaves first!"); return 0; }
  77. // See Shape
  78. virtual void SaveBinaryState(StreamOut &inStream) const override;
  79. // See Shape::GetStats
  80. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  81. // See Shape::GetVolume
  82. virtual float GetVolume() const override { return mInnerShape->GetVolume(); }
  83. // See Shape::IsValidScale
  84. virtual bool IsValidScale(Vec3Arg inScale) const override;
  85. // Register shape functions with the registry
  86. static void sRegister();
  87. protected:
  88. // See: Shape::RestoreBinaryState
  89. virtual void RestoreBinaryState(StreamIn &inStream) override;
  90. private:
  91. // Helper functions called by CollisionDispatch
  92. static void sCollideRotatedTranslatedVsShape(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);
  93. static void sCollideShapeVsRotatedTranslated(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);
  94. static void sCastRotatedTranslatedVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  95. static void sCastShapeVsRotatedTranslated(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  96. /// Transform the scale to the local space of the child shape
  97. inline Vec3 TransformScale(Vec3Arg inScale) const
  98. {
  99. // We don't need to transform uniform scale or if the rotation is identity
  100. if (mIsRotationIdentity || ScaleHelpers::IsUniformScale(inScale))
  101. return inScale;
  102. return ScaleHelpers::RotateScale(mRotation, inScale);
  103. }
  104. bool mIsRotationIdentity; ///< If mRotation is close to identity (put here because it falls in padding bytes)
  105. Vec3 mCenterOfMass; ///< Position of the center of mass
  106. Quat mRotation; ///< Rotation of the child shape
  107. };
  108. JPH_NAMESPACE_END