SphereShape.h 4.6 KB

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