Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Vehicle/VehicleCollisionTester.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Body/Body.h>7#include <Jolt/Core/NonCopyable.h>89JPH_NAMESPACE_BEGIN1011class PhysicsSystem;12class VehicleConstraint;13class BroadPhaseLayerFilter;14class ObjectLayerFilter;15class BodyFilter;1617/// Class that does collision detection between wheels and ground18class JPH_EXPORT VehicleCollisionTester : public RefTarget<VehicleCollisionTester>, public NonCopyable19{20public:21JPH_OVERRIDE_NEW_DELETE2223/// Constructors24VehicleCollisionTester() = default;25explicit VehicleCollisionTester(ObjectLayer inObjectLayer) : mObjectLayer(inObjectLayer) { }2627/// Virtual destructor28virtual ~VehicleCollisionTester() = default;2930/// Object layer to use for collision detection, this is used when the filters are not overridden31ObjectLayer GetObjectLayer() const { return mObjectLayer; }32void SetObjectLayer(ObjectLayer inObjectLayer) { mObjectLayer = inObjectLayer; }3334/// Access to the broad phase layer filter, when set this overrides the object layer supplied in the constructor35void SetBroadPhaseLayerFilter(const BroadPhaseLayerFilter *inFilter) { mBroadPhaseLayerFilter = inFilter; }36const BroadPhaseLayerFilter * GetBroadPhaseLayerFilter() const { return mBroadPhaseLayerFilter; }3738/// Access to the object layer filter, when set this overrides the object layer supplied in the constructor39void SetObjectLayerFilter(const ObjectLayerFilter *inFilter) { mObjectLayerFilter = inFilter; }40const ObjectLayerFilter * GetObjectLayerFilter() const { return mObjectLayerFilter; }4142/// Access to the body filter, when set this overrides the default filter that filters out the vehicle body43void SetBodyFilter(const BodyFilter *inFilter) { mBodyFilter = inFilter; }44const BodyFilter * GetBodyFilter() const { return mBodyFilter; }4546/// Do a collision test with the world47/// @param inPhysicsSystem The physics system that should be tested against48/// @param inVehicleConstraint The vehicle constraint49/// @param inWheelIndex Index of the wheel that we're testing collision for50/// @param inOrigin Origin for the test, corresponds to the world space position for the suspension attachment point51/// @param inDirection Direction for the test (unit vector, world space)52/// @param inVehicleBodyID This body should be filtered out during collision detection to avoid self collisions53/// @param outBody Body that the wheel collided with54/// @param outSubShapeID Sub shape ID that the wheel collided with55/// @param outContactPosition Contact point between wheel and floor, in world space56/// @param outContactNormal Contact normal between wheel and floor, pointing away from the floor57/// @param outSuspensionLength New length of the suspension [0, inSuspensionMaxLength]58/// @return True when collision found, false if not59virtual 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;6061/// Do a cheap contact properties prediction based on the contact properties from the last collision test (provided as input parameters)62/// @param inPhysicsSystem The physics system that should be tested against63/// @param inVehicleConstraint The vehicle constraint64/// @param inWheelIndex Index of the wheel that we're testing collision for65/// @param inOrigin Origin for the test, corresponds to the world space position for the suspension attachment point66/// @param inDirection Direction for the test (unit vector, world space)67/// @param inVehicleBodyID The body ID for the vehicle itself68/// @param ioBody Body that the wheel previously collided with69/// @param ioSubShapeID Sub shape ID that the wheel collided with during the last check70/// @param ioContactPosition Contact point between wheel and floor during the last check, in world space71/// @param ioContactNormal Contact normal between wheel and floor during the last check, pointing away from the floor72/// @param ioSuspensionLength New length of the suspension [0, inSuspensionMaxLength]73virtual 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;7475protected:76const BroadPhaseLayerFilter * mBroadPhaseLayerFilter = nullptr;77const ObjectLayerFilter * mObjectLayerFilter = nullptr;78const BodyFilter * mBodyFilter = nullptr;79ObjectLayer mObjectLayer = cObjectLayerInvalid;80};8182/// Collision tester that tests collision using a raycast83class JPH_EXPORT VehicleCollisionTesterRay : public VehicleCollisionTester84{85public:86JPH_OVERRIDE_NEW_DELETE8788/// Constructor89/// @param inObjectLayer Object layer to test collision with90/// @param inUp World space up vector, used to avoid colliding with vertical walls.91/// @param inMaxSlopeAngle Max angle (rad) that is considered for colliding wheels. This is to avoid colliding with vertical walls.92VehicleCollisionTesterRay(ObjectLayer inObjectLayer, Vec3Arg inUp = Vec3::sAxisY(), float inMaxSlopeAngle = DegreesToRadians(80.0f)) : VehicleCollisionTester(inObjectLayer), mUp(inUp), mCosMaxSlopeAngle(Cos(inMaxSlopeAngle)) { }9394// See: VehicleCollisionTester95virtual 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;96virtual 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;9798private:99Vec3 mUp;100float mCosMaxSlopeAngle;101};102103/// Collision tester that tests collision using a sphere cast104class JPH_EXPORT VehicleCollisionTesterCastSphere : public VehicleCollisionTester105{106public:107JPH_OVERRIDE_NEW_DELETE108109/// Constructor110/// @param inObjectLayer Object layer to test collision with111/// @param inUp World space up vector, used to avoid colliding with vertical walls.112/// @param inRadius Radius of sphere113/// @param inMaxSlopeAngle Max angle (rad) that is considered for colliding wheels. This is to avoid colliding with vertical walls.114VehicleCollisionTesterCastSphere(ObjectLayer inObjectLayer, float inRadius, Vec3Arg inUp = Vec3::sAxisY(), float inMaxSlopeAngle = DegreesToRadians(80.0f)) : VehicleCollisionTester(inObjectLayer), mRadius(inRadius), mUp(inUp), mCosMaxSlopeAngle(Cos(inMaxSlopeAngle)) { }115116// See: VehicleCollisionTester117virtual 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;118virtual 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;119120private:121float mRadius;122Vec3 mUp;123float mCosMaxSlopeAngle;124};125126/// Collision tester that tests collision using a cylinder shape127class JPH_EXPORT VehicleCollisionTesterCastCylinder : public VehicleCollisionTester128{129public:130JPH_OVERRIDE_NEW_DELETE131132/// Constructor133/// @param inObjectLayer Object layer to test collision with134/// @param inConvexRadiusFraction Fraction of half the wheel width (or wheel radius if it is smaller) that is used as the convex radius135VehicleCollisionTesterCastCylinder(ObjectLayer inObjectLayer, float inConvexRadiusFraction = 0.1f) : VehicleCollisionTester(inObjectLayer), mConvexRadiusFraction(inConvexRadiusFraction) { JPH_ASSERT(mConvexRadiusFraction >= 0.0f && mConvexRadiusFraction <= 1.0f); }136137// See: VehicleCollisionTester138virtual 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;139virtual 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;140141private:142float mConvexRadiusFraction;143};144145JPH_NAMESPACE_END146147148