VehicleCollisionTester.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Body/Body.h>
  6. JPH_NAMESPACE_BEGIN
  7. class PhysicsSystem;
  8. class VehicleConstraint;
  9. class BroadPhaseLayerFilter;
  10. class ObjectLayerFilter;
  11. class BodyFilter;
  12. /// Class that does collision detection between wheels and ground
  13. class VehicleCollisionTester : public RefTarget<VehicleCollisionTester>
  14. {
  15. public:
  16. JPH_OVERRIDE_NEW_DELETE
  17. /// Constructors
  18. VehicleCollisionTester() = default;
  19. explicit VehicleCollisionTester(ObjectLayer inObjectLayer) : mObjectLayer(inObjectLayer) { }
  20. /// Virtual destructor
  21. virtual ~VehicleCollisionTester() = default;
  22. /// Object layer to use for collision detection, this is used when the filters are not overridden
  23. ObjectLayer GetObjectLayer() const { return mObjectLayer; }
  24. void SetObjectLayer(ObjectLayer inObjectLayer) { mObjectLayer = inObjectLayer; }
  25. /// Access to the broad phase layer filter, when set this overrides the object layer supplied in the constructor
  26. void SetBroadPhaseLayerFilter(const BroadPhaseLayerFilter *inFilter) { mBroadPhaseLayerFilter = inFilter; }
  27. const BroadPhaseLayerFilter * GetBroadPhaseLayerFilter() const { return mBroadPhaseLayerFilter; }
  28. /// Access to the object layer filter, when set this overrides the object layer supplied in the constructor
  29. void SetObjectLayerFilter(const ObjectLayerFilter *inFilter) { mObjectLayerFilter = inFilter; }
  30. const ObjectLayerFilter * GetObjectLayerFilter() const { return mObjectLayerFilter; }
  31. /// Access to the body filter, when set this overrides the default filter that filters out the vehicle body
  32. void SetBodyFilter(const BodyFilter *inFilter) { mBodyFilter = inFilter; }
  33. const BodyFilter * GetBodyFilter() const { return mBodyFilter; }
  34. /// Do a collision test with the world
  35. /// @param inPhysicsSystem The physics system that should be tested against
  36. /// @param inVehicleConstraint The vehicle constraint
  37. /// @param inWheelIndex Index of the wheel that we're testing collision for
  38. /// @param inOrigin Origin for the test, corresponds to the world space position for the suspension attachment point
  39. /// @param inDirection Direction for the test (unit vector, world space)
  40. /// @param inVehicleBodyID This body should be filtered out during collision detection to avoid self collisions
  41. /// @param outBody Body that the wheel collided with
  42. /// @param outSubShapeID Sub shape ID that the wheel collided with
  43. /// @param outContactPosition Contact point between wheel and floor, in world space
  44. /// @param outContactNormal Contact normal between wheel and floor, pointing away from the floor
  45. /// @param outSuspensionLength New length of the suspension [0, inSuspensionMaxLength]
  46. /// @return True when collision found, false if not
  47. virtual bool Collide(PhysicsSystem &inPhysicsSystem, const VehicleConstraint &inVehicleConstraint, uint inWheelIndex, RVec3Arg inOrigin, Vec3Arg inDirection, const BodyID &inVehicleBodyID, Body *&outBody, SubShapeID &outSubShapeID, RVec3 &outContactPosition, Vec3 &outContactNormal, float &outSuspensionLength) const = 0;
  48. protected:
  49. const BroadPhaseLayerFilter * mBroadPhaseLayerFilter = nullptr;
  50. const ObjectLayerFilter * mObjectLayerFilter = nullptr;
  51. const BodyFilter * mBodyFilter = nullptr;
  52. ObjectLayer mObjectLayer = cObjectLayerInvalid;
  53. };
  54. /// Collision tester that tests collision using a raycast
  55. class VehicleCollisionTesterRay : public VehicleCollisionTester
  56. {
  57. public:
  58. JPH_OVERRIDE_NEW_DELETE
  59. /// Constructor
  60. /// @param inObjectLayer Object layer to test collision with
  61. /// @param inUp World space up vector, used to avoid colliding with vertical walls.
  62. /// @param inMaxSlopeAngle Max angle (rad) that is considered for colliding wheels. This is to avoid colliding with vertical walls.
  63. VehicleCollisionTesterRay(ObjectLayer inObjectLayer, Vec3Arg inUp = Vec3::sAxisY(), float inMaxSlopeAngle = DegreesToRadians(80.0f)) : VehicleCollisionTester(inObjectLayer), mUp(inUp), mCosMaxSlopeAngle(Cos(inMaxSlopeAngle)) { }
  64. // See: VehicleCollisionTester
  65. virtual bool Collide(PhysicsSystem &inPhysicsSystem, const VehicleConstraint &inVehicleConstraint, uint inWheelIndex, RVec3Arg inOrigin, Vec3Arg inDirection, const BodyID &inVehicleBodyID, Body *&outBody, SubShapeID &outSubShapeID, RVec3 &outContactPosition, Vec3 &outContactNormal, float &outSuspensionLength) const override;
  66. private:
  67. Vec3 mUp;
  68. float mCosMaxSlopeAngle;
  69. };
  70. /// Collision tester that tests collision using a sphere cast
  71. class VehicleCollisionTesterCastSphere : public VehicleCollisionTester
  72. {
  73. public:
  74. JPH_OVERRIDE_NEW_DELETE
  75. /// Constructor
  76. /// @param inObjectLayer Object layer to test collision with
  77. /// @param inUp World space up vector, used to avoid colliding with vertical walls.
  78. /// @param inRadius Radius of sphere
  79. /// @param inMaxSlopeAngle Max angle (rad) that is considered for colliding wheels. This is to avoid colliding with vertical walls.
  80. VehicleCollisionTesterCastSphere(ObjectLayer inObjectLayer, float inRadius, Vec3Arg inUp = Vec3::sAxisY(), float inMaxSlopeAngle = DegreesToRadians(80.0f)) : VehicleCollisionTester(inObjectLayer), mRadius(inRadius), mUp(inUp), mCosMaxSlopeAngle(Cos(inMaxSlopeAngle)) { }
  81. // See: VehicleCollisionTester
  82. virtual bool Collide(PhysicsSystem &inPhysicsSystem, const VehicleConstraint &inVehicleConstraint, uint inWheelIndex, RVec3Arg inOrigin, Vec3Arg inDirection, const BodyID &inVehicleBodyID, Body *&outBody, SubShapeID &outSubShapeID, RVec3 &outContactPosition, Vec3 &outContactNormal, float &outSuspensionLength) const override;
  83. private:
  84. float mRadius;
  85. Vec3 mUp;
  86. float mCosMaxSlopeAngle;
  87. };
  88. /// Collision tester that tests collision using a cylinder shape
  89. class VehicleCollisionTesterCastCylinder : public VehicleCollisionTester
  90. {
  91. public:
  92. JPH_OVERRIDE_NEW_DELETE
  93. /// Constructor
  94. /// @param inObjectLayer Object layer to test collision with
  95. /// @param inConvexRadiusFraction Fraction of half the wheel width (or wheel radius if it is smaller) that is used as the convex radius
  96. VehicleCollisionTesterCastCylinder(ObjectLayer inObjectLayer, float inConvexRadiusFraction = 0.1f) : VehicleCollisionTester(inObjectLayer), mConvexRadiusFraction(inConvexRadiusFraction) { JPH_ASSERT(mConvexRadiusFraction >= 0.0f && mConvexRadiusFraction <= 1.0f); }
  97. // See: VehicleCollisionTester
  98. virtual bool Collide(PhysicsSystem &inPhysicsSystem, const VehicleConstraint &inVehicleConstraint, uint inWheelIndex, RVec3Arg inOrigin, Vec3Arg inDirection, const BodyID &inVehicleBodyID, Body *&outBody, SubShapeID &outSubShapeID, RVec3 &outContactPosition, Vec3 &outContactNormal, float &outSuspensionLength) const override;
  99. private:
  100. float mConvexRadiusFraction;
  101. };
  102. JPH_NAMESPACE_END