UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
EngineSystem.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7#include "VehicleUtility.h"
8
9#if VEHICLE_DEBUGGING_ENABLED
11#endif
12
13
14namespace Chaos
15{
16
17 /*
18 * Simple Normally Aspirated Engine
19 * Output defined by a single torque curve over the rev range
20 * No turbo/ turbo lag
21 *
22 * #todo: proper default values
23 * #todo: replace hardcoded graph with curve data
24 */
25
27 {
29 : MaxTorque(0.f)
30 , MaxRPM(6000)
31 , EngineIdleRPM(1200)
32 , EngineBrakeEffect(0.2f)
33 , EngineRevUpMOI(1.0f)
34 , EngineRevDownRate(1.0f)
35 {
36 // #todo: do we want to provide a default torque curve, maybe even just a straight line
37
38 }
39
40 // #todo: want something like UCurveFloat / FRuntimeFloatCurve / FTorqueCurveEditor
42 float MaxTorque; // [N.m] The peak torque Y value in the normalized torque graph
43 uint16 MaxRPM; // [RPM] The absolute maximum RPM the engine can theoretically reach (last X value in the normalized torque graph)
44 uint16 EngineIdleRPM; // [RPM] The RPM at which the throttle sits when the car is not moving
45 float EngineBrakeEffect; // [0..1] How much the engine slows the vehicle when the throttle is released
46
47 float EngineRevUpMOI; // Affects speed at which engine RPM increases
48 float EngineRevDownRate; // Affects speed at which engine RPM decreases
49
50 };
51
52 class CHAOSVEHICLESCORE_API FSimpleEngineSim : public TVehicleSystem<FSimpleEngineConfig>
53 {
54 public:
55
57
60 {
61 EngineStarted = true;
62
63 // #todo: event for audio? perhaps a delay before you can drive off
64 }
65
68 {
69 EngineStarted = false;
70
71 // #todo: event for audio? perhaps a delay before you can drive off
72 }
73
76 {
77 FVehicleUtility::ClampNormalRange(InThrottle);
78 ThrottlePosition = InThrottle;
79 }
80
86 {
87 FreeRunning = FreeRunningIn;
88 if (!FreeRunning)
89 {
90 TargetSpeed = RPMToOmega(FMath::Clamp(FMath::Abs(InEngineRPM), (float)Setup().EngineIdleRPM, (float)Setup().MaxRPM));
91 }
92 }
93
95 {
96 MaxTorque = InTorque;
97 }
98
100 {
101 if (EngineStarted)
102 {
103 return ThrottlePosition * GetTorqueFromRPM();
104 }
105
106 return 0.f;
107 }
108
110 {
111 return RevRate;
112 }
113
114 float GetTorqueFromRPM(bool LimitToIdle = true)
115 {
116 return GetTorqueFromRPM(CurrentRPM);
117 }
118
119 /* get torque value from torque curve based on input RPM */
120 float GetTorqueFromRPM(float RPM, bool LimitToIdle = true);
121
122
124 float GetEngineRPM() const
125 {
126 if (EngineStarted)
127 {
128 return CurrentRPM;
129 }
130
131 return 0.f;
132 }
133
135 float GetEngineOmega() const
136 {
137 return Omega;
138 }
139
141 void SetEngineOmega( const float EngineOmega)
142 {
143 Omega = EngineOmega;
144 }
145
149 void Simulate(float DeltaTime);
150
151 protected:
152 float MaxTorque; // [N.m] The peak torque Y value in the normalized torque graph
153 float ThrottlePosition; // [0..1 Normalized position]
154 float TargetSpeed; // target RPM
155 float CurrentRPM; // current RPM
156 float DriveTorque; // current torque [N.m]
157
160 bool EngineStarted; // is the engine turned off or has it been started
161 bool FreeRunning; // is engine in neutral with no load from the wheels/transmission
162
163 float Omega; // engine speed
164 float RevRate; // for debug rather than any practical purpose
165
166 };
167
168} // namespace Chaos
169
170#if VEHICLE_DEBUGGING_ENABLED
172#endif
#define UE_ENABLE_OPTIMIZATION
Definition CoreMiscDefines.h:60
#define UE_DISABLE_OPTIMIZATION
Definition CoreMiscDefines.h:59
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint16_t uint16
Definition binka_ue_file_header.h:7
Definition VehicleUtility.h:54
Definition EngineSystem.h:53
float DriveTorque
Definition EngineSystem.h:156
void SetEngineRPM(bool FreeRunningIn, float InEngineRPM)
Definition EngineSystem.h:85
float EngineIdleSpeed
Definition EngineSystem.h:158
float TargetSpeed
Definition EngineSystem.h:154
float ThrottlePosition
Definition EngineSystem.h:153
void SetEngineOmega(const float EngineOmega)
Definition EngineSystem.h:141
void StartEngine()
Definition EngineSystem.h:59
void StopEngine()
Definition EngineSystem.h:67
bool FreeRunning
Definition EngineSystem.h:161
float Omega
Definition EngineSystem.h:163
bool EngineStarted
Definition EngineSystem.h:160
float GetEngineRPM() const
Definition EngineSystem.h:124
float RevRate
Definition EngineSystem.h:164
float GetEngineRevRate()
Definition EngineSystem.h:109
float GetTorqueFromRPM(bool LimitToIdle=true)
Definition EngineSystem.h:114
float CurrentRPM
Definition EngineSystem.h:155
float GetEngineTorque()
Definition EngineSystem.h:99
float MaxTorque
Definition EngineSystem.h:152
void SetThrottle(float InThrottle)
Definition EngineSystem.h:75
void SetMaxTorque(float InTorque)
Definition EngineSystem.h:94
float GetEngineOmega() const
Definition EngineSystem.h:135
float MaxEngineSpeed
Definition EngineSystem.h:159
Definition VehicleSystemTemplate.h:13
Definition SkeletalMeshComponent.h:307
FORCEINLINE float RPMToOmega(float RPM)
Definition VehicleUtility.h:175
Definition EngineSystem.h:27
float EngineRevDownRate
Definition EngineSystem.h:48
uint16 MaxRPM
Definition EngineSystem.h:43
FSimpleEngineConfig()
Definition EngineSystem.h:28
float MaxTorque
Definition EngineSystem.h:42
float EngineRevUpMOI
Definition EngineSystem.h:47
uint16 EngineIdleRPM
Definition EngineSystem.h:44
float EngineBrakeEffect
Definition EngineSystem.h:45
FNormalisedGraph TorqueCurve
Definition EngineSystem.h:41
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592