Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
TrackedVehicleController.h
Go to the documentation of this file.
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
12
14
15class PhysicsSystem;
16
19{
20public:
22
23 // See: WheelSettings
24 virtual void SaveBinaryState(StreamOut &inStream) const override;
25 virtual void RestoreBinaryState(StreamIn &inStream) override;
26
27 float mLongitudinalFriction = 4.0f;
28 float mLateralFriction = 2.0f;
29};
30
32class WheelTV : public Wheel
33{
34public:
36
38 explicit WheelTV(const WheelSettingsTV &inWheel);
39
41 const WheelSettingsTV * GetSettings() const { return static_cast<const WheelSettingsTV *>(mSettings.GetPtr()); }
42
44 void CalculateAngularVelocity(const VehicleConstraint &inConstraint);
45
47 void Update(float inDeltaTime, const VehicleConstraint &inConstraint);
48
49 int mTrackIndex = -1;
52 float mBrakeImpulse = 0.0f;
53};
54
60{
61public:
63
64 // Constructor
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
74 VehicleTrackSettings mTracks[(int)ETrackSide::Num];
75};
76
79{
80public:
82
85
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
94 const VehicleEngine & GetEngine() const { return mEngine; }
95
98
101
104
106 const VehicleTracks & GetTracks() const { return mTracks; }
107
110
111#ifdef JPH_DEBUG_RENDERER
113 void SetRPMMeter(Vec3Arg inPosition, float inSize) { mRPMMeterPosition = inPosition; mRPMMeterSize = inSize; }
114#endif // JPH_DEBUG_RENDERER
115
116protected:
118 void SyncLeftRightTracks();
119
120 // See: VehicleController
121 virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsTV))); return new WheelTV(static_cast<const WheelSettingsTV &>(inWheel)); }
122 virtual bool AllowSleep() const override { return mForwardInput == 0.0f; }
123 virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
124 virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
125 virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;
126 virtual void SaveState(StateRecorder &inStream) const override;
127 virtual void RestoreState(StateRecorder &inStream) override;
128#ifdef JPH_DEBUG_RENDERER
129 virtual void Draw(DebugRenderer *inRenderer) const override;
130#endif // JPH_DEBUG_RENDERER
131
132 // Control information
133 float mForwardInput = 0.0f;
134 float mLeftRatio = 1.0f;
135 float mRightRatio = 1.0f;
136 float mBrakeInput = 0.0f;
137
138 // Simluation information
142
143#ifdef JPH_DEBUG_RENDERER
144 // Debug settings
146 float mRPMMeterSize = 0.5f;
147#endif // JPH_DEBUG_RENDERER
148};
149
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
bool IsKindOf(const Type *inObject, const RTTI *inRTTI)
Check if inObject is or is derived from DstType.
Definition: RTTI.h:365
#define JPH_RTTI(class_name)
Definition: RTTI.h:315
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
VehicleTrack[(int) ETrackSide::Num] VehicleTracks
Definition: VehicleTrack.h:54
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
Definition: PhysicsSystem.h:28
const T * GetPtr() const
Get pointer.
Definition: Reference.h:188
Definition: StateRecorder.h:15
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
Runtime controller class for vehicle with tank tracks.
Definition: TrackedVehicleController.h:79
VehicleEngine mEngine
Engine state of the vehicle.
Definition: TrackedVehicleController.h:139
float mLeftRatio
Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used fo...
Definition: TrackedVehicleController.h:134
VehicleTracks mTracks
Tracks of the vehicle.
Definition: TrackedVehicleController.h:141
const VehicleTransmission & GetTransmission() const
Get current transmission state.
Definition: TrackedVehicleController.h:100
const VehicleTracks & GetTracks() const
Get the tracks this vehicle has.
Definition: TrackedVehicleController.h:106
Vec3 mRPMMeterPosition
Position (in local space of the body) of the RPM meter when drawing the constraint.
Definition: TrackedVehicleController.h:145
float mRPMMeterSize
Size of the RPM meter when drawing the constraint.
Definition: TrackedVehicleController.h:146
virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override
Definition: TrackedVehicleController.cpp:155
VehicleTransmission mTransmission
Transmission state of the vehicle.
Definition: TrackedVehicleController.h:140
virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override
Definition: TrackedVehicleController.h:121
void SetRPMMeter(Vec3Arg inPosition, float inSize)
Debug drawing of RPM meter.
Definition: TrackedVehicleController.h:113
virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override
Definition: TrackedVehicleController.cpp:194
VehicleTracks & GetTracks()
Get the tracks this vehicle has (writable interface, allows you to make changes to the configuration ...
Definition: TrackedVehicleController.h:109
const VehicleEngine & GetEngine() const
Get current engine state.
Definition: TrackedVehicleController.h:94
virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override
Definition: TrackedVehicleController.cpp:335
virtual void Draw(DebugRenderer *inRenderer) const override
Definition: TrackedVehicleController.cpp:412
virtual bool AllowSleep() const override
Definition: TrackedVehicleController.h:122
void SyncLeftRightTracks()
Synchronize angular velocities of left and right tracks according to their ratios.
Definition: TrackedVehicleController.cpp:172
virtual void RestoreState(StateRecorder &inStream) override
Definition: TrackedVehicleController.cpp:502
VehicleEngine & GetEngine()
Get current engine state (writable interface, allows you to make changes to the configuration which w...
Definition: TrackedVehicleController.h:97
virtual void SaveState(StateRecorder &inStream) const override
Definition: TrackedVehicleController.cpp:488
float mForwardInput
Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving dir...
Definition: TrackedVehicleController.h:133
float mRightRatio
Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used f...
Definition: TrackedVehicleController.h:135
float mBrakeInput
Value between 0 and 1 indicating how strong the brake pedal is pressed.
Definition: TrackedVehicleController.h:136
void SetDriverInput(float inForward, float inLeftRatio, float inRightRatio, float inBrake)
Definition: TrackedVehicleController.h:91
VehicleTransmission & GetTransmission()
Get current transmission state (writable interface, allows you to make changes to the configuration w...
Definition: TrackedVehicleController.h:103
Definition: TrackedVehicleController.h:60
TrackedVehicleControllerSettings()
Definition: TrackedVehicleController.cpp:89
virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override
Create an instance of the vehicle controller class.
Definition: TrackedVehicleController.cpp:84
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the controller settings in binary form to inStream.
Definition: TrackedVehicleController.cpp:102
virtual void RestoreBinaryState(StreamIn &inStream) override
Restore the contents of the controller settings in binary form from inStream.
Definition: TrackedVehicleController.cpp:112
VehicleTrackSettings mTracks[(int) ETrackSide::Num]
List of tracks and their properties.
Definition: TrackedVehicleController.h:74
VehicleEngineSettings mEngine
The properties of the engine.
Definition: TrackedVehicleController.h:72
VehicleTransmissionSettings mTransmission
The properties of the transmission (aka gear box)
Definition: TrackedVehicleController.h:73
Definition: Vec3.h:16
Definition: VehicleConstraint.h:67
Runtime data for interface that controls acceleration / decelleration of the vehicle.
Definition: VehicleController.h:34
Basic settings object for interface that controls acceleration / decelleration of the vehicle.
Definition: VehicleController.h:18
Runtime data for engine.
Definition: VehicleEngine.h:44
Generic properties for a vehicle engine.
Definition: VehicleEngine.h:21
Generic properties for tank tracks.
Definition: VehicleTrack.h:25
Runtime data for transmission.
Definition: VehicleTransmission.h:46
Configuration for the transmission of a vehicle (gear box)
Definition: VehicleTransmission.h:23
Base class for runtime data for a wheel, each VehicleController can implement a derived class of this...
Definition: Wheel.h:45
RefConst< WheelSettings > mSettings
Configuration settings for this wheel.
Definition: Wheel.h:122
Base class for wheel settings, each VehicleController can implement a derived class of this.
Definition: Wheel.h:19
WheelSettings object specifically for TrackedVehicleController.
Definition: TrackedVehicleController.h:19
virtual void RestoreBinaryState(StreamIn &inStream) override
Restores the contents in binary form to inStream.
Definition: TrackedVehicleController.cpp:39
float mLateralFriction
Friction in sideway direction of tire.
Definition: TrackedVehicleController.h:28
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents in binary form to inStream.
Definition: TrackedVehicleController.cpp:33
float mLongitudinalFriction
Friction in forward direction of tire.
Definition: TrackedVehicleController.h:27
Wheel object specifically for TrackedVehicleController.
Definition: TrackedVehicleController.h:33
float mBrakeImpulse
Amount of impulse that the brakes can apply to the floor (excluding friction), spread out from brake ...
Definition: TrackedVehicleController.h:52
float mCombinedLongitudinalFriction
Combined friction coefficient in longitudinal direction (combines terrain and track)
Definition: TrackedVehicleController.h:50
float mCombinedLateralFriction
Combined friction coefficient in lateral direction (combines terrain and track)
Definition: TrackedVehicleController.h:51
const WheelSettingsTV * GetSettings() const
Override GetSettings and cast to the correct class.
Definition: TrackedVehicleController.h:41
int mTrackIndex
Index in mTracks to which this wheel is attached (calculated on initialization)
Definition: TrackedVehicleController.h:49
void CalculateAngularVelocity(const VehicleConstraint &inConstraint)
Update the angular velocity of the wheel based on the angular velocity of the track.
Definition: TrackedVehicleController.cpp:50
void Update(float inDeltaTime, const VehicleConstraint &inConstraint)
Update the wheel rotation based on the current angular velocity.
Definition: TrackedVehicleController.cpp:60