StaticCompoundShape.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Collision/Shape/CompoundShape.h>
  6. #include <Jolt/Physics/Collision/SortReverseAndStore.h>
  7. #include <Jolt/Math/HalfFloat.h>
  8. JPH_NAMESPACE_BEGIN
  9. class CollideShapeSettings;
  10. class TempAllocator;
  11. /// Class that constructs a StaticCompoundShape. Note that if you only want a compound of 1 shape, use a RotatedTranslatedShape instead.
  12. class StaticCompoundShapeSettings final : public CompoundShapeSettings
  13. {
  14. public:
  15. JPH_DECLARE_SERIALIZABLE_VIRTUAL(StaticCompoundShapeSettings)
  16. // See: ShapeSettings
  17. virtual ShapeResult Create() const override;
  18. /// Specialization of Create() function that allows specifying a temp allocator to avoid temporary memory allocations on the heap
  19. ShapeResult Create(TempAllocator &inTempAllocator) const;
  20. };
  21. /// A compound shape, sub shapes can be rotated and translated.
  22. /// Sub shapes cannot be modified once the shape is constructed.
  23. /// Shifts all child objects so that they're centered around the center of mass.
  24. class StaticCompoundShape final : public CompoundShape
  25. {
  26. public:
  27. JPH_OVERRIDE_NEW_DELETE
  28. /// Constructor
  29. StaticCompoundShape() : CompoundShape(EShapeSubType::StaticCompound) { }
  30. StaticCompoundShape(const StaticCompoundShapeSettings &inSettings, TempAllocator &inTempAllocator, ShapeResult &outResult);
  31. // See Shape::CastRay
  32. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  33. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  34. // See: Shape::CollidePoint
  35. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  36. // See Shape::CollectTransformedShapes
  37. virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override;
  38. // See: CompoundShape::GetIntersectingSubShapes
  39. virtual int GetIntersectingSubShapes(const AABox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;
  40. // See: CompoundShape::GetIntersectingSubShapes
  41. virtual int GetIntersectingSubShapes(const OrientedBox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;
  42. // See Shape
  43. virtual void SaveBinaryState(StreamOut &inStream) const override;
  44. // See Shape::GetStats
  45. virtual Stats GetStats() const override { return Stats(sizeof(*this) + mSubShapes.size() * sizeof(SubShape) + mNodes.size() * sizeof(Node), 0); }
  46. // Register shape functions with the registry
  47. static void sRegister();
  48. protected:
  49. // See: Shape::RestoreBinaryState
  50. virtual void RestoreBinaryState(StreamIn &inStream) override;
  51. private:
  52. // Visitor for GetIntersectingSubShapes
  53. template <class BoxType>
  54. struct GetIntersectingSubShapesVisitorSC : public GetIntersectingSubShapesVisitor<BoxType>
  55. {
  56. using GetIntersectingSubShapesVisitor<BoxType>::GetIntersectingSubShapesVisitor;
  57. JPH_INLINE bool ShouldVisitNode(int inStackTop) const
  58. {
  59. return true;
  60. }
  61. JPH_INLINE int VisitNodes(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)
  62. {
  63. // Test if point overlaps with box
  64. UVec4 collides = GetIntersectingSubShapesVisitor<BoxType>::TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  65. return CountAndSortTrues(collides, ioProperties);
  66. }
  67. };
  68. /// Sorts ioBodyIdx spatially into 2 groups. Second groups starts at ioBodyIdx + outMidPoint.
  69. /// After the function returns ioBodyIdx and ioBounds will be shuffled
  70. static void sPartition(uint *ioBodyIdx, AABox *ioBounds, int inNumber, int &outMidPoint);
  71. /// Sorts ioBodyIdx from inBegin to (but excluding) inEnd spatially into 4 groups.
  72. /// outSplit needs to be 5 ints long, when the function returns each group runs from outSplit[i] to (but excluding) outSplit[i + 1]
  73. /// After the function returns ioBodyIdx and ioBounds will be shuffled
  74. static void sPartition4(uint *ioBodyIdx, AABox *ioBounds, int inBegin, int inEnd, int *outSplit);
  75. // Helper functions called by CollisionDispatch
  76. static void sCollideCompoundVsShape(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);
  77. static void sCollideShapeVsCompound(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);
  78. static void sCastShapeVsCompound(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  79. // Maximum size of the stack during tree walk
  80. static constexpr int cStackSize = 128;
  81. template <class Visitor>
  82. JPH_INLINE void WalkTree(Visitor &ioVisitor) const; ///< Walk the node tree calling the Visitor::VisitNodes for each node encountered and Visitor::VisitShape for each sub shape encountered
  83. /// Bits used in Node::mNodeProperties
  84. enum : uint32
  85. {
  86. IS_SUBSHAPE = 0x80000000, ///< If this bit is set, the other bits index in mSubShape, otherwise in mNodes
  87. INVALID_NODE = 0x7fffffff, ///< Signifies an invalid node
  88. };
  89. /// Node structure
  90. struct Node
  91. {
  92. void SetChildBounds(uint inIndex, const AABox &inBounds); ///< Set bounding box for child inIndex to inBounds
  93. void SetChildInvalid(uint inIndex); ///< Mark the child inIndex as invalid and set its bounding box to invalid
  94. HalfFloat mBoundsMinX[4]; ///< 4 child bounding boxes
  95. HalfFloat mBoundsMinY[4];
  96. HalfFloat mBoundsMinZ[4];
  97. HalfFloat mBoundsMaxX[4];
  98. HalfFloat mBoundsMaxY[4];
  99. HalfFloat mBoundsMaxZ[4];
  100. uint32 mNodeProperties[4]; ///< 4 child node properties
  101. };
  102. static_assert(sizeof(Node) == 64, "Node should be 64 bytes");
  103. using Nodes = Array<Node>;
  104. Nodes mNodes; ///< Quad tree node structure
  105. };
  106. JPH_NAMESPACE_END