Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
MutableCompoundShape Class Referencefinal

#include <MutableCompoundShape.h>

Inheritance diagram for MutableCompoundShape:
CompoundShape Shape RefTarget< Shape > NonCopyable

Public Member Functions

JPH_OVERRIDE_NEW_DELETE MutableCompoundShape ()
 Constructor.
 
 MutableCompoundShape (const MutableCompoundShapeSettings &inSettings, ShapeResult &outResult)
 
Ref< MutableCompoundShapeClone () const
 Clone this shape. Can be used to avoid race conditions. See the documentation of this class for more information.
 
virtual bool CastRay (const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override
 
virtual void CastRay (const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter={ }) const override
 
virtual void CollidePoint (Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter={ }) const override
 
virtual void CollectTransformedShapes (const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, const SubShapeIDCreator &inSubShapeIDCreator, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const override
 
virtual int GetIntersectingSubShapes (const AABox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override
 
virtual int GetIntersectingSubShapes (const OrientedBox &inBox, uint *outSubShapeIndices, int inMaxSubShapeIndices) const override
 
virtual void SaveBinaryState (StreamOut &inStream) const override
 Saves the contents of the shape in binary form to inStream.
 
virtual Stats GetStats () const override
 Get stats of this shape. Use for logging / data collection purposes only. Does not add values from child shapes, use GetStatsRecursive for this.
 
template<class Visitor >
void WalkSubShapes (Visitor &ioVisitor) const
 
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.
uint AddShape (Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape, uint32 inUserData=0)
 
void RemoveShape (uint inIndex)
 
void ModifyShape (uint inIndex, Vec3Arg inPosition, QuatArg inRotation)
 
void ModifyShape (uint inIndex, Vec3Arg inPosition, QuatArg inRotation, const Shape *inShape)
 
void ModifyShapes (uint inStartIndex, uint inNumber, const Vec3 *inPositions, const Quat *inRotations, uint inPositionStride=sizeof(Vec3), uint inRotationStride=sizeof(Quat))
 Batch set positions / orientations, this avoids duplicate work due to bounding box calculation. Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.
 
void AdjustCenterOfMass ()
 
- Public Member Functions inherited from CompoundShape
JPH_OVERRIDE_NEW_DELETE CompoundShape (EShapeSubType inSubType)
 Constructor.
 
 CompoundShape (EShapeSubType inSubType, const ShapeSettings &inSettings, ShapeResult &outResult)
 
virtual Vec3 GetCenterOfMass () const override
 All shapes are centered around their center of mass. This function returns the center of mass position that needs to be applied to transform the shape to where it was created.
 
virtual bool MustBeStatic () const override
 Check if this shape can only be used to create a static body or if it can also be dynamic/kinematic.
 
virtual AABox GetLocalBounds () const override
 Get local bounding box including convex radius, this box is centered around the center of mass rather than the world transform.
 
virtual uint GetSubShapeIDBitsRecursive () const override
 Get the max number of sub shape ID bits that are needed to be able to address any leaf shape in this shape. Used mainly for checking that it is smaller or equal than SubShapeID::MaxBits.
 
virtual AABox GetWorldSpaceBounds (Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override
 
virtual float GetInnerRadius () const override
 
virtual MassProperties GetMassProperties () const override
 Calculate the mass and inertia of this shape.
 
virtual const PhysicsMaterialGetMaterial (const SubShapeID &inSubShapeID) const override
 Get the material assigned to a particular sub shape ID.
 
virtual const ShapeGetLeafShape (const SubShapeID &inSubShapeID, SubShapeID &outRemainder) const override
 
virtual uint64 GetSubShapeUserData (const SubShapeID &inSubShapeID) const override
 Get the user data of a particular sub shape ID. Corresponds with the value stored in Shape::GetUserData of the leaf shape pointed to by inSubShapeID.
 
virtual TransformedShape GetSubShapeTransformedShape (const SubShapeID &inSubShapeID, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale, SubShapeID &outRemainder) const override
 
virtual Vec3 GetSurfaceNormal (const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override
 
virtual void GetSupportingFace (const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override
 
virtual void GetSubmergedVolume (Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override
 
virtual void Draw (DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override
 Draw the shape at a particular location with a particular color (debugging purposes)
 
virtual void DrawGetSupportFunction (DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override
 Draw the results of the GetSupportFunction with the convex radius added back on to show any errors introduced by this process (only relevant for convex shapes)
 
virtual void DrawGetSupportingFace (DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override
 Draw the results of the GetSupportingFace function to show any errors introduced by this process (only relevant for convex shapes)
 
virtual void CollideSoftBodyVertices (Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override
 
virtual void TransformShape (Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override
 
virtual void GetTrianglesStart (GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override
 
virtual int GetTrianglesNext (GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials=nullptr) const override
 
const SubShapesGetSubShapes () const
 Access to the sub shapes of this compound.
 
uint GetNumSubShapes () const
 Get the total number of sub shapes.
 
const SubShapeGetSubShape (uint inIdx) const
 Access to a particular sub shape.
 
uint32 GetCompoundUserData (uint inIdx) const
 Get the user data associated with a shape in this compound.
 
void SetCompoundUserData (uint inIdx, uint32 inUserData)
 Set the user data associated with a shape in this compound.
 
bool IsSubShapeIDValid (SubShapeID inSubShapeID) const
 
uint32 GetSubShapeIndexFromID (SubShapeID inSubShapeID, SubShapeID &outRemainder) const
 
SubShapeIDCreator GetSubShapeIDFromIndex (int inIdx, const SubShapeIDCreator &inParentSubShapeID) const
 Convert a sub shape index to a sub shape ID.
 
virtual void SaveSubShapeState (ShapeList &outSubShapes) const override
 Outputs the shape references that this shape has to outSubShapes.
 
virtual void RestoreSubShapeState (const ShapeRefC *inSubShapes, uint inNumShapes) override
 Restore the shape references after calling sRestoreFromBinaryState. Note that the exact same shapes need to be provided in the same order as returned by SaveSubShapeState.
 
virtual Stats GetStatsRecursive (VisitedShapes &ioVisitedShapes) const override
 Volume of this shape (m^3). Note that for compound shapes the volume may be incorrect since child shapes can overlap which is not accounted for.
 
virtual float GetVolume () const override
 
virtual bool IsValidScale (Vec3Arg inScale) const override
 
virtual Vec3 MakeScaleValid (Vec3Arg inScale) const override
 
virtual AABox GetWorldSpaceBounds (Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
 
AABox GetWorldSpaceBounds (DMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
 Get world space bounds including convex radius.
 
- Public Member Functions inherited from Shape
 Shape (EShapeType inType, EShapeSubType inSubType)
 Constructor.
 
 Shape (EShapeType inType, EShapeSubType inSubType, const ShapeSettings &inSettings, ShapeResult &outResult)
 
virtual ~Shape ()=default
 Destructor.
 
EShapeType GetType () const
 Get type.
 
EShapeSubType GetSubType () const
 
uint64 GetUserData () const
 User data (to be used freely by the application)
 
void SetUserData (uint64 inUserData)
 
AABox GetWorldSpaceBounds (DMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const
 Get world space bounds including convex radius.
 
virtual void GetSubmergedVolume (Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy, RVec3Arg inBaseOffset) const =0
 
ShapeResult ScaleShape (Vec3Arg inScale) const
 
virtual void SaveMaterialState (PhysicsMaterialList &outMaterials) const
 Outputs the material references that this shape has to outMaterials.
 
virtual void RestoreMaterialState (const PhysicsMaterialRefC *inMaterials, uint inNumMaterials)
 Restore the material references after calling sRestoreFromBinaryState. Note that the exact same materials need to be provided in the same order as returned by SaveMaterialState.
 
void SaveWithChildren (StreamOut &inStream, ShapeToIDMap &ioShapeMap, MaterialToIDMap &ioMaterialMap) const
 Save this shape, all its children and its materials. Pass in an empty map in ioShapeMap / ioMaterialMap or reuse the same map while saving multiple shapes to the same stream in order to avoid writing duplicates.
 
- Public Member Functions inherited from RefTarget< Shape >
 RefTarget ()=default
 Constructor.
 
 RefTarget (const RefTarget &)
 
 ~RefTarget ()
 assert no one is referencing us
 
void SetEmbedded () const
 
RefTargetoperator= (const RefTarget &)
 Assignment operator.
 
uint32 GetRefCount () const
 Get current refcount of this object.
 
void AddRef () const
 Add or release a reference to this object.
 
void Release () const
 
- Public Member Functions inherited from NonCopyable
 NonCopyable ()=default
 
 NonCopyable (const NonCopyable &)=delete
 
void operator= (const NonCopyable &)=delete
 

Static Public Member Functions

static void sRegister ()
 
- Static Public Member Functions inherited from CompoundShape
static void sRegister ()
 
- Static Public Member Functions inherited from Shape
static ShapeResult sRestoreFromBinaryState (StreamIn &inStream)
 Creates a Shape of the correct type and restores its contents from the binary stream inStream.
 
static ShapeResult sRestoreWithChildren (StreamIn &inStream, IDToShapeMap &ioShapeMap, IDToMaterialMap &ioMaterialMap)
 Restore a shape, all its children and materials. Pass in an empty map in ioShapeMap / ioMaterialMap or reuse the same map while reading multiple shapes from the same stream in order to restore duplicates.
 
- Static Public Member Functions inherited from RefTarget< Shape >
static int sInternalGetRefCountOffset ()
 INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
 

Protected Member Functions

virtual void RestoreBinaryState (StreamIn &inStream) override
 This function should not be called directly, it is used by sRestoreFromBinaryState.
 
- Protected Member Functions inherited from CompoundShape
uint GetSubShapeIDBits () const
 Determine amount of bits needed to encode sub shape id.
 
void CalculateInnerRadius ()
 Determine the inner radius of this shape.
 

Additional Inherited Members

- Public Types inherited from CompoundShape
using SubShapes = Array< SubShape >
 
- Public Types inherited from Shape
using ShapeResult = ShapeSettings::ShapeResult
 
using SupportingFace = StaticArray< Vec3, 32 >
 Type definition for a supporting face.
 
using VisitedShapes = UnorderedSet< const Shape * >
 
using ShapeToIDMap = StreamUtils::ObjectToIDMap< Shape >
 
using IDToShapeMap = StreamUtils::IDToObjectMap< Shape >
 
using MaterialToIDMap = StreamUtils::ObjectToIDMap< PhysicsMaterial >
 
using IDToMaterialMap = StreamUtils::IDToObjectMap< PhysicsMaterial >
 
- Static Public Attributes inherited from Shape
static constexpr int cGetTrianglesMinTrianglesRequested = 32
 This is the minimum amount of triangles that should be requested through GetTrianglesNext.
 
static bool sDrawSubmergedVolumes = false
 Debug helper which draws the intersection between water and the shapes, the center of buoyancy and the submerged volume.
 
- Static Protected Member Functions inherited from Shape
static void sCollidePointUsingRayCast (const Shape &inShape, Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter)
 A fallback version of CollidePoint that uses a ray cast and counts the number of hits to determine if the point is inside the shape. Odd number of hits means inside, even number of hits means outside.
 
- Protected Attributes inherited from CompoundShape
Vec3 mCenterOfMass { Vec3::sZero() }
 Center of mass of the compound.
 
AABox mLocalBounds
 
SubShapes mSubShapes
 
float mInnerRadius = FLT_MAX
 Smallest radius of GetInnerRadius() of child shapes.
 
- Protected Attributes inherited from RefTarget< Shape >
atomic< uint32mRefCount
 Current reference count.
 
- Static Protected Attributes inherited from RefTarget< Shape >
static constexpr uint32 cEmbedded
 A large value that gets added to the refcount to mark the object as embedded.
 

Detailed Description

A compound shape, sub shapes can be rotated and translated. This shape is optimized for adding / removing and changing the rotation / translation of sub shapes but is less efficient in querying. 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).

Note: If you're using MutableCompoundShape and are querying data while modifying the shape you'll have a race condition. 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. 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.

Constructor & Destructor Documentation

◆ MutableCompoundShape() [1/2]

JPH_OVERRIDE_NEW_DELETE MutableCompoundShape::MutableCompoundShape ( )
inline

Constructor.

◆ MutableCompoundShape() [2/2]

MutableCompoundShape::MutableCompoundShape ( const MutableCompoundShapeSettings inSettings,
ShapeResult outResult 
)

Member Function Documentation

◆ AddShape()

uint MutableCompoundShape::AddShape ( Vec3Arg  inPosition,
QuatArg  inRotation,
const Shape inShape,
uint32  inUserData = 0 
)

Adding a new shape. Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.

Returns
The index of the newly added shape

◆ AdjustCenterOfMass()

void MutableCompoundShape::AdjustCenterOfMass ( )

Recalculate the center of mass and shift all objects so they're centered around it (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) 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. Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.

◆ CastRay() [1/2]

void MutableCompoundShape::CastRay ( const RayCast inRay,
const RayCastSettings inRayCastSettings,
const SubShapeIDCreator inSubShapeIDCreator,
CastRayCollector ioCollector,
const ShapeFilter inShapeFilter = { } 
) const
overridevirtual

Cast a ray against this shape. Allows returning multiple hits through ioCollector. Note that this version is more flexible but also slightly slower than the CastRay function that returns only a single hit. If you want the surface normal of the hit use GetSurfaceNormal(collected sub shape ID, inRay.GetPointOnRay(collected faction)).

Implements Shape.

◆ CastRay() [2/2]

bool MutableCompoundShape::CastRay ( const RayCast inRay,
const SubShapeIDCreator inSubShapeIDCreator,
RayCastResult ioHit 
) const
overridevirtual

Cast a ray against this shape, returns true if it finds a hit closer than ioHit.mFraction and updates that fraction. Otherwise ioHit is left untouched and the function returns false. Note that the ray should be relative to the center of mass of this shape (i.e. subtract Shape::GetCenterOfMass() from RayCast::mOrigin if you want to cast against the shape in the space it was created). Convex objects will be treated as solid (meaning if the ray starts inside, you'll get a hit fraction of 0) and back face hits against triangles are returned. If you want the surface normal of the hit use GetSurfaceNormal(ioHit.mSubShapeID2, inRay.GetPointOnRay(ioHit.mFraction)).

Implements Shape.

◆ Clone()

Ref< MutableCompoundShape > MutableCompoundShape::Clone ( ) const

Clone this shape. Can be used to avoid race conditions. See the documentation of this class for more information.

◆ CollectTransformedShapes()

void MutableCompoundShape::CollectTransformedShapes ( const AABox inBox,
Vec3Arg  inPositionCOM,
QuatArg  inRotation,
Vec3Arg  inScale,
const SubShapeIDCreator inSubShapeIDCreator,
TransformedShapeCollector ioCollector,
const ShapeFilter inShapeFilter 
) const
overridevirtual

Collect the leaf transformed shapes of all leaf shapes of this shape. inBox is the world space axis aligned box which leaf shapes should collide with. inPositionCOM/inRotation/inScale describes the transform of this shape. inSubShapeIDCeator represents the current sub shape ID of this shape.

Reimplemented from Shape.

◆ CollidePoint()

void MutableCompoundShape::CollidePoint ( Vec3Arg  inPoint,
const SubShapeIDCreator inSubShapeIDCreator,
CollidePointCollector ioCollector,
const ShapeFilter inShapeFilter = { } 
) const
overridevirtual

Check if inPoint is inside this shape. For this tests all shapes are treated as if they were solid. Note that inPoint should be relative to the center of mass of this shape (i.e. subtract Shape::GetCenterOfMass() from inPoint if you want to test against the shape in the space it was created). For a mesh shape, this test will only provide sensible information if the mesh is a closed manifold. For each shape that collides, ioCollector will receive a hit.

Implements Shape.

◆ GetIntersectingSubShapes() [1/2]

int MutableCompoundShape::GetIntersectingSubShapes ( const AABox inBox,
uint outSubShapeIndices,
int  inMaxSubShapeIndices 
) const
overridevirtual

Get which sub shape's bounding boxes overlap with an axis aligned box

Parameters
inBoxThe axis aligned box to test against (relative to the center of mass of this shape)
outSubShapeIndicesBuffer where to place the indices of the sub shapes that intersect
inMaxSubShapeIndicesHow many indices will fit in the buffer (normally you'd provide a buffer of GetNumSubShapes() indices)
Returns
How many indices were placed in outSubShapeIndices

Implements CompoundShape.

◆ GetIntersectingSubShapes() [2/2]

int MutableCompoundShape::GetIntersectingSubShapes ( const OrientedBox inBox,
uint outSubShapeIndices,
int  inMaxSubShapeIndices 
) const
overridevirtual

Get which sub shape's bounding boxes overlap with an axis aligned box

Parameters
inBoxThe axis aligned box to test against (relative to the center of mass of this shape)
outSubShapeIndicesBuffer where to place the indices of the sub shapes that intersect
inMaxSubShapeIndicesHow many indices will fit in the buffer (normally you'd provide a buffer of GetNumSubShapes() indices)
Returns
How many indices were placed in outSubShapeIndices

Implements CompoundShape.

◆ GetStats()

virtual Stats MutableCompoundShape::GetStats ( ) const
inlineoverridevirtual

Get stats of this shape. Use for logging / data collection purposes only. Does not add values from child shapes, use GetStatsRecursive for this.

Implements Shape.

◆ ModifyShape() [1/2]

void MutableCompoundShape::ModifyShape ( uint  inIndex,
Vec3Arg  inPosition,
QuatArg  inRotation 
)

Modify the position / orientation of a shape. Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.

◆ ModifyShape() [2/2]

void MutableCompoundShape::ModifyShape ( uint  inIndex,
Vec3Arg  inPosition,
QuatArg  inRotation,
const Shape inShape 
)

Modify the position / orientation and shape at the same time. Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.

◆ ModifyShapes()

void MutableCompoundShape::ModifyShapes ( uint  inStartIndex,
uint  inNumber,
const Vec3 inPositions,
const Quat inRotations,
uint  inPositionStride = sizeof(Vec3),
uint  inRotationStride = sizeof(Quat) 
)

Batch set positions / orientations, this avoids duplicate work due to bounding box calculation. Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.

Parameters
inStartIndexIndex of first shape to update
inNumberNumber of shapes to update
inPositionsA list of positions with arbitrary stride
inRotationsA list of orientations with arbitrary stride
inPositionStrideThe position stride (the number of bytes between the first and second element)
inRotationStrideThe orientation stride (the number of bytes between the first and second element)

◆ RemoveShape()

void MutableCompoundShape::RemoveShape ( uint  inIndex)

Remove a shape by index. Beware this can create a race condition if you're running collision queries in parallel. See class documentation for more information.

◆ RestoreBinaryState()

void MutableCompoundShape::RestoreBinaryState ( StreamIn inStream)
overrideprotectedvirtual

This function should not be called directly, it is used by sRestoreFromBinaryState.

Reimplemented from CompoundShape.

◆ SaveBinaryState()

void MutableCompoundShape::SaveBinaryState ( StreamOut inStream) const
overridevirtual

Saves the contents of the shape in binary form to inStream.

Reimplemented from CompoundShape.

◆ sRegister()

void MutableCompoundShape::sRegister ( )
static

◆ WalkSubShapes()

template<class Visitor >
void MutableCompoundShape::WalkSubShapes ( Visitor &  ioVisitor) const
inline

The documentation for this class was generated from the following files: