Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/MutableCompoundShape.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>78JPH_NAMESPACE_BEGIN910class CollideShapeSettings;1112/// Class that constructs a MutableCompoundShape.13class JPH_EXPORT MutableCompoundShapeSettings final : public CompoundShapeSettings14{15JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, MutableCompoundShapeSettings)1617public:18// See: ShapeSettings19virtual ShapeResult Create() const override;20};2122/// A compound shape, sub shapes can be rotated and translated.23/// This shape is optimized for adding / removing and changing the rotation / translation of sub shapes but is less efficient in querying.24/// Shifts all child objects so that they're centered around the center of mass (which needs to be kept up to date by calling AdjustCenterOfMass).25///26/// Note: If you're using MutableCompoundShape and are querying data while modifying the shape you'll have a race condition.27/// In this case it is best to create a new MutableCompoundShape using the Clone function. You replace the shape on a body using BodyInterface::SetShape.28/// If a query is still working on the old shape, it will have taken a reference and keep the old shape alive until the query finishes.29///30/// When you modify a MutableCompoundShape, beware that the SubShapeIDs of all other shapes can change. So be careful when storing SubShapeIDs.31class JPH_EXPORT MutableCompoundShape final : public CompoundShape32{33public:34JPH_OVERRIDE_NEW_DELETE3536/// Constructor37MutableCompoundShape() : CompoundShape(EShapeSubType::MutableCompound) { }38MutableCompoundShape(const MutableCompoundShapeSettings &inSettings, ShapeResult &outResult);3940/// Clone this shape. Can be used to avoid race conditions. See the documentation of this class for more information.41Ref<MutableCompoundShape> Clone() const;4243// See Shape::CastRay44virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;45virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;4647// See: Shape::CollidePoint48virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;4950// See Shape::CollectTransformedShapes51virtual void CollectTransformedShapes(const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override;5253// See: CompoundShape::GetIntersectingSubShapes54virtual int GetIntersectingSubShapes(const AABox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;5556// See: CompoundShape::GetIntersectingSubShapes57virtual int GetIntersectingSubShapes(const OrientedBox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override;5859// See Shape60virtual void SaveBinaryState(StreamOut &inStream) const override;6162// See Shape::GetStats63virtual Stats GetStats() const override { return Stats(sizeof(*this) + mSubShapes.size() * sizeof(SubShape) + mSubShapeBounds.size() * sizeof(Bounds), 0); }6465///@{66/// @name Mutating shapes. Note that this is not thread safe, so you need to ensure that any bodies that use this shape are locked at the time of modification using BodyLockWrite. After modification you need to call BodyInterface::NotifyShapeChanged to update the broadphase and collision caches.6768/// Adding a new shape.69/// Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.70/// @param inPosition The position of the new shape71/// @param inRotation The orientation of the new shape72/// @param inShape The shape to add73/// @param inUserData User data that will be stored with the shape and can be retrieved using GetCompoundUserData74/// @param inIndex Index where to insert the shape, UINT_MAX to add to the end75/// @return The index of the newly added shape76uint AddShape(Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape, uint32 inUserData = 0, uint inIndex = UINT_MAX);7778/// Remove a shape by index.79/// Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.80void RemoveShape(uint inIndex);8182/// Modify the position / orientation of a shape.83/// Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.84void ModifyShape(uint inIndex, Vec3Arg inPosition, QuatArg inRotation);8586/// Modify the position / orientation and shape at the same time.87/// Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.88void ModifyShape(uint inIndex, Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape);8990/// @brief Batch set positions / orientations, this avoids duplicate work due to bounding box calculation.91/// Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.92/// @param inStartIndex Index of first shape to update93/// @param inNumber Number of shapes to update94/// @param inPositions A list of positions with arbitrary stride95/// @param inRotations A list of orientations with arbitrary stride96/// @param inPositionStride The position stride (the number of bytes between the first and second element)97/// @param inRotationStride The orientation stride (the number of bytes between the first and second element)98void ModifyShapes(uint inStartIndex, uint inNumber, const Vec3 *inPositions, const Quat *inRotations, uint inPositionStride = sizeof(Vec3), uint inRotationStride = sizeof(Quat));99100/// Recalculate the center of mass and shift all objects so they're centered around it101/// (this needs to be done of dynamic bodies and if the center of mass changes significantly due to adding / removing / repositioning sub shapes or else the simulation will look unnatural)102/// Note that after adjusting the center of mass of an object you need to call BodyInterface::NotifyShapeChanged and Constraint::NotifyShapeChanged on the relevant bodies / constraints.103/// Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.104void AdjustCenterOfMass();105106///@}107108// Register shape functions with the registry109static void sRegister();110111protected:112// See: Shape::RestoreBinaryState113virtual void RestoreBinaryState(StreamIn &inStream) override;114115private:116// Visitor for GetIntersectingSubShapes117template <class BoxType>118struct GetIntersectingSubShapesVisitorMC : public GetIntersectingSubShapesVisitor<BoxType>119{120using GetIntersectingSubShapesVisitor<BoxType>::GetIntersectingSubShapesVisitor;121122using Result = UVec4;123124JPH_INLINE Result TestBlock(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ) const125{126return GetIntersectingSubShapesVisitor<BoxType>::TestBounds(inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);127}128129JPH_INLINE bool ShouldVisitBlock(UVec4Arg inResult) const130{131return inResult.TestAnyTrue();132}133134JPH_INLINE bool ShouldVisitSubShape(UVec4Arg inResult, uint inIndexInBlock) const135{136return inResult[inIndexInBlock] != 0;137}138};139140/// Get the number of blocks of 4 bounding boxes141inline uint GetNumBlocks() const { return ((uint)mSubShapes.size() + 3) >> 2; }142143/// Ensure that the mSubShapeBounds has enough space to store bounding boxes equivalent to the number of shapes in mSubShapes144void EnsureSubShapeBoundsCapacity();145146/// Update mSubShapeBounds147/// @param inStartIdx First sub shape to update148/// @param inNumber Number of shapes to update149void CalculateSubShapeBounds(uint inStartIdx, uint inNumber);150151/// Calculate mLocalBounds from mSubShapeBounds152void CalculateLocalBounds();153154template <class Visitor>155JPH_INLINE void WalkSubShapes(Visitor &ioVisitor) const; ///< Walk the sub shapes and call Visitor::VisitShape for each sub shape encountered156157// Helper functions called by CollisionDispatch158static 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);159static 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);160static 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);161162struct Bounds163{164Vec4 mMinX;165Vec4 mMinY;166Vec4 mMinZ;167Vec4 mMaxX;168Vec4 mMaxY;169Vec4 mMaxZ;170};171172Array<Bounds> mSubShapeBounds; ///< Bounding boxes of all sub shapes in SOA format (in blocks of 4 boxes), MinX 0..3, MinY 0..3, MinZ 0..3, MaxX 0..3, MaxY 0..3, MaxZ 0..3, MinX 4..7, MinY 4..7, ...173};174175JPH_NAMESPACE_END176177178