Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Vehicle/VehicleEngine.h
9912 views
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
7
#include <Jolt/ObjectStream/SerializableObject.h>
8
#include <Jolt/Core/LinearCurve.h>
9
#include <Jolt/Core/StreamIn.h>
10
#include <Jolt/Core/StreamOut.h>
11
#include <Jolt/Physics/StateRecorder.h>
12
13
JPH_NAMESPACE_BEGIN
14
15
#ifdef JPH_DEBUG_RENDERER
16
class DebugRenderer;
17
#endif // JPH_DEBUG_RENDERER
18
19
/// Generic properties for a vehicle engine
20
class JPH_EXPORT VehicleEngineSettings
21
{
22
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, VehicleEngineSettings)
23
24
public:
25
/// Constructor
26
VehicleEngineSettings();
27
28
/// Saves the contents in binary form to inStream.
29
void SaveBinaryState(StreamOut &inStream) const;
30
31
/// Restores the contents in binary form to inStream.
32
void RestoreBinaryState(StreamIn &inStream);
33
34
float mMaxTorque = 500.0f; ///< Max amount of torque (Nm) that the engine can deliver
35
float mMinRPM = 1000.0f; ///< Min amount of revolutions per minute (rpm) the engine can produce without stalling
36
float mMaxRPM = 6000.0f; ///< Max amount of revolutions per minute (rpm) the engine can generate
37
LinearCurve mNormalizedTorque; ///< Y-axis: Curve that describes a ratio of the max torque the engine can produce (0 = 0, 1 = mMaxTorque). X-axis: the fraction of the RPM of the engine (0 = mMinRPM, 1 = mMaxRPM)
38
float mInertia = 0.5f; ///< Moment of inertia (kg m^2) of the engine
39
float mAngularDamping = 0.2f; ///< Angular damping factor of the wheel: dw/dt = -c * w
40
};
41
42
/// Runtime data for engine
43
class JPH_EXPORT VehicleEngine : public VehicleEngineSettings
44
{
45
public:
46
/// Multiply an angular velocity (rad/s) with this value to get rounds per minute (RPM)
47
static constexpr float cAngularVelocityToRPM = 60.0f / (2.0f * JPH_PI);
48
49
/// Clamp the RPM between min and max RPM
50
inline void ClampRPM() { mCurrentRPM = Clamp(mCurrentRPM, mMinRPM, mMaxRPM); }
51
52
/// Current rotation speed of engine in rounds per minute
53
float GetCurrentRPM() const { return mCurrentRPM; }
54
55
/// Update rotation speed of engine in rounds per minute
56
void SetCurrentRPM(float inRPM) { mCurrentRPM = inRPM; ClampRPM(); }
57
58
/// Get current angular velocity of the engine in radians / second
59
inline float GetAngularVelocity() const { return mCurrentRPM / cAngularVelocityToRPM; }
60
61
/// Get the amount of torque (N m) that the engine can supply
62
/// @param inAcceleration How much the gas pedal is pressed [0, 1]
63
float GetTorque(float inAcceleration) const { return inAcceleration * mMaxTorque * mNormalizedTorque.GetValue(mCurrentRPM / mMaxRPM); }
64
65
/// Apply a torque to the engine rotation speed
66
/// @param inTorque Torque in N m
67
/// @param inDeltaTime Delta time in seconds
68
void ApplyTorque(float inTorque, float inDeltaTime);
69
70
/// Update the engine RPM for damping
71
/// @param inDeltaTime Delta time in seconds
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
78
/// Debug draw a RPM meter
79
void DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const;
80
#endif // JPH_DEBUG_RENDERER
81
82
/// If the engine is idle we allow the vehicle to sleep
83
bool AllowSleep() const { return mCurrentRPM <= 1.01f * mMinRPM; }
84
85
/// Saving state for replay
86
void SaveState(StateRecorder &inStream) const;
87
void RestoreState(StateRecorder &inStream);
88
89
private:
90
float mCurrentRPM = mMinRPM; ///< Current rotation speed of engine in rounds per minute
91
};
92
93
JPH_NAMESPACE_END
94
95