VehicleCollisionTester.h 9.3 KB

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