Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Vehicle/VehicleEngine.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/ObjectStream/SerializableObject.h>7#include <Jolt/Core/LinearCurve.h>8#include <Jolt/Core/StreamIn.h>9#include <Jolt/Core/StreamOut.h>10#include <Jolt/Physics/StateRecorder.h>1112JPH_NAMESPACE_BEGIN1314#ifdef JPH_DEBUG_RENDERER15class DebugRenderer;16#endif // JPH_DEBUG_RENDERER1718/// Generic properties for a vehicle engine19class JPH_EXPORT VehicleEngineSettings20{21JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, VehicleEngineSettings)2223public:24/// Constructor25VehicleEngineSettings();2627/// Saves the contents in binary form to inStream.28void SaveBinaryState(StreamOut &inStream) const;2930/// Restores the contents in binary form to inStream.31void RestoreBinaryState(StreamIn &inStream);3233float mMaxTorque = 500.0f; ///< Max amount of torque (Nm) that the engine can deliver34float mMinRPM = 1000.0f; ///< Min amount of revolutions per minute (rpm) the engine can produce without stalling35float mMaxRPM = 6000.0f; ///< Max amount of revolutions per minute (rpm) the engine can generate36LinearCurve 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)37float mInertia = 0.5f; ///< Moment of inertia (kg m^2) of the engine38float mAngularDamping = 0.2f; ///< Angular damping factor of the wheel: dw/dt = -c * w39};4041/// Runtime data for engine42class JPH_EXPORT VehicleEngine : public VehicleEngineSettings43{44public:45/// Multiply an angular velocity (rad/s) with this value to get rounds per minute (RPM)46static constexpr float cAngularVelocityToRPM = 60.0f / (2.0f * JPH_PI);4748/// Clamp the RPM between min and max RPM49inline void ClampRPM() { mCurrentRPM = Clamp(mCurrentRPM, mMinRPM, mMaxRPM); }5051/// Current rotation speed of engine in rounds per minute52float GetCurrentRPM() const { return mCurrentRPM; }5354/// Update rotation speed of engine in rounds per minute55void SetCurrentRPM(float inRPM) { mCurrentRPM = inRPM; ClampRPM(); }5657/// Get current angular velocity of the engine in radians / second58inline float GetAngularVelocity() const { return mCurrentRPM / cAngularVelocityToRPM; }5960/// Get the amount of torque (N m) that the engine can supply61/// @param inAcceleration How much the gas pedal is pressed [0, 1]62float GetTorque(float inAcceleration) const { return inAcceleration * mMaxTorque * mNormalizedTorque.GetValue(mCurrentRPM / mMaxRPM); }6364/// Apply a torque to the engine rotation speed65/// @param inTorque Torque in N m66/// @param inDeltaTime Delta time in seconds67void ApplyTorque(float inTorque, float inDeltaTime);6869/// Update the engine RPM for damping70/// @param inDeltaTime Delta time in seconds71void ApplyDamping(float inDeltaTime);7273#ifdef JPH_DEBUG_RENDERER74// Function that converts RPM to an angle in radians for debugging purposes75float ConvertRPMToAngle(float inRPM) const { return (-0.75f + 1.5f * inRPM / mMaxRPM) * JPH_PI; }7677/// Debug draw a RPM meter78void DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const;79#endif // JPH_DEBUG_RENDERER8081/// If the engine is idle we allow the vehicle to sleep82bool AllowSleep() const { return mCurrentRPM <= 1.01f * mMinRPM; }8384/// Saving state for replay85void SaveState(StateRecorder &inStream) const;86void RestoreState(StateRecorder &inStream);8788private:89float mCurrentRPM = mMinRPM; ///< Current rotation speed of engine in rounds per minute90};9192JPH_NAMESPACE_END939495