BoxShape.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include <Jolt/Physics/PhysicsSettings.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Class that constructs a BoxShape
  9. class BoxShapeSettings final : public ConvexShapeSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(BoxShapeSettings)
  13. /// Default constructor for deserialization
  14. BoxShapeSettings() = default;
  15. /// Create a box with half edge length inHalfExtent and convex radius inConvexRadius.
  16. /// (internally the convex radius will be subtracted from the half extent so the total box will not grow with the convex radius).
  17. BoxShapeSettings(Vec3Arg inHalfExtent, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mHalfExtent(inHalfExtent), mConvexRadius(inConvexRadius) { }
  18. // See: ShapeSettings
  19. virtual ShapeResult Create() const override;
  20. Vec3 mHalfExtent = Vec3::sZero(); ///< Half the size of the box (including convex radius)
  21. float mConvexRadius = 0.0f;
  22. };
  23. /// A box, centered around the origin
  24. class BoxShape final : public ConvexShape
  25. {
  26. public:
  27. JPH_OVERRIDE_NEW_DELETE
  28. /// Constructor
  29. BoxShape() : ConvexShape(EShapeSubType::Box) { }
  30. BoxShape(const BoxShapeSettings &inSettings, ShapeResult &outResult);
  31. /// Create a box with half edge length inHalfExtent and convex radius inConvexRadius.
  32. /// (internally the convex radius will be subtracted from the half extent so the total box will not grow with the convex radius).
  33. BoxShape(Vec3Arg inHalfExtent, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShape(EShapeSubType::Box, inMaterial), mHalfExtent(inHalfExtent), mConvexRadius(inConvexRadius) { JPH_ASSERT(inConvexRadius >= 0.0f); JPH_ASSERT(inHalfExtent.ReduceMin() >= inConvexRadius); }
  34. /// Get half extent of box
  35. Vec3 GetHalfExtent() const { return mHalfExtent; }
  36. // See Shape::GetLocalBounds
  37. virtual AABox GetLocalBounds() const override { return AABox(-mHalfExtent, mHalfExtent); }
  38. // See Shape::GetInnerRadius
  39. virtual float GetInnerRadius() const override { return mHalfExtent.ReduceMin(); }
  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;
  46. // See ConvexShape::GetSupportFunction
  47. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  48. #ifdef JPH_DEBUG_RENDERER
  49. // See Shape::Draw
  50. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  51. #endif // JPH_DEBUG_RENDERER
  52. // See Shape::CastRay
  53. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  54. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  55. // See: Shape::CollidePoint
  56. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  57. // See Shape::GetTrianglesStart
  58. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  59. // See Shape::GetTrianglesNext
  60. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  61. // See Shape
  62. virtual void SaveBinaryState(StreamOut &inStream) const override;
  63. // See Shape::GetStats
  64. virtual Stats GetStats() const override { return Stats(sizeof(*this), 12); }
  65. // See Shape::GetVolume
  66. virtual float GetVolume() const override { return GetLocalBounds().GetVolume(); }
  67. /// Get the convex radius of this box
  68. float GetConvexRadius() const { return mConvexRadius; }
  69. // Register shape functions with the registry
  70. static void sRegister();
  71. protected:
  72. // See: Shape::RestoreBinaryState
  73. virtual void RestoreBinaryState(StreamIn &inStream) override;
  74. private:
  75. // Class for GetSupportFunction
  76. class Box;
  77. Vec3 mHalfExtent = Vec3::sZero(); ///< Half the size of the box (including convex radius)
  78. float mConvexRadius = 0.0f;
  79. };
  80. JPH_NAMESPACE_END