| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
| 18 |
|
| 19 |
|
| 20 |
class JPH_EXPORT VehicleEngineSettings |
| 21 |
{ |
| 22 |
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, VehicleEngineSettings) |
| 23 |
|
| 24 |
public: |
| 25 |
|
| 26 |
VehicleEngineSettings(); |
| 27 |
|
| 28 |
|
| 29 |
void SaveBinaryState(StreamOut &inStream) const; |
| 30 |
|
| 31 |
|
| 32 |
void RestoreBinaryState(StreamIn &inStream); |
| 33 |
|
| 34 |
float mMaxTorque = 500.0f; |
| 35 |
float mMinRPM = 1000.0f; |
| 36 |
float mMaxRPM = 6000.0f; |
| 37 |
LinearCurve mNormalizedTorque; |
| 38 |
float mInertia = 0.5f; |
| 39 |
float mAngularDamping = 0.2f; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
class JPH_EXPORT VehicleEngine : public VehicleEngineSettings |
| 44 |
{ |
| 45 |
public: |
| 46 |
|
| 47 |
static constexpr float cAngularVelocityToRPM = 60.0f / (2.0f * JPH_PI); |
| 48 |
|
| 49 |
|
| 50 |
inline void ClampRPM() { mCurrentRPM = Clamp(mCurrentRPM, mMinRPM, mMaxRPM); } |
| 51 |
|
| 52 |
|
| 53 |
float GetCurrentRPM() const { return mCurrentRPM; } |
| 54 |
|
| 55 |
|
| 56 |
void SetCurrentRPM(float inRPM) { mCurrentRPM = inRPM; ClampRPM(); } |
| 57 |
|
| 58 |
|
| 59 |
inline float GetAngularVelocity() const { return mCurrentRPM / cAngularVelocityToRPM; } |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
float GetTorque(float inAcceleration) const { return inAcceleration * mMaxTorque * mNormalizedTorque.GetValue(mCurrentRPM / mMaxRPM); } |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
void ApplyTorque(float inTorque, float inDeltaTime); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
void ApplyDamping(float inDeltaTime); |
| 73 |
|
| 74 |
#ifdef JPH_DEBUG_RENDERER |
| 75 |
|
| 76 |
float ConvertRPMToAngle(float inRPM) const { return (-0.75f + 1.5f * inRPM / mMaxRPM) * JPH_PI; } |
| 77 |
|
| 78 |
|
| 79 |
void DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const; |
| 80 |
#endif |
| 81 |
|
| 82 |
|
| 83 |
bool AllowSleep() const { return mCurrentRPM <= 1.01f * mMinRPM; } |
| 84 |
|
| 85 |
|
| 86 |
void SaveState(StateRecorder &inStream) const; |
| 87 |
void RestoreState(StateRecorder &inStream); |
| 88 |
|
| 89 |
private: |
| 90 |
float mCurrentRPM = mMinRPM; |
| 91 |
}; |
| 92 |
|
| 93 |
JPH_NAMESPACE_END |