Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/PlaneShape.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2024 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Collision/Shape/Shape.h>7#include <Jolt/Physics/Collision/Shape/SubShapeID.h>8#include <Jolt/Physics/Collision/PhysicsMaterial.h>910JPH_NAMESPACE_BEGIN1112class CollideShapeSettings;1314/// Class that constructs a PlaneShape15class JPH_EXPORT PlaneShapeSettings final : public ShapeSettings16{17JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PlaneShapeSettings)1819public:20/// Default constructor for deserialization21PlaneShapeSettings() = default;2223/// Create a plane shape.24PlaneShapeSettings(const Plane &inPlane, const PhysicsMaterial *inMaterial = nullptr, float inHalfExtent = cDefaultHalfExtent) : mPlane(inPlane), mMaterial(inMaterial), mHalfExtent(inHalfExtent) { }2526// See: ShapeSettings27virtual ShapeResult Create() const override;2829Plane mPlane; ///< Plane that describes the shape. The negative half space is considered solid.3031RefConst<PhysicsMaterial> mMaterial; ///< Surface material of the plane3233static constexpr float cDefaultHalfExtent = 1000.0f; ///< Default half-extent of the plane (total size along 1 axis will be 2 * half-extent)3435float mHalfExtent = cDefaultHalfExtent; ///< The bounding box of this plane will run from [-half_extent, half_extent]. Keep this as low as possible for better broad phase performance.36};3738/// A plane shape. The negative half space is considered solid. Planes cannot be dynamic objects, only static or kinematic.39/// The plane is considered an infinite shape, but testing collision outside of its bounding box (defined by the half-extent parameter) will not return a collision result.40/// At the edge of the bounding box collision with the plane will be inconsistent. If you need something of a well defined size, a box shape may be better.41class JPH_EXPORT PlaneShape final : public Shape42{43public:44JPH_OVERRIDE_NEW_DELETE4546/// Constructor47PlaneShape() : Shape(EShapeType::Plane, EShapeSubType::Plane) { }48PlaneShape(const Plane &inPlane, const PhysicsMaterial *inMaterial = nullptr, float inHalfExtent = PlaneShapeSettings::cDefaultHalfExtent) : Shape(EShapeType::Plane, EShapeSubType::Plane), mPlane(inPlane), mMaterial(inMaterial), mHalfExtent(inHalfExtent) { CalculateLocalBounds(); }49PlaneShape(const PlaneShapeSettings &inSettings, ShapeResult &outResult);5051/// Get the plane52const Plane & GetPlane() const { return mPlane; }5354/// Get the half-extent of the bounding box of the plane55float GetHalfExtent() const { return mHalfExtent; }5657// See Shape::MustBeStatic58virtual bool MustBeStatic() const override { return true; }5960// See Shape::GetLocalBounds61virtual AABox GetLocalBounds() const override { return mLocalBounds; }6263// See Shape::GetSubShapeIDBitsRecursive64virtual uint GetSubShapeIDBitsRecursive() const override { return 0; }6566// See Shape::GetInnerRadius67virtual float GetInnerRadius() const override { return 0.0f; }6869// See Shape::GetMassProperties70virtual MassProperties GetMassProperties() const override;7172// See Shape::GetMaterial73virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return GetMaterial(); }7475// See Shape::GetSurfaceNormal76virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override { JPH_ASSERT(inSubShapeID.IsEmpty(), "Invalid subshape ID"); return mPlane.GetNormal(); }7778// See Shape::GetSupportingFace79virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;8081#ifdef JPH_DEBUG_RENDERER82// See Shape::Draw83virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;84#endif // JPH_DEBUG_RENDERER8586// See Shape::CastRay87virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;88virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;8990// See: Shape::CollidePoint91virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;9293// See: Shape::CollideSoftBodyVertices94virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;9596// See Shape::GetTrianglesStart97virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;9899// See Shape::GetTrianglesNext100virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;101102// See Shape::GetSubmergedVolume103virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override { JPH_ASSERT(false, "Not supported"); }104105// See Shape106virtual void SaveBinaryState(StreamOut &inStream) const override;107virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;108virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;109110// See Shape::GetStats111virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }112113// See Shape::GetVolume114virtual float GetVolume() const override { return 0; }115116/// Material of the shape117void SetMaterial(const PhysicsMaterial *inMaterial) { mMaterial = inMaterial; }118const PhysicsMaterial * GetMaterial() const { return mMaterial != nullptr? mMaterial : PhysicsMaterial::sDefault; }119120// Register shape functions with the registry121static void sRegister();122123protected:124// See: Shape::RestoreBinaryState125virtual void RestoreBinaryState(StreamIn &inStream) override;126127private:128struct PSGetTrianglesContext; ///< Context class for GetTrianglesStart/Next129130// Get 4 vertices that form the plane131void GetVertices(Vec3 *outVertices) const;132133// Cache the local bounds134void CalculateLocalBounds();135136// Helper functions called by CollisionDispatch137static void sCollideConvexVsPlane(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);138static void sCastConvexVsPlane(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);139140Plane mPlane;141RefConst<PhysicsMaterial> mMaterial;142float mHalfExtent;143AABox mLocalBounds;144};145146JPH_NAMESPACE_END147148149