BoxShape.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/Shape/ConvexShape.h>
  5. #include <Jolt/Physics/PhysicsSettings.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Class that constructs a BoxShape
  8. class BoxShapeSettings final : public ConvexShapeSettings
  9. {
  10. public:
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(BoxShapeSettings)
  12. /// Default constructor for deserialization
  13. BoxShapeSettings() = default;
  14. /// Create a box with half edge length inHalfExtent and convex radius inConvexRadius.
  15. /// (internally the convex radius will be subtracted from the half extent so the total box will not grow with the convex radius).
  16. BoxShapeSettings(Vec3Arg inHalfExtent, float inConvexRadius = cDefaultConvexRadius, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mHalfExtent(inHalfExtent), mConvexRadius(inConvexRadius) { }
  17. // See: ShapeSettings
  18. virtual ShapeResult Create() const override;
  19. Vec3 mHalfExtent = Vec3::sZero(); ///< Half the size of the box (including convex radius)
  20. float mConvexRadius = 0.0f;
  21. };
  22. /// A box, centered around the origin
  23. class BoxShape final : public ConvexShape
  24. {
  25. public:
  26. JPH_OVERRIDE_NEW_DELETE
  27. /// Constructor
  28. BoxShape() : ConvexShape(EShapeSubType::Box) { }
  29. BoxShape(const BoxShapeSettings &inSettings, ShapeResult &outResult);
  30. /// Create a box with half edge length inHalfExtent and convex radius inConvexRadius.
  31. /// (internally the convex radius will be subtracted from the half extent so the total box will not grow with the convex radius).
  32. 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); }
  33. /// Get half extent of box
  34. Vec3 GetHalfExtent() const { return mHalfExtent; }
  35. // See Shape::GetLocalBounds
  36. virtual AABox GetLocalBounds() const override { return AABox(-mHalfExtent, mHalfExtent); }
  37. // See Shape::GetInnerRadius
  38. virtual float GetInnerRadius() const override { return mHalfExtent.ReduceMin(); }
  39. // See Shape::GetMassProperties
  40. virtual MassProperties GetMassProperties() const override;
  41. // See Shape::GetSurfaceNormal
  42. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  43. // See Shape::GetSupportingFace
  44. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  45. // See ConvexShape::GetSupportFunction
  46. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) 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 ShapeFilter &inShapeFilter = { }) const override;
  54. // See: Shape::CollidePoint
  55. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  56. // See Shape::GetTrianglesStart
  57. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  58. // See Shape::GetTrianglesNext
  59. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  60. // See Shape
  61. virtual void SaveBinaryState(StreamOut &inStream) const override;
  62. // See Shape::GetStats
  63. virtual Stats GetStats() const override { return Stats(sizeof(*this), 12); }
  64. // See Shape::GetVolume
  65. virtual float GetVolume() const override { return GetLocalBounds().GetVolume(); }
  66. /// Get the convex radius of this box
  67. float GetConvexRadius() const { return mConvexRadius; }
  68. // Register shape functions with the registry
  69. static void sRegister();
  70. protected:
  71. // See: Shape::RestoreBinaryState
  72. virtual void RestoreBinaryState(StreamIn &inStream) override;
  73. private:
  74. // Class for GetSupportFunction
  75. class Box;
  76. Vec3 mHalfExtent = Vec3::sZero(); ///< Half the size of the box (including convex radius)
  77. float mConvexRadius = 0.0f;
  78. };
  79. JPH_NAMESPACE_END