Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Vehicle/VehicleEngine.cpp
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#include <Jolt/Jolt.h>56#include <Jolt/Physics/Vehicle/VehicleEngine.h>7#include <Jolt/ObjectStream/TypeDeclarations.h>8#ifdef JPH_DEBUG_RENDERER9#include <Jolt/Renderer/DebugRenderer.h>10#endif // JPH_DEBUG_RENDERER1112JPH_NAMESPACE_BEGIN1314JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(VehicleEngineSettings)15{16JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mMaxTorque)17JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mMinRPM)18JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mMaxRPM)19JPH_ADD_ATTRIBUTE(VehicleEngineSettings, mNormalizedTorque)20}2122VehicleEngineSettings::VehicleEngineSettings()23{24mNormalizedTorque.Reserve(3);25mNormalizedTorque.AddPoint(0.0f, 0.8f);26mNormalizedTorque.AddPoint(0.66f, 1.0f);27mNormalizedTorque.AddPoint(1.0f, 0.8f);28}2930void VehicleEngineSettings::SaveBinaryState(StreamOut &inStream) const31{32inStream.Write(mMaxTorque);33inStream.Write(mMinRPM);34inStream.Write(mMaxRPM);35mNormalizedTorque.SaveBinaryState(inStream);36}3738void VehicleEngineSettings::RestoreBinaryState(StreamIn &inStream)39{40inStream.Read(mMaxTorque);41inStream.Read(mMinRPM);42inStream.Read(mMaxRPM);43mNormalizedTorque.RestoreBinaryState(inStream);44}4546void VehicleEngine::ApplyTorque(float inTorque, float inDeltaTime)47{48// Accelerate engine using torque49mCurrentRPM += cAngularVelocityToRPM * inTorque * inDeltaTime / mInertia;50ClampRPM();51}5253void VehicleEngine::ApplyDamping(float inDeltaTime)54{55// Angular damping: dw/dt = -c * w56// Solution: w(t) = w(0) * e^(-c * t) or w2 = w1 * e^(-c * dt)57// Taylor expansion of e^(-c * dt) = 1 - c * dt + ...58// Since dt is usually in the order of 1/60 and c is a low number too this approximation is good enough59mCurrentRPM *= max(0.0f, 1.0f - mAngularDamping * inDeltaTime);60ClampRPM();61}6263#ifdef JPH_DEBUG_RENDERER6465void VehicleEngine::DrawRPM(DebugRenderer *inRenderer, RVec3Arg inPosition, Vec3Arg inForward, Vec3Arg inUp, float inSize, float inShiftDownRPM, float inShiftUpRPM) const66{67// Function to draw part of a pie68auto draw_pie = [this, inRenderer, inSize, inPosition, inForward, inUp](float inMinRPM, float inMaxRPM, Color inColor) {69inRenderer->DrawPie(inPosition, inSize, inForward, inUp, ConvertRPMToAngle(inMinRPM), ConvertRPMToAngle(inMaxRPM), inColor, DebugRenderer::ECastShadow::Off);70};7172// Draw segment under min RPM73draw_pie(0, mMinRPM, Color::sGrey);7475// Draw segment until inShiftDownRPM76if (mCurrentRPM < inShiftDownRPM)77{78draw_pie(mMinRPM, mCurrentRPM, Color::sRed);79draw_pie(mCurrentRPM, inShiftDownRPM, Color::sDarkRed);80}81else82{83draw_pie(mMinRPM, inShiftDownRPM, Color::sRed);84}8586// Draw segment between inShiftDownRPM and inShiftUpRPM87if (mCurrentRPM > inShiftDownRPM && mCurrentRPM < inShiftUpRPM)88{89draw_pie(inShiftDownRPM, mCurrentRPM, Color::sOrange);90draw_pie(mCurrentRPM, inShiftUpRPM, Color::sDarkOrange);91}92else93{94draw_pie(inShiftDownRPM, inShiftUpRPM, mCurrentRPM <= inShiftDownRPM? Color::sDarkOrange : Color::sOrange);95}9697// Draw segment above inShiftUpRPM98if (mCurrentRPM > inShiftUpRPM)99{100draw_pie(inShiftUpRPM, mCurrentRPM, Color::sGreen);101draw_pie(mCurrentRPM, mMaxRPM, Color::sDarkGreen);102}103else104{105draw_pie(inShiftUpRPM, mMaxRPM, Color::sDarkGreen);106}107}108109#endif // JPH_DEBUG_RENDERER110111void VehicleEngine::SaveState(StateRecorder &inStream) const112{113inStream.Write(mCurrentRPM);114}115116void VehicleEngine::RestoreState(StateRecorder &inStream)117{118inStream.Read(mCurrentRPM);119}120121JPH_NAMESPACE_END122123124