Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Vehicle/TrackedVehicleController.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/Physics/Vehicle/VehicleConstraint.h>7#include <Jolt/Physics/Vehicle/VehicleController.h>8#include <Jolt/Physics/Vehicle/VehicleEngine.h>9#include <Jolt/Physics/Vehicle/VehicleTransmission.h>10#include <Jolt/Physics/Vehicle/VehicleTrack.h>1112JPH_NAMESPACE_BEGIN1314class PhysicsSystem;1516/// WheelSettings object specifically for TrackedVehicleController17class JPH_EXPORT WheelSettingsTV : public WheelSettings18{19JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, WheelSettingsTV)2021public:22// See: WheelSettings23virtual void SaveBinaryState(StreamOut &inStream) const override;24virtual void RestoreBinaryState(StreamIn &inStream) override;2526float mLongitudinalFriction = 4.0f; ///< Friction in forward direction of tire27float mLateralFriction = 2.0f; ///< Friction in sideways direction of tire28};2930/// Wheel object specifically for TrackedVehicleController31class JPH_EXPORT WheelTV : public Wheel32{33public:34JPH_OVERRIDE_NEW_DELETE3536/// Constructor37explicit WheelTV(const WheelSettingsTV &inWheel);3839/// Override GetSettings and cast to the correct class40const WheelSettingsTV * GetSettings() const { return StaticCast<WheelSettingsTV>(mSettings); }4142/// Update the angular velocity of the wheel based on the angular velocity of the track43void CalculateAngularVelocity(const VehicleConstraint &inConstraint);4445/// Update the wheel rotation based on the current angular velocity46void Update(uint inWheelIndex, float inDeltaTime, const VehicleConstraint &inConstraint);4748int mTrackIndex = -1; ///< Index in mTracks to which this wheel is attached (calculated on initialization)49float mCombinedLongitudinalFriction = 0.0f; ///< Combined friction coefficient in longitudinal direction (combines terrain and track)50float mCombinedLateralFriction = 0.0f; ///< Combined friction coefficient in lateral direction (combines terrain and track)51float mBrakeImpulse = 0.0f; ///< Amount of impulse that the brakes can apply to the floor (excluding friction), spread out from brake impulse applied on track52};5354/// Settings of a vehicle with tank tracks55///56/// Default settings are based around what I could find about the M1 Abrams tank.57/// Note to avoid issues with very heavy objects vs very light objects the mass of the tank should be a lot lower (say 10x) than that of a real tank. That means that the engine/brake torque is also 10x less.58class JPH_EXPORT TrackedVehicleControllerSettings : public VehicleControllerSettings59{60JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, TrackedVehicleControllerSettings)6162public:63// Constructor64TrackedVehicleControllerSettings();6566// See: VehicleControllerSettings67virtual VehicleController * ConstructController(VehicleConstraint &inConstraint) const override;68virtual void SaveBinaryState(StreamOut &inStream) const override;69virtual void RestoreBinaryState(StreamIn &inStream) override;7071VehicleEngineSettings mEngine; ///< The properties of the engine72VehicleTransmissionSettings mTransmission; ///< The properties of the transmission (aka gear box)73VehicleTrackSettings mTracks[(int)ETrackSide::Num]; ///< List of tracks and their properties74};7576/// Runtime controller class for vehicle with tank tracks77class JPH_EXPORT TrackedVehicleController : public VehicleController78{79public:80JPH_OVERRIDE_NEW_DELETE8182/// Constructor83TrackedVehicleController(const TrackedVehicleControllerSettings &inSettings, VehicleConstraint &inConstraint);8485/// Set input from driver86/// @param inForward Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed87/// @param inLeftRatio Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)88/// @param inRightRatio Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)89/// @param inBrake Value between 0 and 1 indicating how strong the brake pedal is pressed90void SetDriverInput(float inForward, float inLeftRatio, float inRightRatio, float inBrake) { JPH_ASSERT(inLeftRatio != 0.0f && inRightRatio != 0.0f); mForwardInput = inForward; mLeftRatio = inLeftRatio; mRightRatio = inRightRatio; mBrakeInput = inBrake; }9192/// Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed93void SetForwardInput(float inForward) { mForwardInput = inForward; }94float GetForwardInput() const { return mForwardInput; }9596/// Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)97void SetLeftRatio(float inLeftRatio) { JPH_ASSERT(inLeftRatio != 0.0f); mLeftRatio = inLeftRatio; }98float GetLeftRatio() const { return mLeftRatio; }99100/// Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)101void SetRightRatio(float inRightRatio) { JPH_ASSERT(inRightRatio != 0.0f); mRightRatio = inRightRatio; }102float GetRightRatio() const { return mRightRatio; }103104/// Value between 0 and 1 indicating how strong the brake pedal is pressed105void SetBrakeInput(float inBrake) { mBrakeInput = inBrake; }106float GetBrakeInput() const { return mBrakeInput; }107108/// Get current engine state109const VehicleEngine & GetEngine() const { return mEngine; }110111/// Get current engine state (writable interface, allows you to make changes to the configuration which will take effect the next time step)112VehicleEngine & GetEngine() { return mEngine; }113114/// Get current transmission state115const VehicleTransmission & GetTransmission() const { return mTransmission; }116117/// Get current transmission state (writable interface, allows you to make changes to the configuration which will take effect the next time step)118VehicleTransmission & GetTransmission() { return mTransmission; }119120/// Get the tracks this vehicle has121const VehicleTracks & GetTracks() const { return mTracks; }122123/// Get the tracks this vehicle has (writable interface, allows you to make changes to the configuration which will take effect the next time step)124VehicleTracks & GetTracks() { return mTracks; }125126#ifdef JPH_DEBUG_RENDERER127/// Debug drawing of RPM meter128void SetRPMMeter(Vec3Arg inPosition, float inSize) { mRPMMeterPosition = inPosition; mRPMMeterSize = inSize; }129#endif // JPH_DEBUG_RENDERER130131protected:132/// Synchronize angular velocities of left and right tracks according to their ratios133void SyncLeftRightTracks();134135// See: VehicleController136virtual Wheel * ConstructWheel(const WheelSettings &inWheel) const override { JPH_ASSERT(IsKindOf(&inWheel, JPH_RTTI(WheelSettingsTV))); return new WheelTV(static_cast<const WheelSettingsTV &>(inWheel)); }137virtual bool AllowSleep() const override;138virtual void PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;139virtual void PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;140virtual bool SolveLongitudinalAndLateralConstraints(float inDeltaTime) override;141virtual void SaveState(StateRecorder &inStream) const override;142virtual void RestoreState(StateRecorder &inStream) override;143#ifdef JPH_DEBUG_RENDERER144virtual void Draw(DebugRenderer *inRenderer) const override;145#endif // JPH_DEBUG_RENDERER146147// Control information148float mForwardInput = 0.0f; ///< Value between -1 and 1 for auto transmission and value between 0 and 1 indicating desired driving direction and amount the gas pedal is pressed149float mLeftRatio = 1.0f; ///< Value between -1 and 1 indicating an extra multiplier to the rotation rate of the left track (used for steering)150float mRightRatio = 1.0f; ///< Value between -1 and 1 indicating an extra multiplier to the rotation rate of the right track (used for steering)151float mBrakeInput = 0.0f; ///< Value between 0 and 1 indicating how strong the brake pedal is pressed152153// Simulation information154VehicleEngine mEngine; ///< Engine state of the vehicle155VehicleTransmission mTransmission; ///< Transmission state of the vehicle156VehicleTracks mTracks; ///< Tracks of the vehicle157158#ifdef JPH_DEBUG_RENDERER159// Debug settings160Vec3 mRPMMeterPosition { 0, 1, 0 }; ///< Position (in local space of the body) of the RPM meter when drawing the constraint161float mRPMMeterSize = 0.5f; ///< Size of the RPM meter when drawing the constraint162#endif // JPH_DEBUG_RENDERER163};164165JPH_NAMESPACE_END166167168