Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/ConvexShape.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/Core/StaticArray.h>7#include <Jolt/Physics/Collision/Shape/Shape.h>8#include <Jolt/Physics/Collision/Shape/SubShapeID.h>9#include <Jolt/Physics/Collision/PhysicsMaterial.h>1011JPH_NAMESPACE_BEGIN1213class CollideShapeSettings;1415/// Class that constructs a ConvexShape (abstract)16class JPH_EXPORT ConvexShapeSettings : public ShapeSettings17{18JPH_DECLARE_SERIALIZABLE_ABSTRACT(JPH_EXPORT, ConvexShapeSettings)1920public:21/// Constructor22ConvexShapeSettings() = default;23explicit ConvexShapeSettings(const PhysicsMaterial *inMaterial) : mMaterial(inMaterial) { }2425/// Set the density of the object in kg / m^326void SetDensity(float inDensity) { mDensity = inDensity; }2728// Properties29RefConst<PhysicsMaterial> mMaterial; ///< Material assigned to this shape30float mDensity = 1000.0f; ///< Uniform density of the interior of the convex object (kg / m^3)31};3233/// Base class for all convex shapes. Defines a virtual interface.34class JPH_EXPORT ConvexShape : public Shape35{36public:37JPH_OVERRIDE_NEW_DELETE3839/// Constructor40explicit ConvexShape(EShapeSubType inSubType) : Shape(EShapeType::Convex, inSubType) { }41ConvexShape(EShapeSubType inSubType, const ConvexShapeSettings &inSettings, ShapeResult &outResult) : Shape(EShapeType::Convex, inSubType, inSettings, outResult), mMaterial(inSettings.mMaterial), mDensity(inSettings.mDensity) { }42ConvexShape(EShapeSubType inSubType, const PhysicsMaterial *inMaterial) : Shape(EShapeType::Convex, inSubType), mMaterial(inMaterial) { }4344// See Shape::GetSubShapeIDBitsRecursive45virtual uint GetSubShapeIDBitsRecursive() const override { return 0; } // Convex shapes don't have sub shapes4647// See Shape::GetMaterial48virtual const PhysicsMaterial * GetMaterial([[maybe_unused]] const SubShapeID &inSubShapeID) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return GetMaterial(); }4950// See Shape::CastRay51virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;52virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;5354// See: Shape::CollidePoint55virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;5657// See Shape::GetTrianglesStart58virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;5960// See Shape::GetTrianglesNext61virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;6263// See Shape::GetSubmergedVolume64virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;6566/// Function that provides an interface for GJK67class Support68{69public:70/// Warning: Virtual destructor will not be called on this object!71virtual ~Support() = default;7273/// Calculate the support vector for this convex shape (includes / excludes the convex radius depending on how this was obtained).74/// Support vector is relative to the center of mass of the shape.75virtual Vec3 GetSupport(Vec3Arg inDirection) const = 0;7677/// Convex radius of shape. Collision detection on penetrating shapes is much more expensive,78/// so you can add a radius around objects to increase the shape. This makes it far less likely that they will actually penetrate.79virtual float GetConvexRadius() const = 0;80};8182/// Buffer to hold a Support object, used to avoid dynamic memory allocations83class alignas(16) SupportBuffer84{85public:86uint8 mData[4160];87};8889/// How the GetSupport function should behave90enum class ESupportMode91{92ExcludeConvexRadius, ///< Return the shape excluding the convex radius, Support::GetConvexRadius will return the convex radius if there is one, but adding this radius may not result in the most accurate/efficient representation of shapes with sharp edges93IncludeConvexRadius, ///< Return the shape including the convex radius, Support::GetSupport includes the convex radius if there is one, Support::GetConvexRadius will return 094Default, ///< Use both Support::GetSupport add Support::GetConvexRadius to get a support point that matches the original shape as accurately/efficiently as possible95};9697/// Returns an object that provides the GetSupport function for this shape.98/// inMode determines if this support function includes or excludes the convex radius.99/// of the values returned by the GetSupport function. This improves numerical accuracy of the results.100/// inScale scales this shape in local space.101virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const = 0;102103/// Material of the shape104void SetMaterial(const PhysicsMaterial *inMaterial) { mMaterial = inMaterial; }105const PhysicsMaterial * GetMaterial() const { return mMaterial != nullptr? mMaterial : PhysicsMaterial::sDefault; }106107/// Set density of the shape (kg / m^3)108void SetDensity(float inDensity) { mDensity = inDensity; }109110/// Get density of the shape (kg / m^3)111float GetDensity() const { return mDensity; }112113#ifdef JPH_DEBUG_RENDERER114// See Shape::DrawGetSupportFunction115virtual void DrawGetSupportFunction(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inDrawSupportDirection) const override;116117// See Shape::DrawGetSupportingFace118virtual void DrawGetSupportingFace(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;119#endif // JPH_DEBUG_RENDERER120121// See Shape122virtual void SaveBinaryState(StreamOut &inStream) const override;123virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;124virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;125126// Register shape functions with the registry127static void sRegister();128129protected:130// See: Shape::RestoreBinaryState131virtual void RestoreBinaryState(StreamIn &inStream) override;132133/// Vertex list that forms a unit sphere134static const StaticArray<Vec3, 384> sUnitSphereTriangles;135136private:137// Class for GetTrianglesStart/Next138class CSGetTrianglesContext;139140// Helper functions called by CollisionDispatch141static void sCollideConvexVsConvex(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);142static void sCastConvexVsConvex(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);143144// Properties145RefConst<PhysicsMaterial> mMaterial; ///< Material assigned to this shape146float mDensity = 1000.0f; ///< Uniform density of the interior of the convex object (kg / m^3)147};148149JPH_NAMESPACE_END150151152