SphereShape.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Collision/Shape/ConvexShape.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Class that constructs a SphereShape
  8. class SphereShapeSettings final : public ConvexShapeSettings
  9. {
  10. public:
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(SphereShapeSettings)
  12. /// Default constructor for deserialization
  13. SphereShapeSettings() = default;
  14. /// Create a sphere with radius inRadius
  15. SphereShapeSettings(float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mRadius(inRadius) { }
  16. // See: ShapeSettings
  17. virtual ShapeResult Create() const override;
  18. float mRadius = 0.0f;
  19. };
  20. /// A sphere, centered around the origin.
  21. /// Note that it is implemented as a point with convex radius.
  22. class SphereShape final : public ConvexShape
  23. {
  24. public:
  25. JPH_OVERRIDE_NEW_DELETE
  26. /// Constructor
  27. SphereShape() : ConvexShape(EShapeSubType::Sphere) { }
  28. SphereShape(const SphereShapeSettings &inSettings, ShapeResult &outResult);
  29. /// Create a sphere with radius inRadius
  30. SphereShape(float inRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShape(EShapeSubType::Sphere, inMaterial), mRadius(inRadius) { JPH_ASSERT(inRadius > 0.0f); }
  31. /// Radius of the sphere
  32. float GetRadius() const { return mRadius; }
  33. // See Shape::GetLocalBounds
  34. virtual AABox GetLocalBounds() const override;
  35. // See Shape::GetWorldSpaceBounds
  36. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  37. using Shape::GetWorldSpaceBounds;
  38. // See Shape::GetInnerRadius
  39. virtual float GetInnerRadius() const override { return mRadius; }
  40. // See Shape::GetMassProperties
  41. virtual MassProperties GetMassProperties() const override;
  42. // See Shape::GetSurfaceNormal
  43. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  44. // See Shape::GetSupportingFace
  45. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override { /* Hit is always a single point, no point in returning anything */ }
  46. // See ConvexShape::GetSupportFunction
  47. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  48. // See Shape::GetSubmergedVolume
  49. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;
  50. #ifdef JPH_DEBUG_RENDERER
  51. // See Shape::Draw
  52. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  53. #endif // JPH_DEBUG_RENDERER
  54. // See Shape::CastRay
  55. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  56. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  57. // See: Shape::CollidePoint
  58. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  59. // See Shape::TransformShape
  60. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  61. // See Shape::GetTrianglesStart
  62. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  63. // See Shape::GetTrianglesNext
  64. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  65. // See Shape
  66. virtual void SaveBinaryState(StreamOut &inStream) const override;
  67. // See Shape::GetStats
  68. virtual Stats GetStats() const override { return Stats(sizeof(*this), 0); }
  69. // See Shape::GetVolume
  70. virtual float GetVolume() const override { return 4.0f / 3.0f * JPH_PI * Cubed(mRadius); }
  71. // See Shape::IsValidScale
  72. virtual bool IsValidScale(Vec3Arg inScale) const override;
  73. // Register shape functions with the registry
  74. static void sRegister();
  75. protected:
  76. // See: Shape::RestoreBinaryState
  77. virtual void RestoreBinaryState(StreamIn &inStream) override;
  78. private:
  79. // Get the radius of this sphere scaled by inScale
  80. inline float GetScaledRadius(Vec3Arg inScale) const;
  81. // Classes for GetSupportFunction
  82. class SphereNoConvex;
  83. class SphereWithConvex;
  84. float mRadius = 0.0f;
  85. };
  86. JPH_NAMESPACE_END