Skip to content
0.2.0-dev Download ZIP
87 lines ·4.9 KB Raw
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/StreamIn.h>
9#include <Jolt/Core/StreamOut.h>
10#include <Jolt/Physics/StateRecorder.h>
11
12JPH_NAMESPACE_BEGIN
13
14/// How gears are shifted
15enum class ETransmissionMode : uint8
16{
17 Auto, ///< Automatically shift gear up and down
18 Manual, ///< Manual gear shift (call SetTransmissionInput)
19};
20
21/// Configuration for the transmission of a vehicle (gear box)
22class JPH_EXPORT VehicleTransmissionSettings
23{
24 JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, VehicleTransmissionSettings)
25
26public:
27 /// Saves the contents in binary form to inStream.
28 void SaveBinaryState(StreamOut &inStream) const;
29
30 /// Restores the contents in binary form to inStream.
31 void RestoreBinaryState(StreamIn &inStream);
32
33 ETransmissionMode mMode = ETransmissionMode::Auto; ///< How to switch gears
34 Array<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.
35 Array<float> mReverseGearRatios { -2.90f }; ///< Ratio in rotation rate between engine and gear box when driving in reverse
36 float mSwitchTime = 0.5f; ///< How long it takes to switch gears (s), only used in auto mode
37 float mClutchReleaseTime = 0.3f; ///< How long it takes to release the clutch (go to full friction), only used in auto mode
38 float mSwitchLatency = 0.5f; ///< How long to wait after releasing the clutch before another switch is attempted (s), only used in auto mode
39 float mShiftUpRPM = 4000.0f; ///< If RPM of engine is bigger then this we will shift a gear up, only used in auto mode
40 float mShiftDownRPM = 2000.0f; ///< If RPM of engine is smaller then this we will shift a gear down, only used in auto mode
41 float 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)
42};
43
44/// Runtime data for transmission
45class JPH_EXPORT VehicleTransmission : public VehicleTransmissionSettings
46{
47public:
48 /// Set input from driver regarding the transmission (only relevant when transmission is set to manual mode)
49 /// @param inCurrentGear Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.
50 /// @param inClutchFriction Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)
51 void Set(int inCurrentGear, float inClutchFriction) { mCurrentGear = inCurrentGear; mClutchFriction = inClutchFriction; }
52
53 /// Update the current gear and clutch friction if the transmission is in auto mode
54 /// @param inDeltaTime Time step delta time in s
55 /// @param inCurrentRPM Current RPM for engine
56 /// @param inForwardInput Hint if the user wants to drive forward (> 0) or backwards (< 0)
57 /// @param inCanShiftUp Indicates if we want to allow the transmission to shift up (e.g. pass false if wheels are slipping)
58 void Update(float inDeltaTime, float inCurrentRPM, float inForwardInput, bool inCanShiftUp);
59
60 /// Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.
61 int GetCurrentGear() const { return mCurrentGear; }
62
63 /// Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)
64 float GetClutchFriction() const { return mClutchFriction; }
65
66 /// If the auto box is currently switching gears
67 bool IsSwitchingGear() const { return mGearSwitchTimeLeft > 0.0f; }
68
69 /// Return the transmission ratio based on the current gear (ratio between engine and differential)
70 float GetCurrentRatio() const;
71
72 /// Only allow sleeping when the transmission is idle
73 bool AllowSleep() const { return mGearSwitchTimeLeft <= 0.0f && mClutchReleaseTimeLeft <= 0.0f && mGearSwitchLatencyTimeLeft <= 0.0f; }
74
75 /// Saving state for replay
76 void SaveState(StateRecorder &inStream) const;
77 void RestoreState(StateRecorder &inStream);
78
79private:
80 int mCurrentGear = 0; ///< Current gear, -1 = reverse, 0 = neutral, 1 = 1st gear etc.
81 float mClutchFriction = 1.0f; ///< Value between 0 and 1 indicating how much friction the clutch gives (0 = no friction, 1 = full friction)
82 float 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)
83 float 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)
84 float mGearSwitchLatencyTimeLeft = 0.0f; ///< After releasing the clutch this will be > 0 and will prevent another gear switch (used for automatic gear switching only)
85};
86
87JPH_NAMESPACE_END