Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Vehicle/TrackedVehicleController.h
9913 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/Vehicle/VehicleConstraint.h>
8
#include <Jolt/Physics/Vehicle/VehicleController.h>
9
#include <Jolt/Physics/Vehicle/VehicleEngine.h>
10
#include <Jolt/Physics/Vehicle/VehicleTransmission.h>
11
#include <Jolt/Physics/Vehicle/VehicleTrack.h>
12
13
JPH_NAMESPACE_BEGIN
14
15
class PhysicsSystem;
16
17
/// WheelSettings object specifically for TrackedVehicleController
18
class JPH_EXPORT WheelSettingsTV : public WheelSettings
19
{
20
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, WheelSettingsTV)
21
22
public:
23
// See: WheelSettings
24
virtual void SaveBinaryState(StreamOut &inStream) const override;
25
virtual void RestoreBinaryState(StreamIn &inStream) override;
26
27
float mLongitudinalFriction = 4.0f; ///< Friction in forward direction of tire
28
float mLateralFriction = 2.0f; ///< Friction in sideways direction of tire
29
};
30
31
/// Wheel object specifically for TrackedVehicleController
32
class JPH_EXPORT WheelTV : public Wheel
33
{
34
public:
35
JPH_OVERRIDE_NEW_DELETE
36
37
/// Constructor
38
explicit WheelTV(const WheelSettingsTV &inWheel);
39
40
/// Override GetSettings and cast to the correct class
41
const WheelSettingsTV * GetSettings() const { return StaticCast<WheelSettingsTV>(mSettings); }
42
43
/// Update the angular velocity of the wheel based on the angular velocity of the track
44
void CalculateAngularVelocity(const VehicleConstraint &inConstraint);
45
46
/// Update the wheel rotation based on the current angular velocity
47
void Update(uint inWheelIndex, float inDeltaTime, const VehicleConstraint &inConstraint);
48
49
int mTrackIndex = -1; ///< Index in mTracks to which this wheel is attached (calculated on initialization)
50
float mCombinedLongitudinalFriction = 0.0f; ///< Combined friction coefficient in longitudinal direction (combines terrain and track)
51
float mCombinedLateralFriction = 0.0f; ///< Combined friction coefficient in lateral direction (combines terrain and track)
52
float mBrakeImpulse = 0.0f; ///< Amount of impulse that the brakes can apply to the floor (excluding friction), spread out from brake impulse applied on track
53
};
54
55
/// Settings of a vehicle with tank tracks
56
///
57
/// Default settings are based around what I could find about the M1 Abrams tank.
58
/// Note to avoid issues with very heavy objects vs very light objects the mass of the tank should be a lot lower (say 10x) than that of a real tank. That means that the engine/brake torque is also 10x less.
59
class JPH_EXPORT TrackedVehicleControllerSettings : public VehicleControllerSettings
60
{
61
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, TrackedVehicleControllerSettings)
62
63
public:
64
// Constructor
65
TrackedVehicleControllerSettings();
66
67
// See: VehicleControllerSettings
68
virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override;
69
virtual void SaveBinaryState(StreamOut &inStream) const override;
70
virtual void RestoreBinaryState(StreamIn &inStream) override;
71
72
VehicleEngineSettings mEngine; ///< The properties of the engine
73
VehicleTransmissionSettings mTransmission; ///< The properties of the transmission (aka gear box)
74
VehicleTrackSettings mTracks[(int)ETrackSide::Num]; ///< List of tracks and their properties
75
};
76
77
/// Runtime controller class for vehicle with tank tracks
78
class JPH_EXPORT TrackedVehicleController : public VehicleController
79
{
80
public:
81
JPH_OVERRIDE_NEW_DELETE
82
83
/// Constructor
84
TrackedVehicleController(const TrackedVehicleControllerSettings &inSettings, VehicleConstraint &inConstraint);
85
86
/// Set input from driver
87
/// @param inForward Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed
88
/// @param inLeftRatio Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)
89
/// @param inRightRatio Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)
90
/// @param inBrake Value between 0 and 1 indicating how strong the brake pedal is pressed
91
void SetDriverInput(float inForward, float inLeftRatio, float inRightRatio, float inBrake) { JPH_ASSERT(inLeftRatio != 0.0f && inRightRatio != 0.0f); mForwardInput = inForward; mLeftRatio = inLeftRatio; mRightRatio = inRightRatio; mBrakeInput = inBrake; }
92
93
/// Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed
94
void SetForwardInput(float inForward) { mForwardInput = inForward; }
95
float GetForwardInput() const { return mForwardInput; }
96
97
/// Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)
98
void SetLeftRatio(float inLeftRatio) { JPH_ASSERT(inLeftRatio != 0.0f); mLeftRatio = inLeftRatio; }
99
float GetLeftRatio() const { return mLeftRatio; }
100
101
/// Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)
102
void SetRightRatio(float inRightRatio) { JPH_ASSERT(inRightRatio != 0.0f); mRightRatio = inRightRatio; }
103
float GetRightRatio() const { return mRightRatio; }
104
105
/// Value between 0 and 1 indicating how strong the brake pedal is pressed
106
void SetBrakeInput(float inBrake) { mBrakeInput = inBrake; }
107
float GetBrakeInput() const { return mBrakeInput; }
108
109
/// Get current engine state
110
const VehicleEngine & GetEngine() const { return mEngine; }
111
112
/// Get current engine state (writable interface, allows you to make changes to the configuration which will take effect the next time step)
113
VehicleEngine & GetEngine() { return mEngine; }
114
115
/// Get current transmission state
116
const VehicleTransmission & GetTransmission() const { return mTransmission; }
117
118
/// Get current transmission state (writable interface, allows you to make changes to the configuration which will take effect the next time step)
119
VehicleTransmission & GetTransmission() { return mTransmission; }
120
121
/// Get the tracks this vehicle has
122
const VehicleTracks & GetTracks() const { return mTracks; }
123
124
/// Get the tracks this vehicle has (writable interface, allows you to make changes to the configuration which will take effect the next time step)
125
VehicleTracks & GetTracks() { return mTracks; }
126
127
#ifdef JPH_DEBUG_RENDERER
128
/// Debug drawing of RPM meter
129
void SetRPMMeter(Vec3Arg inPosition, float inSize) { mRPMMeterPosition = inPosition; mRPMMeterSize = inSize; }
130
#endif // JPH_DEBUG_RENDERER
131
132
protected:
133
/// Synchronize angular velocities of left and right tracks according to their ratios
134
void SyncLeftRightTracks();
135
136
// See: VehicleController
137
virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsTV))); return new WheelTV(static_cast<const WheelSettingsTV &>(inWheel)); }
138
virtual bool AllowSleep() const override;
139
virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
140
virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
141
virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
142
virtual void SaveState(StateRecorder &inStream) const override;
143
virtual void RestoreState(StateRecorder &inStream) override;
144
#ifdef JPH_DEBUG_RENDERER
145
virtual void Draw(DebugRenderer *inRenderer) const override;
146
#endif // JPH_DEBUG_RENDERER
147
148
// Control information
149
float mForwardInput = 0.0f; ///< Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed
150
float mLeftRatio = 1.0f; ///< Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)
151
float mRightRatio = 1.0f; ///< Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)
152
float mBrakeInput = 0.0f; ///< Value between 0 and 1 indicating how strong the brake pedal is pressed
153
154
// Simulation information
155
VehicleEngine mEngine; ///< Engine state of the vehicle
156
VehicleTransmission mTransmission; ///< Transmission state of the vehicle
157
VehicleTracks mTracks; ///< Tracks of the vehicle
158
159
#ifdef JPH_DEBUG_RENDERER
160
// Debug settings
161
Vec3 mRPMMeterPosition { 0, 1, 0 }; ///< Position (in local space of the body) of the RPM meter when drawing the constraint
162
float mRPMMeterSize = 0.5f; ///< Size of the RPM meter when drawing the constraint
163
#endif // JPH_DEBUG_RENDERER
164
};
165
166
JPH_NAMESPACE_END
167
168