StaticCompoundShape.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Collision/Shape/CompoundShape.h>
  5. #include <Math/HalfFloat.h>
  6. namespace JPH {
  7. class CollideShapeSettings;
  8. class TempAllocator;
  9. /// Class that constructs a StaticCompoundShape. Note that if you only want a compound of 1 shape, use a RotatedTranslatedShape instead.
  10. class StaticCompoundShapeSettings final : public CompoundShapeSettings
  11. {
  12. public:
  13. JPH_DECLARE_SERIALIZABLE_VIRTUAL(StaticCompoundShapeSettings)
  14. // See: ShapeSettings
  15. virtual ShapeResult Create() const override;
  16. /// Specialization of Create() function that allows specifying a temp allocator to avoid temporary memory allocations on the heap
  17. ShapeResult Create(TempAllocator &inTempAllocator) const;
  18. };
  19. /// A compound shape, sub shapes can be rotated and translated.
  20. /// Sub shapes cannot be modified once the shape is constructed.
  21. /// Shifts all child objects so that they're centered around the center of mass.
  22. class StaticCompoundShape final : public CompoundShape
  23. {
  24. public:
  25. /// Constructor
  26. StaticCompoundShape() : CompoundShape(EShapeSubType::StaticCompound) { }
  27. StaticCompoundShape(const StaticCompoundShapeSettings &inSettings, TempAllocator &inTempAllocator, ShapeResult &outResult);
  28. // See Shape::CastRay
  29. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  30. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) const override;
  31. // See: Shape::CollidePoint
  32. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const override;
  33. // See Shape::CollectTransformedShapes
  34. virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector) const override;
  35. // See: CompoundShape::GetIntersectingSubShapes
  36. virtual int GetIntersectingSubShapes(const AABox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;
  37. // See: CompoundShape::GetIntersectingSubShapes
  38. virtual int GetIntersectingSubShapes(const OrientedBox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;
  39. // See Shape
  40. virtual void SaveBinaryState(StreamOut &inStream) const override;
  41. // See Shape::GetStats
  42. virtual Stats GetStats() const override { return Stats(sizeof(*this) + mSubShapes.size() * sizeof(SubShape) + mNodes.size() * sizeof(Node), 0); }
  43. // Register shape functions with the registry
  44. static void sRegister();
  45. protected:
  46. // See: Shape::RestoreBinaryState
  47. virtual void RestoreBinaryState(StreamIn &inStream) override;
  48. private:
  49. // Visitor for GetIntersectingSubShapes
  50. template <class BoxType>
  51. struct GetIntersectingSubShapesVisitorSC : public GetIntersectingSubShapesVisitor<BoxType>
  52. {
  53. using GetIntersectingSubShapesVisitor<BoxType>::GetIntersectingSubShapesVisitor;
  54. JPH_INLINE bool ShouldVisitNode(int inStackTop) const
  55. {
  56. return true;
  57. }
  58. JPH_INLINE int VisitNodes(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)
  59. {
  60. // Test if point overlaps with box
  61. UVec4 collides = GetIntersectingSubShapesVisitor<BoxType>::TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
  62. // Sort so the colliding ones go first
  63. UVec4::sSort4True(collides, ioProperties);
  64. return collides.CountTrues();
  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);
  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);
  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 = vector<Node>;
  103. Nodes mNodes; ///< Quad tree node structure
  104. };
  105. } // JPH