Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Vehicle/VehicleTransmission.h
9913 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/StreamIn.h>8#include <Jolt/Core/StreamOut.h>9#include <Jolt/Physics/StateRecorder.h>1011JPH_NAMESPACE_BEGIN1213/// How gears are shifted14enum class ETransmissionMode : uint815{16Auto, ///< Automatically shift gear up and down17Manual, ///< Manual gear shift (call SetTransmissionInput)18};1920/// Configuration for the transmission of a vehicle (gear box)21class JPH_EXPORT VehicleTransmissionSettings22{23JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, VehicleTransmissionSettings)2425public:26/// Saves the contents in binary form to inStream.27void SaveBinaryState(StreamOut &inStream) const;2829/// Restores the contents in binary form to inStream.30void RestoreBinaryState(StreamIn &inStream);3132ETransmissionMode mMode = ETransmissionMode::Auto; ///< How to switch gears33Array<float> mGearRatios { 2.66f, 1.78f, 1.3f, 1.0f, 0.74f }; ///< Ratio in rotation rate between engine and gear box, first element is 1st gear, 2nd element 2nd gear etc.34Array<float> mReverseGearRatios { -2.90f }; ///< Ratio in rotation rate between engine and gear box when driving in reverse35float mSwitchTime = 0.5f; ///< How long it takes to switch gears (s), only used in auto mode36float mClutchReleaseTime = 0.3f; ///< How long it takes to release the clutch (go to full friction), only used in auto mode37float mSwitchLatency = 0.5f; ///< How long to wait after releasing the clutch before another switch is attempted (s), only used in auto mode38float mShiftUpRPM = 4000.0f; ///< If RPM of engine is bigger then this we will shift a gear up, only used in auto mode39float mShiftDownRPM = 2000.0f; ///< If RPM of engine is smaller then this we will shift a gear down, only used in auto mode40float mClutchStrength = 10.0f; ///< Strength of the clutch when fully engaged. Total torque a clutch applies is Torque = ClutchStrength * (Velocity Engine - Avg Velocity Wheels At Clutch) (units: k m^2 s^-1)41};4243/// Runtime data for transmission44class JPH_EXPORT VehicleTransmission : public VehicleTransmissionSettings45{46public:47/// Set input from driver regarding the transmission (only relevant when transmission is set to manual mode)48/// @param inCurrentGear Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.49/// @param inClutchFriction Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)50void Set(int inCurrentGear, float inClutchFriction) { mCurrentGear = inCurrentGear; mClutchFriction = inClutchFriction; }5152/// Update the current gear and clutch friction if the transmission is in auto mode53/// @param inDeltaTime Time step delta time in s54/// @param inCurrentRPM Current RPM for engine55/// @param inForwardInput Hint if the user wants to drive forward (> 0) or backwards (< 0)56/// @param inCanShiftUp Indicates if we want to allow the transmission to shift up (e.g. pass false if wheels are slipping)57void Update(float inDeltaTime, float inCurrentRPM, float inForwardInput, bool inCanShiftUp);5859/// Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.60int GetCurrentGear() const { return mCurrentGear; }6162/// Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)63float GetClutchFriction() const { return mClutchFriction; }6465/// If the auto box is currently switching gears66bool IsSwitchingGear() const { return mGearSwitchTimeLeft > 0.0f; }6768/// Return the transmission ratio based on the current gear (ratio between engine and differential)69float GetCurrentRatio() const;7071/// Only allow sleeping when the transmission is idle72bool AllowSleep() const { return mGearSwitchTimeLeft <= 0.0f && mClutchReleaseTimeLeft <= 0.0f && mGearSwitchLatencyTimeLeft <= 0.0f; }7374/// Saving state for replay75void SaveState(StateRecorder &inStream) const;76void RestoreState(StateRecorder &inStream);7778private:79int mCurrentGear = 0; ///< Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.80float mClutchFriction = 1.0f; ///< Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)81float mGearSwitchTimeLeft = 0.0f; ///< When switching gears this will be > 0 and will cause the engine to not provide any torque to the wheels for a short time (used for automatic gear switching only)82float mClutchReleaseTimeLeft = 0.0f; ///< After switching gears this will be > 0 and will cause the clutch friction to go from 0 to 1 (used for automatic gear switching only)83float mGearSwitchLatencyTimeLeft = 0.0f; ///< After releasing the clutch this will be > 0 and will prevent another gear switch (used for automatic gear switching only)84};8586JPH_NAMESPACE_END878889