StaticCompoundShape.h 7.0 KB

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