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