TriangleShape.h 7.4 KB

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