BoxShape.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Collision/Shape/ConvexShape.h>
  5. #include <Physics/PhysicsSettings.h>
  6. namespace JPH {
  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. /// Constructor
  27. BoxShape() : ConvexShape(EShapeSubType::Box) { }
  28. BoxShape(const BoxShapeSettings &inSettings, ShapeResult &outResult);
  29. /// Create a box with half edge length inHalfExtent and convex radius inConvexRadius.
  30. /// (internally the convex radius will be subtracted from the half extent so the total box will not grow with the convex radius).
  31. 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); }
  32. /// Get half extent of box
  33. Vec3 GetHalfExtent() const { return mHalfExtent; }
  34. // See Shape::GetLocalBounds
  35. virtual AABox GetLocalBounds() const override { return AABox(-mHalfExtent, mHalfExtent); }
  36. // See Shape::GetInnerRadius
  37. virtual float GetInnerRadius() const override { return mHalfExtent.ReduceMin(); }
  38. // See Shape::GetMassProperties
  39. virtual MassProperties GetMassProperties() const override;
  40. // See Shape::GetSurfaceNormal
  41. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  42. // See ConvexShape::GetSupportFunction
  43. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  44. // See ConvexShape::GetSupportingFace
  45. virtual void GetSupportingFace(Vec3Arg inDirection, Vec3Arg inScale, SupportingFace &outVertices) const override;
  46. #ifdef JPH_DEBUG_RENDERER
  47. // See Shape::Draw
  48. virtual void Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  49. #endif // JPH_DEBUG_RENDERER
  50. // See Shape::CastRay
  51. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  52. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) const override;
  53. // See: Shape::CollidePoint
  54. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const override;
  55. // See Shape::GetTrianglesStart
  56. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  57. // See Shape::GetTrianglesNext
  58. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  59. // See Shape
  60. virtual void SaveBinaryState(StreamOut &inStream) const override;
  61. // See Shape::GetStats
  62. virtual Stats GetStats() const override { return Stats(sizeof(*this), 12); }
  63. // See Shape::GetVolume
  64. virtual float GetVolume() const override { return GetLocalBounds().GetVolume(); }
  65. // Register shape functions with the registry
  66. static void sRegister();
  67. protected:
  68. // See: Shape::RestoreBinaryState
  69. virtual void RestoreBinaryState(StreamIn &inStream) override;
  70. private:
  71. // Class for GetSupportFunction
  72. class Box;
  73. Vec3 mHalfExtent = Vec3::sZero(); ///< Half the size of the box (including convex radius)
  74. float mConvexRadius = 0.0f;
  75. };
  76. } // JPH