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
75 // Function that converts RPM to an angle in radians for debugging purposes
76 float ConvertRPMToAngle(float inRPM) const { return (-0.75f + 1.5f * inRPM / mMaxRPM) * JPH_PI; }
77
79 void DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const;
80#endif // JPH_DEBUG_RENDERER
81
83 bool AllowSleep() const { return mCurrentRPM <= 1.01f * mMinRPM; }
84
86 void SaveState(StateRecorder &inStream) const;
87 void RestoreState(StateRecorder &inStream);
88
89private:
90 float mCurrentRPM = mMinRPM;
91};
92
#define JPH_EXPORT
Definition: Core.h:214
#define JPH_NAMESPACE_END
Definition: Core.h:354
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
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(linkage, class_name)
Definition: SerializableObject.h:71
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:30
Definition: LinearCurve.h:17
Definition: StateRecorder.h:48
Simple binary input stream.
Definition: StreamIn.h:13
Simple binary output stream.
Definition: StreamOut.h:13
Definition: Vec3.h:16
Runtime data for engine.
Definition: VehicleEngine.h:44
void ClampRPM()
Clamp the RPM between min and max RPM.
Definition: VehicleEngine.h:50
float GetTorque(float inAcceleration) const
Definition: VehicleEngine.h:63
float ConvertRPMToAngle(float inRPM) const
Definition: VehicleEngine.h:76
bool AllowSleep() const
If the engine is idle we allow the vehicle to sleep.
Definition: VehicleEngine.h:83
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
float GetAngularVelocity() const
Get current angular velocity of the engine in radians / second.
Definition: VehicleEngine.h:59
Generic properties for a vehicle engine.
Definition: VehicleEngine.h:21
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