Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
VehicleEngine.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
10#include <Jolt/Core/StreamOut.h>
12
14
15#ifdef JPH_DEBUG_RENDERER
16 class DebugRenderer;
17#endif // JPH_DEBUG_RENDERER
18
21{
22public:
24
25
27
29 void SaveBinaryState(StreamOut &inStream) const;
30
32 void RestoreBinaryState(StreamIn &inStream);
33
34 float mMaxTorque = 500.0f;
35 float mMinRPM = 1000.0f;
36 float mMaxRPM = 6000.0f;
38 float mInertia = 0.5f;
39 float mAngularDamping = 0.2f;
40};
41
44{
45public:
47 static constexpr float cAngularVelocityToRPM = 60.0f / (2.0f * JPH_PI);
48
50 inline void ClampRPM() { mCurrentRPM = Clamp(mCurrentRPM, mMinRPM, mMaxRPM); }
51
53 float GetCurrentRPM() const { return mCurrentRPM; }
54
56 void SetCurrentRPM(float inRPM) { mCurrentRPM = inRPM; ClampRPM(); }
57
59 inline float GetAngularVelocity() const { return mCurrentRPM / cAngularVelocityToRPM; }
60
63 float GetTorque(float inAcceleration) const { return inAcceleration * mMaxTorque * mNormalizedTorque.GetValue(mCurrentRPM / mMaxRPM); }
64
68 void ApplyTorque(float inTorque, float inDeltaTime);
69
72 void ApplyDamping(float inDeltaTime);
73
74#ifdef JPH_DEBUG_RENDERER
76 void DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const;
77#endif // JPH_DEBUG_RENDERER
78
80 void SaveState(StateRecorder &inStream) const;
81 void RestoreState(StateRecorder &inStream);
82
83private:
84 float mCurrentRPM = mMinRPM;
85};
86
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition: Math.h:45
#define JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(class_name)
Definition: SerializableObject.h:71
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
Definition: LinearCurve.h:17
float GetValue(float inX) const
Definition: LinearCurve.cpp:25
Definition: StateRecorder.h:15
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
Definition: Vec3.h:16
Runtime data for engine.
Definition: VehicleEngine.h:44
static constexpr float cAngularVelocityToRPM
Multiply an angular velocity (rad/s) with this value to get rounds per minute (RPM)
Definition: VehicleEngine.h:47
void ClampRPM()
Clamp the RPM between min and max RPM.
Definition: VehicleEngine.h:50
void RestoreState(StateRecorder &inStream)
Definition: VehicleEngine.cpp:117
void SaveState(StateRecorder &inStream) const
Saving state for replay.
Definition: VehicleEngine.cpp:112
void DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const
Debug draw a RPM meter.
Definition: VehicleEngine.cpp:66
float GetTorque(float inAcceleration) const
Definition: VehicleEngine.h:63
void SetCurrentRPM(float inRPM)
Update rotation speed of engine in rounds per minute.
Definition: VehicleEngine.h:56
float GetCurrentRPM() const
Current rotation speed of engine in rounds per minute.
Definition: VehicleEngine.h:53
void ApplyDamping(float inDeltaTime)
Definition: VehicleEngine.cpp:54
float GetAngularVelocity() const
Get current angular velocity of the engine in radians / second.
Definition: VehicleEngine.h:59
void ApplyTorque(float inTorque, float inDeltaTime)
Definition: VehicleEngine.cpp:47
Generic properties for a vehicle engine.
Definition: VehicleEngine.h:21
void RestoreBinaryState(StreamIn &inStream)
Restores the contents in binary form to inStream.
Definition: VehicleEngine.cpp:39
float mMinRPM
Min amount of revolutions per minute (rpm) the engine can produce without stalling.
Definition: VehicleEngine.h:35
LinearCurve mNormalizedTorque
Curve that describes a ratio of the max torque the engine can produce vs the fraction of the max RPM ...
Definition: VehicleEngine.h:37
float mMaxTorque
Max amount of torque (Nm) that the engine can deliver.
Definition: VehicleEngine.h:34
float mMaxRPM
Max amount of revolutions per minute (rpm) the engine can generate.
Definition: VehicleEngine.h:36
float mAngularDamping
Angular damping factor of the wheel: dw/dt = -c * w.
Definition: VehicleEngine.h:39
void SaveBinaryState(StreamOut &inStream) const
Saves the contents in binary form to inStream.
Definition: VehicleEngine.cpp:31
VehicleEngineSettings()
Constructor.
Definition: VehicleEngine.cpp:23
float mInertia
Moment of inertia (kg m^2) of the engine.
Definition: VehicleEngine.h:38