Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/Shape/SphereShape.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 SphereShape11class JPH_EXPORT SphereShapeSettings final : public ConvexShapeSettings12{13JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, SphereShapeSettings)1415public:16/// Default constructor for deserialization17SphereShapeSettings() = default;1819/// Create a sphere with radius inRadius20SphereShapeSettings(float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mRadius(inRadius) { }2122// See: ShapeSettings23virtual ShapeResult Create() const override;2425float mRadius = 0.0f;26};2728/// A sphere, centered around the origin.29/// Note that it is implemented as a point with convex radius.30class JPH_EXPORT SphereShape final : public ConvexShape31{32public:33JPH_OVERRIDE_NEW_DELETE3435/// Constructor36SphereShape() : ConvexShape(EShapeSubType::Sphere) { }37SphereShape(const SphereShapeSettings &inSettings, ShapeResult &outResult);3839/// Create a sphere with radius inRadius40SphereShape(float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShape(EShapeSubType::Sphere, inMaterial), mRadius(inRadius) { JPH_ASSERT(inRadius > 0.0f); }4142/// Radius of the sphere43float GetRadius() const { return mRadius; }4445// See Shape::GetLocalBounds46virtual AABox GetLocalBounds() const override;4748// See Shape::GetWorldSpaceBounds49virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;50using Shape::GetWorldSpaceBounds;5152// See Shape::GetInnerRadius53virtual float GetInnerRadius() const override { return mRadius; }5455// See Shape::GetMassProperties56virtual MassProperties GetMassProperties() const override;5758// See Shape::GetSurfaceNormal59virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;6061// See Shape::GetSupportingFace62virtual void GetSupportingFace([[maybe_unused]] const SubShapeID &inSubShapeID, [[maybe_unused]] Vec3Arg inDirection, [[maybe_unused]] Vec3Arg inScale, [[maybe_unused]] Mat44Arg inCenterOfMassTransform, [[maybe_unused]] SupportingFace &outVertices) const override { /* Hit is always a single point, no point in returning anything */ }6364// See ConvexShape::GetSupportFunction65virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;6667// See Shape::GetSubmergedVolume68virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;6970#ifdef JPH_DEBUG_RENDERER71// See Shape::Draw72virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;73#endif // JPH_DEBUG_RENDERER7475// See Shape::CastRay76virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;77virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;7879// See: Shape::CollidePoint80virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;8182// See: Shape::CollideSoftBodyVertices83virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override;8485// See Shape::GetTrianglesStart86virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;8788// See Shape::GetTrianglesNext89virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;9091// See Shape92virtual void SaveBinaryState(StreamOut &inStream) const override;9394// See Shape::GetStats95virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }9697// See Shape::GetVolume98virtual float GetVolume() const override { return 4.0f / 3.0f * JPH_PI * Cubed(mRadius); }99100// See Shape::IsValidScale101virtual bool IsValidScale(Vec3Arg inScale) const override;102103// See Shape::MakeScaleValid104virtual Vec3 MakeScaleValid(Vec3Arg inScale) const override;105106// Register shape functions with the registry107static void sRegister();108109protected:110// See: Shape::RestoreBinaryState111virtual void RestoreBinaryState(StreamIn &inStream) override;112113private:114// Get the radius of this sphere scaled by inScale115inline float GetScaledRadius(Vec3Arg inScale) const;116117// Classes for GetSupportFunction118class SphereNoConvex;119class SphereWithConvex;120121float mRadius = 0.0f;122};123124JPH_NAMESPACE_END125126127