From 65bc5bd2fdabf203126189d17060f611a31210ba Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Mon, 29 Aug 2022 07:33:43 +0100 Subject: [PATCH] vehicle: Implement per-wheel brake torque Based upon the same calculations that regular VPhysics does, calculate the per-wheel brake torque. Improves vehicle feel and matches it more closely. --- vphysics_jolt/vjolt_controller_vehicle.cpp | 15 +++++++++++++++ vphysics_jolt/vjolt_controller_vehicle.h | 1 + 2 files changed, 16 insertions(+) diff --git a/vphysics_jolt/vjolt_controller_vehicle.cpp b/vphysics_jolt/vjolt_controller_vehicle.cpp index a53e0c7..8fd524e 100644 --- a/vphysics_jolt/vjolt_controller_vehicle.cpp +++ b/vphysics_jolt/vjolt_controller_vehicle.cpp @@ -403,6 +403,9 @@ void JoltPhysicsVehicleController::CreateWheel( JPH::VehicleConstraintSettings & const float steeringAngle = DEG2RAD( Max( m_VehicleParams.steering.degreesSlow, m_VehicleParams.steering.degreesFast ) ); const float additionalLength = SourceToJolt::Distance( axle.wheels.springAdditionalLength ); + Vector gravity; + m_pEnvironment->GetGravity( &gravity ); + JPH::WheelSettingsWV *wheelSettings = new JPH::WheelSettingsWV; wheelSettings->mPosition = SourceToJolt::Distance( wheelPositionLocal ); wheelSettings->mDirection = JPH::Vec3( 0, 0, -1 ); @@ -431,6 +434,14 @@ void JoltPhysicsVehicleController::CreateWheel( JPH::VehicleConstraintSettings & wheelSettings->mLongitudinalFriction.AddPoint( 1.0f, axle.wheels.frictionScale ); } + // TODO: We may want to update this every pre-simulation to account for changing gravity. + wheelSettings->mMaxBrakeTorque = + 0.5f * + SourceToJolt::Distance( gravity.Length() ) * + ( m_pCarBodyObject->GetMass() + m_TotalWheelMass ) * + axle.brakeFactor * + SourceToJolt::Distance( axle.wheels.radius ); + vehicleSettings.mWheels.push_back( wheelSettings ); m_InternalState.LargestWheelRadius = Max( m_InternalState.LargestWheelRadius, SourceToJolt::Distance( wheelWidth ) ); } @@ -442,6 +453,10 @@ void JoltPhysicsVehicleController::CreateWheels( JPH::VehicleConstraintSettings m_Wheels.reserve( m_VehicleParams.axleCount * m_VehicleParams.wheelsPerAxle ); vehicleSettings.mAntiRollBars.reserve( m_VehicleParams.axleCount ); + m_TotalWheelMass = 0.0f; + for ( int axle = 0; axle < m_VehicleParams.axleCount; axle++ ) + m_TotalWheelMass += m_VehicleParams.axles[ axle ].wheels.mass * m_VehicleParams.wheelsPerAxle; + for ( int axle = 0; axle < m_VehicleParams.axleCount; axle++ ) { for ( int wheel = 0; wheel < m_VehicleParams.wheelsPerAxle; wheel++ ) diff --git a/vphysics_jolt/vjolt_controller_vehicle.h b/vphysics_jolt/vjolt_controller_vehicle.h index b9cd03f..3cfb44b 100644 --- a/vphysics_jolt/vjolt_controller_vehicle.h +++ b/vphysics_jolt/vjolt_controller_vehicle.h @@ -83,6 +83,7 @@ private: std::vector< JoltPhysicsWheel > m_Wheels; + float m_TotalWheelMass = 0.0f; JoltPhysicsInternalVehicleState m_InternalState; JPH::Ref< JPH::VehicleConstraint > m_VehicleConstraint;