TriangleShape.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/Shape/ConvexShape.h>
  5. JPH_NAMESPACE_BEGIN
  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. JPH_OVERRIDE_NEW_DELETE
  28. /// Constructor
  29. TriangleShape() : ConvexShape(EShapeSubType::Triangle) { }
  30. TriangleShape(const TriangleShapeSettings &inSettings, ShapeResult &outResult);
  31. /// Create a triangle with points (inV1, inV2, inV3) (counter clockwise) and convex radius inConvexRadius.
  32. /// Note that the convex radius is currently only used for shape vs shape collision, for all other purposes the triangle is infinitely thin.
  33. 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); }
  34. /// Convex radius
  35. float GetConvexRadius() const { return mConvexRadius; }
  36. // See Shape::GetLocalBounds
  37. virtual AABox GetLocalBounds() const override;
  38. // See Shape::GetWorldSpaceBounds
  39. virtual AABox GetWorldSpaceBounds(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) const override;
  40. using Shape::GetWorldSpaceBounds;
  41. // See Shape::GetInnerRadius
  42. virtual float GetInnerRadius() const override { return mConvexRadius; }
  43. // See Shape::GetMassProperties
  44. virtual MassProperties GetMassProperties() const override;
  45. // See Shape::GetSurfaceNormal
  46. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  47. // See Shape::GetSupportingFace
  48. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  49. // See ConvexShape::GetSupportFunction
  50. virtual const Support * GetSupportFunction(ESupportMode inMode, SupportBuffer &inBuffer, Vec3Arg inScale) const override;
  51. // See Shape::GetSubmergedVolume
  52. virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override;
  53. #ifdef JPH_DEBUG_RENDERER
  54. // See Shape::Draw
  55. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  56. #endif // JPH_DEBUG_RENDERER
  57. // See Shape::CastRay
  58. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  59. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  60. // See: Shape::CollidePoint
  61. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  62. // See Shape::TransformShape
  63. virtual void TransformShape(Mat44Arg inCenterOfMassTransform, TransformedShapeCollector &ioCollector) const override;
  64. // See Shape::GetTrianglesStart
  65. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  66. // See Shape::GetTrianglesNext
  67. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  68. // See Shape
  69. virtual void SaveBinaryState(StreamOut &inStream) const override;
  70. // See Shape::GetStats
  71. virtual Stats GetStats() const override { return Stats(sizeof(*this), 1); }
  72. // See Shape::GetVolume
  73. virtual float GetVolume() const override { return 0; }
  74. // See Shape::IsValidScale
  75. virtual bool IsValidScale(Vec3Arg inScale) const override;
  76. // Register shape functions with the registry
  77. static void sRegister();
  78. protected:
  79. // See: Shape::RestoreBinaryState
  80. virtual void RestoreBinaryState(StreamIn &inStream) override;
  81. private:
  82. // Helper functions called by CollisionDispatch
  83. 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, const ShapeFilter &inShapeFilter);
  84. 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, const ShapeFilter &inShapeFilter);
  85. 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);
  86. 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);
  87. // Context for GetTrianglesStart/Next
  88. class TSGetTrianglesContext;
  89. // Classes for GetSupportFunction
  90. class TriangleNoConvex;
  91. class TriangleWithConvex;
  92. Vec3 mV1;
  93. Vec3 mV2;
  94. Vec3 mV3;
  95. float mConvexRadius = 0.0f;
  96. };
  97. JPH_NAMESPACE_END