VehicleCollisionTester.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Body/Body.h>
  5. JPH_NAMESPACE_BEGIN
  6. class PhysicsSystem;
  7. /// Class that does collision detection between wheels and ground
  8. class VehicleCollisionTester : public RefTarget<VehicleCollisionTester>
  9. {
  10. public:
  11. JPH_OVERRIDE_NEW_DELETE
  12. /// Virtual destructor
  13. virtual ~VehicleCollisionTester() = default;
  14. /// Do a collision test with the world
  15. /// @param inPhysicsSystem The physics system that should be tested against
  16. /// @param inWheelIndex Index of the wheel that we're testing collision for
  17. /// @param inOrigin Origin for the test, corresponds to the world space position for the suspension attachment point
  18. /// @param inDirection Direction for the test (unit vector, world space)
  19. /// @param inSuspensionMaxLength Length of the suspension at max droop (m)
  20. /// @param inVehicleBodyID This body should be filtered out during collision detection to avoid self collisions
  21. /// @param outBody Body that the wheel collided with
  22. /// @param outSubShapeID Sub shape ID that the wheel collided with
  23. /// @param outContactPosition Contact point between wheel and floor, in world space
  24. /// @param outContactNormal Contact normal between wheel and floor, pointing away from the floor
  25. /// @param outSuspensionLength New length of the suspension [0, inSuspensionMaxLength]
  26. /// @return True when collision found, false if not
  27. virtual bool Collide(PhysicsSystem &inPhysicsSystem, uint inWheelIndex, Vec3Arg inOrigin, Vec3Arg inDirection, float inSuspensionMaxLength, const BodyID &inVehicleBodyID, Body *&outBody, SubShapeID &outSubShapeID, Vec3 &outContactPosition, Vec3 &outContactNormal, float &outSuspensionLength) const = 0;
  28. };
  29. /// Collision tester that tests collision using a raycast
  30. class VehicleCollisionTesterRay : public VehicleCollisionTester
  31. {
  32. public:
  33. JPH_OVERRIDE_NEW_DELETE
  34. /// Constructor
  35. /// @param inObjectLayer Object layer to test collision with
  36. /// @param inUp World space up vector, used to avoid colliding with vertical walls.
  37. /// @param inMaxSlopeAngle Max angle (rad) that is considered for colliding wheels. This is to avoid colliding with vertical walls.
  38. VehicleCollisionTesterRay(ObjectLayer inObjectLayer, Vec3Arg inUp = Vec3::sAxisY(), float inMaxSlopeAngle = DegreesToRadians(80.0f)) : mObjectLayer(inObjectLayer), mUp(inUp), mCosMaxSlopeAngle(Cos(inMaxSlopeAngle)) { }
  39. // See: VehicleCollisionTester
  40. virtual bool Collide(PhysicsSystem &inPhysicsSystem, uint inWheelIndex, Vec3Arg inOrigin, Vec3Arg inDirection, float inSuspensionMaxLength, const BodyID &inVehicleBodyID, Body *&outBody, SubShapeID &outSubShapeID, Vec3 &outContactPosition, Vec3 &outContactNormal, float &outSuspensionLength) const override;
  41. private:
  42. ObjectLayer mObjectLayer;
  43. Vec3 mUp;
  44. float mCosMaxSlopeAngle;
  45. };
  46. /// Collision tester that tests collision using a sphere cast
  47. class VehicleCollisionTesterCastSphere : public VehicleCollisionTester
  48. {
  49. public:
  50. JPH_OVERRIDE_NEW_DELETE
  51. /// Constructor
  52. /// @param inObjectLayer Object layer to test collision with
  53. /// @param inUp World space up vector, used to avoid colliding with vertical walls.
  54. /// @param inRadius Radius of sphere
  55. /// @param inMaxSlopeAngle Max angle (rad) that is considered for colliding wheels. This is to avoid colliding with vertical walls.
  56. VehicleCollisionTesterCastSphere(ObjectLayer inObjectLayer, float inRadius, Vec3Arg inUp = Vec3::sAxisY(), float inMaxSlopeAngle = DegreesToRadians(80.0f)) : mObjectLayer(inObjectLayer), mRadius(inRadius), mUp(inUp), mCosMaxSlopeAngle(Cos(inMaxSlopeAngle)) { }
  57. // See: VehicleCollisionTester
  58. virtual bool Collide(PhysicsSystem &inPhysicsSystem, uint inWheelIndex, Vec3Arg inOrigin, Vec3Arg inDirection, float inSuspensionMaxLength, const BodyID &inVehicleBodyID, Body *&outBody, SubShapeID &outSubShapeID, Vec3 &outContactPosition, Vec3 &outContactNormal, float &outSuspensionLength) const override;
  59. private:
  60. ObjectLayer mObjectLayer;
  61. float mRadius;
  62. Vec3 mUp;
  63. float mCosMaxSlopeAngle;
  64. };
  65. JPH_NAMESPACE_END