TriangleShape.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 TriangleShape
  7. class TriangleShapeSettings final : public ConvexShapeSettings
  8. {
  9. public:
  10. JPH_DECLARE_SERIALIZABLE_VIRTUAL(TriangleShapeSettings)
  11. /// Default constructor for deserialization
  12. TriangleShapeSettings() = default;
  13. /// Create a triangle with points (inV1, inV2, inV3) (counter clockwise) and convex radius inConvexRadius.
  14. /// Note that the convex radius is currently only used for shape vs shape collision, for all other purposes the triangle is infinitely thin.
  15. TriangleShapeSettings(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, float inConvexRadius = 0.0f, const PhysicsMaterial *inMaterial = nullptr) : ConvexShapeSettings(inMaterial), mV1(inV1), mV2(inV2), mV3(inV3), mConvexRadius(inConvexRadius) { }
  16. // See: ShapeSettings
  17. virtual ShapeResult Create() const override;
  18. Vec3 mV1;
  19. Vec3 mV2;
  20. Vec3 mV3;
  21. float mConvexRadius = 0.0f;
  22. };
  23. /// A single triangle, not the most efficient way of creating a world filled with triangles but can be used as a query shape for example.
  24. class TriangleShape final : public ConvexShape
  25. {
  26. public:
  27. /// Constructor
  28. TriangleShape() : ConvexShape(EShapeSubType::Triangle) { }
  29. TriangleShape(const TriangleShapeSettings &inSettings, ShapeResult &outResult);
  30. /// Create a triangle with points (inV1, inV2, inV3) (counter clockwise) and convex radius inConvexRadius.
  31. /// Note that the convex radius is currently only used for shape vs shape collision, for all other purposes the triangle is infinitely thin.
  32. TriangleShape(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, float inConvexRadius = 0.0f, const PhysicsMaterial *inMaterial = nullptr) : ConvexShape(EShapeSubType::Triangle, inMaterial), mV1(inV1), mV2(inV2), mV3(inV3), mConvexRadius(inConvexRadius) { JPH_ASSERT(inConvexRadius >= 0.0f); }
  33. /// Convex radius
  34. float GetConvexRadius() const { return mConvexRadius; }
  35. // See Shape::GetLocalBounds
  36. virtual AABox GetLocalBounds() const override;
  37. // See Shape::GetWorldSpaceBounds
  38. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  39. // See Shape::GetInnerRadius
  40. virtual float GetInnerRadius() const override { return mConvexRadius; }
  41. // See Shape::GetMassProperties
  42. virtual MassProperties GetMassProperties() const override;
  43. // See Shape::GetSurfaceNormal
  44. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  45. // See ConvexShape::GetSupportFunction
  46. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  47. // See ConvexShape::GetSupportingFace
  48. virtual void GetSupportingFace(Vec3Arg inDirection, Vec3Arg inScale, SupportingFace &outVertices) const override;
  49. // See Shape::GetSubmergedVolume
  50. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy) const override;
  51. #ifdef JPH_DEBUG_RENDERER
  52. // See Shape::Draw
  53. virtual void Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  54. #endif // JPH_DEBUG_RENDERER
  55. // See Shape::CastRay
  56. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  57. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) const override;
  58. // See: Shape::CollidePoint
  59. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const override;
  60. // See Shape::TransformShape
  61. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  62. // See Shape::GetTrianglesStart
  63. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  64. // See Shape::GetTrianglesNext
  65. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  66. // See Shape
  67. virtual void SaveBinaryState(StreamOut &inStream) const override;
  68. // See Shape::GetStats
  69. virtual Stats GetStats() const override { return Stats(sizeof(*this), 1); }
  70. // See Shape::GetVolume
  71. virtual float GetVolume() const override { return 0; }
  72. // See Shape::IsValidScale
  73. virtual bool IsValidScale(Vec3Arg inScale) const override;
  74. // Register shape functions with the registry
  75. static void sRegister();
  76. protected:
  77. // See: Shape::RestoreBinaryState
  78. virtual void RestoreBinaryState(StreamIn &inStream) override;
  79. private:
  80. // Helper functions called by CollisionDispatch
  81. static void sCollideConvexVsTriangle(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector);
  82. static void sCollideSphereVsTriangle(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector);
  83. static void sCastConvexVsTriangle(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  84. static void sCastSphereVsTriangle(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  85. // Context for GetTrianglesStart/Next
  86. class TSGetTrianglesContext;
  87. // Classes for GetSupportFunction
  88. class TriangleNoConvex;
  89. class TriangleWithConvex;
  90. Vec3 mV1;
  91. Vec3 mV2;
  92. Vec3 mV3;
  93. float mConvexRadius = 0.0f;
  94. };
  95. } // JPH