VehicleCollisionTester.h 3.9 KB

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