Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/StaticCompoundShape.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Collision/Shape/CompoundShape.h>7#include <Jolt/Physics/Collision/SortReverseAndStore.h>8#include <Jolt/Math/HalfFloat.h>910JPH_NAMESPACE_BEGIN1112class CollideShapeSettings;13class TempAllocator;1415/// Class that constructs a StaticCompoundShape. Note that if you only want a compound of 1 shape, use a RotatedTranslatedShape instead.16class JPH_EXPORT StaticCompoundShapeSettings final : public CompoundShapeSettings17{18JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, StaticCompoundShapeSettings)1920public:21// See: ShapeSettings22virtual ShapeResult Create() const override;2324/// Specialization of Create() function that allows specifying a temp allocator to avoid temporary memory allocations on the heap25ShapeResult Create(TempAllocator &inTempAllocator) const;26};2728/// A compound shape, sub shapes can be rotated and translated.29/// Sub shapes cannot be modified once the shape is constructed.30/// Shifts all child objects so that they're centered around the center of mass.31class JPH_EXPORT StaticCompoundShape final : public CompoundShape32{33public:34JPH_OVERRIDE_NEW_DELETE3536/// Constructor37StaticCompoundShape() : CompoundShape(EShapeSubType::StaticCompound) { }38StaticCompoundShape(const StaticCompoundShapeSettings &inSettings, TempAllocator &inTempAllocator, ShapeResult &outResult);3940// See Shape::CastRay41virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;42virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;4344// See: Shape::CollidePoint45virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;4647// See Shape::CollectTransformedShapes48virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override;4950// See: CompoundShape::GetIntersectingSubShapes51virtual int GetIntersectingSubShapes(const AABox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;5253// See: CompoundShape::GetIntersectingSubShapes54virtual int GetIntersectingSubShapes(const OrientedBox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;5556// See Shape57virtual void SaveBinaryState(StreamOut &inStream) const override;5859// See Shape::GetStats60virtual Stats GetStats() const override { return Stats(sizeof(*this) + mSubShapes.size() * sizeof(SubShape) + mNodes.size() * sizeof(Node), 0); }6162// Register shape functions with the registry63static void sRegister();6465protected:66// See: Shape::RestoreBinaryState67virtual void RestoreBinaryState(StreamIn &inStream) override;6869private:70// Visitor for GetIntersectingSubShapes71template <class BoxType>72struct GetIntersectingSubShapesVisitorSC : public GetIntersectingSubShapesVisitor<BoxType>73{74using GetIntersectingSubShapesVisitor<BoxType>::GetIntersectingSubShapesVisitor;7576JPH_INLINE bool ShouldVisitNode(int inStackTop) const77{78return true;79}8081JPH_INLINE int VisitNodes(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioProperties, int inStackTop)82{83// Test if point overlaps with box84UVec4 collides = GetIntersectingSubShapesVisitor<BoxType>::TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);85return CountAndSortTrues(collides, ioProperties);86}87};8889/// Sorts ioBodyIdx spatially into 2 groups. Second groups starts at ioBodyIdx + outMidPoint.90/// After the function returns ioBodyIdx and ioBounds will be shuffled91static void sPartition(uint *ioBodyIdx, AABox *ioBounds, int inNumber, int &outMidPoint);9293/// Sorts ioBodyIdx from inBegin to (but excluding) inEnd spatially into 4 groups.94/// outSplit needs to be 5 ints long, when the function returns each group runs from outSplit[i] to (but excluding) outSplit[i + 1]95/// After the function returns ioBodyIdx and ioBounds will be shuffled96static void sPartition4(uint *ioBodyIdx, AABox *ioBounds, int inBegin, int inEnd, int *outSplit);9798// Helper functions called by CollisionDispatch99static 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);100static 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);101static 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);102103// Maximum size of the stack during tree walk104static constexpr int cStackSize = 128;105106template <class Visitor>107JPH_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 encountered108109/// Bits used in Node::mNodeProperties110enum : uint32111{112IS_SUBSHAPE = 0x80000000, ///< If this bit is set, the other bits index in mSubShape, otherwise in mNodes113INVALID_NODE = 0x7fffffff, ///< Signifies an invalid node114};115116/// Node structure117struct Node118{119void SetChildBounds(uint inIndex, const AABox &inBounds); ///< Set bounding box for child inIndex to inBounds120void SetChildInvalid(uint inIndex); ///< Mark the child inIndex as invalid and set its bounding box to invalid121122HalfFloat mBoundsMinX[4]; ///< 4 child bounding boxes123HalfFloat mBoundsMinY[4];124HalfFloat mBoundsMinZ[4];125HalfFloat mBoundsMaxX[4];126HalfFloat mBoundsMaxY[4];127HalfFloat mBoundsMaxZ[4];128uint32 mNodeProperties[4]; ///< 4 child node properties129};130131static_assert(sizeof(Node) == 64, "Node should be 64 bytes");132133using Nodes = Array<Node>;134135Nodes mNodes; ///< Quad tree node structure136};137138JPH_NAMESPACE_END139140141