Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/CapsuleShape.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/ConvexShape.h>78JPH_NAMESPACE_BEGIN910/// Class that constructs a CapsuleShape11class JPH_EXPORT CapsuleShapeSettings final : public ConvexShapeSettings12{13JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, CapsuleShapeSettings)1415public:16/// Default constructor for deserialization17CapsuleShapeSettings() = default;1819/// Create a capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfCylinder, 0) and the other at (0, inHalfHeightOfCylinder, 0)20CapsuleShapeSettings(float inHalfHeightOfCylinder, float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mRadius(inRadius), mHalfHeightOfCylinder(inHalfHeightOfCylinder) { }2122/// Check if this is a valid capsule shape23bool IsValid() const { return mRadius > 0.0f && mHalfHeightOfCylinder >= 0.0f; }2425/// Checks if the settings of this capsule make this shape a sphere26bool IsSphere() const { return mHalfHeightOfCylinder == 0.0f; }2728// See: ShapeSettings29virtual ShapeResult Create() const override;3031float mRadius = 0.0f;32float mHalfHeightOfCylinder = 0.0f;33};3435/// A capsule, implemented as a line segment with convex radius36class JPH_EXPORT CapsuleShape final : public ConvexShape37{38public:39JPH_OVERRIDE_NEW_DELETE4041/// Constructor42CapsuleShape() : ConvexShape(EShapeSubType::Capsule) { }43CapsuleShape(const CapsuleShapeSettings &inSettings, ShapeResult &outResult);4445/// Create a capsule centered around the origin with one sphere cap at (0, -inHalfHeightOfCylinder, 0) and the other at (0, inHalfHeightOfCylinder, 0)46CapsuleShape(float inHalfHeightOfCylinder, float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShape(EShapeSubType::Capsule, inMaterial), mRadius(inRadius), mHalfHeightOfCylinder(inHalfHeightOfCylinder) { JPH_ASSERT(inHalfHeightOfCylinder > 0.0f); JPH_ASSERT(inRadius > 0.0f); }4748/// Radius of the cylinder49float GetRadius() const { return mRadius; }5051/// Get half of the height of the cylinder52float GetHalfHeightOfCylinder() const { return mHalfHeightOfCylinder; }5354// See Shape::GetLocalBounds55virtual AABox GetLocalBounds() const override;5657// See Shape::GetWorldSpaceBounds58virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;59using Shape::GetWorldSpaceBounds;6061// See Shape::GetInnerRadius62virtual float GetInnerRadius() const override { return mRadius; }6364// See Shape::GetMassProperties65virtual MassProperties GetMassProperties() const override;6667// See Shape::GetSurfaceNormal68virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;6970// See Shape::GetSupportingFace71virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;7273// See ConvexShape::GetSupportFunction74virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;7576#ifdef JPH_DEBUG_RENDERER77// See Shape::Draw78virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;79#endif // JPH_DEBUG_RENDERER8081// See Shape::CastRay82using ConvexShape::CastRay;83virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;8485// See: Shape::CollidePoint86virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;8788// See: Shape::CollideSoftBodyVertices89virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;9091// See Shape::GetTrianglesStart92virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;9394// See Shape::GetTrianglesNext95virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;9697// See Shape98virtual void SaveBinaryState(StreamOut &inStream) const override;99100// See Shape::GetStats101virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }102103// See Shape::GetVolume104virtual float GetVolume() const override { return 4.0f / 3.0f * JPH_PI * Cubed(mRadius) + 2.0f * JPH_PI * mHalfHeightOfCylinder * Square(mRadius); }105106// See Shape::IsValidScale107virtual bool IsValidScale(Vec3Arg inScale) const override;108109// See Shape::MakeScaleValid110virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;111112// Register shape functions with the registry113static void sRegister();114115protected:116// See: Shape::RestoreBinaryState117virtual void RestoreBinaryState(StreamIn &inStream) override;118119private:120// Classes for GetSupportFunction121class CapsuleNoConvex;122class CapsuleWithConvex;123124float mRadius = 0.0f;125float mHalfHeightOfCylinder = 0.0f;126};127128JPH_NAMESPACE_END129130131