Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Vehicle.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "entity/EntityId.hpp"
5#include <Jolt/Physics/Vehicle/VehicleAntiRollBar.h>
6#include <Jolt/Physics/Vehicle/VehicleConstraint.h>
7#include <array>
8#include <memory>
9
10namespace Physics::Component {
11
15enum class DrivetrainType : uint8_t {
22};
23
39
43enum class TransmissionMode : uint8_t {
48};
49
58 float rpm;
59 float torque;
60};
61
68
71 std::vector<float> forwardGearRatios = {2.66f, 1.78f, 1.3f, 1.0f, 0.74f};
72
75 std::vector<float> reverseGearRatios = {-2.90f};
76
78 int currentGear = 1;
79
81 float switchTime = 0.5f;
82
84 float clutchReleaseTime = 0.3f;
85
87 float switchLatency = 0.5f;
88
90 float shiftUpRPM = 4000.0f;
91
93 float shiftDownRPM = 2000.0f;
94
98 float clutchStrength = 10.0f;
99
101 float clutchFriction = 1.0f;
102};
103
109 float maxTorque = 500.0f;
110
112 float minRPM = 1000.0f;
113
115 float maxRPM = 6000.0f;
116
121 std::vector<TorqueCurvePoint> normalizedTorque = {
122 {0.0f, 0.8f}, // 80% torque at minRPM
123 {0.66f, 1.0f}, // 100% torque at 66% of RPM range (peak)
124 {1.0f, 0.8f} // 80% torque at maxRPM
125 };
126
128 float inertia = 0.5f;
129
132 float angularDamping = 0.2f;
133};
134
140 float frontStiffness = 5000.0f;
141
143 float rearStiffness = 5000.0f;
144};
145
152struct Vehicle {
154 std::array<WheelSettings, static_cast<size_t>(WheelIndex::Count)> wheels;
155
158
161
164
167
169 std::array<Engine::EntityId, 4> wheelEntities;
170
172 std::array<glm::vec3, 4> wheelPositions = GetDefaultWheelPositions();
173
176
180
191 static std::array<glm::vec3, 4> GetDefaultWheelPositions()
192 {
193 return {
194 glm::vec3(-0.9f, -0.3f, 1.2f), // Front-left
195 glm::vec3(0.9f, -0.3f, 1.2f), // Front-right
196 glm::vec3(-0.9f, -0.3f, -1.2f), // Rear-left
197 glm::vec3(0.9f, -0.3f, -1.2f) // Rear-right
198 };
199 }
200
205 {
206 using enum WheelIndex;
207
208 Vehicle vehicle;
210 vehicle.engine.maxTorque = 600.0f;
211 vehicle.engine.maxRPM = 7000.0f;
212 vehicle.rollbar.rearStiffness = 8000.0f;
213
214 // Front wheels with steering
215 vehicle.wheels[static_cast<size_t>(FrontLeft)] = WheelSettings::CreateFrontWheel();
216 vehicle.wheels[static_cast<size_t>(FrontRight)] = WheelSettings::CreateFrontWheel();
217
218 // Rear wheels without steering
219 vehicle.wheels[static_cast<size_t>(RearLeft)] = WheelSettings::CreateRearWheel();
220 vehicle.wheels[static_cast<size_t>(RearRight)] = WheelSettings::CreateRearWheel();
221 return vehicle;
222 }
223};
224
225} // namespace Physics::Component
Definition BoxCollider.hpp:27
WheelIndex
Enum for identifying wheel positions in a 4-wheel vehicle.
Definition WheelSettings.hpp:11
@ FrontRight
Definition WheelSettings.hpp:13
@ FrontLeft
Definition WheelSettings.hpp:12
@ RearRight
Definition WheelSettings.hpp:15
@ RearLeft
Definition WheelSettings.hpp:14
@ Count
Definition WheelSettings.hpp:16
DrivetrainType
Drivetrain configuration for vehicle.
Definition Vehicle.hpp:15
@ RWD
Rear-Wheel Drive - power to rear wheels only.
Definition Vehicle.hpp:21
@ AWD
All-Wheel Drive - power to all 4 wheels.
Definition Vehicle.hpp:17
@ FWD
Front-Wheel Drive - power to front wheels only.
Definition Vehicle.hpp:19
TransmissionMode
Transmission mode - how gears are shifted.
Definition Vehicle.hpp:43
@ Auto
Automatically shift gear up and down.
Definition Vehicle.hpp:45
@ Manual
Manual gear shift (controlled by user input).
Definition Vehicle.hpp:47
CollisionTesterType
Collision tester type for vehicle wheel collision detection.
Definition Vehicle.hpp:31
@ CastSphere
Sphere cast - better than ray, good for rough terrain.
Definition Vehicle.hpp:35
@ Ray
Simple raycast - fastest but prone to ghost collisions on tiled floors.
Definition Vehicle.hpp:33
@ CastCylinder
Cylinder cast - most accurate, best for tiled floors and complex terrain (default).
Definition Vehicle.hpp:37
Engine configuration.
Definition Vehicle.hpp:107
float maxRPM
Maximum RPM.
Definition Vehicle.hpp:115
float minRPM
Minimum RPM.
Definition Vehicle.hpp:112
float maxTorque
Maximum engine torque in Nm.
Definition Vehicle.hpp:109
std::vector< TorqueCurvePoint > normalizedTorque
Definition Vehicle.hpp:121
float angularDamping
Definition Vehicle.hpp:132
float inertia
Moment of inertia (kg m^2) of the engine.
Definition Vehicle.hpp:128
Gearbox configuration for vehicle transmission.
Definition Vehicle.hpp:65
int currentGear
Current gear (-1 = reverse, 0 = neutral, 1 = first forward, etc.).
Definition Vehicle.hpp:78
float clutchFriction
Current clutch friction (0 = no friction, 1 = full friction) - for manual mode.
Definition Vehicle.hpp:101
float switchLatency
How long to wait after releasing clutch before another switch is attempted (s) - only used in auto mo...
Definition Vehicle.hpp:87
TransmissionMode mode
Transmission mode (auto or manual shifting).
Definition Vehicle.hpp:67
std::vector< float > forwardGearRatios
Definition Vehicle.hpp:71
float switchTime
How long it takes to switch gears (s) - only used in auto mode.
Definition Vehicle.hpp:81
float shiftUpRPM
If RPM of engine is bigger than this, shift a gear up - only used in auto mode.
Definition Vehicle.hpp:90
float clutchReleaseTime
How long it takes to release the clutch (go to full friction) - only used in auto mode.
Definition Vehicle.hpp:84
float shiftDownRPM
If RPM of engine is smaller than this, shift a gear down - only used in auto mode.
Definition Vehicle.hpp:93
float clutchStrength
Definition Vehicle.hpp:98
std::vector< float > reverseGearRatios
Definition Vehicle.hpp:75
Rollbar (anti-roll bar) configuration.
Definition Vehicle.hpp:138
float rearStiffness
Rear rollbar stiffness (N/rad).
Definition Vehicle.hpp:143
float frontStiffness
Front rollbar stiffness (N/rad).
Definition Vehicle.hpp:140
Torque curve point for normalized engine torque.
Definition Vehicle.hpp:57
float rpm
Normalized RPM (0.0 to 1.0).
Definition Vehicle.hpp:58
float torque
Normalized torque (0.0 to 1.0).
Definition Vehicle.hpp:59
Main vehicle component containing all configuration.
Definition Vehicle.hpp:152
std::array< Engine::EntityId, 4 > wheelEntities
Wheel entities for visual representation.
Definition Vehicle.hpp:169
static std::array< glm::vec3, 4 > GetDefaultWheelPositions()
Get default wheel positions relative to chassis center.
Definition Vehicle.hpp:191
RollbarSettings rollbar
Rollbar configuration.
Definition Vehicle.hpp:166
GearboxSettings gearbox
Gearbox configuration.
Definition Vehicle.hpp:163
DrivetrainType drivetrain
Drivetrain configuration.
Definition Vehicle.hpp:157
std::array< glm::vec3, 4 > wheelPositions
Wheel positions relative to chassis center.
Definition Vehicle.hpp:172
static Vehicle CreateDefaultCar()
Create a default RWD sports car configuration.
Definition Vehicle.hpp:204
EngineSettings engine
Engine configuration.
Definition Vehicle.hpp:160
CollisionTesterType collisionTesterType
Collision tester type for wheel-ground detection (default: CastCylinder).
Definition Vehicle.hpp:175
std::array< WheelSettings, static_cast< size_t >(WheelIndex::Count)> wheels
Wheel settings for all 4 wheels (indexed by WheelIndex).
Definition Vehicle.hpp:154
float convexRadiusFraction
Definition Vehicle.hpp:179
static WheelSettings CreateRearWheel()
Create default rear wheel settings without steering.
Definition WheelSettings.hpp:109
static WheelSettings CreateFrontWheel()
Create default front wheel settings with steering.
Definition WheelSettings.hpp:99