From ed6896ed50ee443d27dee88e5b461722cdf79250 Mon Sep 17 00:00:00 2001 From: glopezdiest <58212725+glopezdiest@users.noreply.github.com> Date: Mon, 11 Nov 2024 13:20:37 +0100 Subject: [PATCH] Fixed VehiclePhysicsControl (#8354) --- PythonAPI/carla/src/Control.cpp | 50 +++++++++---------- .../Carla/Vehicle/CarlaWheeledVehicle.cpp | 38 +++++++++++--- .../Carla/Vehicle/CarlaWheeledVehicle.h | 4 ++ 3 files changed, 60 insertions(+), 32 deletions(-) diff --git a/PythonAPI/carla/src/Control.cpp b/PythonAPI/carla/src/Control.cpp index cd39e6593..482e13dc9 100644 --- a/PythonAPI/carla/src/Control.cpp +++ b/PythonAPI/carla/src/Control.cpp @@ -455,35 +455,35 @@ void export_control() { .def("__init__", raw_function(VehiclePhysicsControl_init)) .def(init<>()) .add_property("torque_curve", &GetTorqueCurve, &SetTorqueCurve) - .add_property("max_torque", &cr::VehiclePhysicsControl::max_torque) - .add_property("max_rpm", &cr::VehiclePhysicsControl::max_rpm) - .add_property("idle_rpm", &cr::VehiclePhysicsControl::idle_rpm) - .add_property("brake_effect", &cr::VehiclePhysicsControl::brake_effect) - .add_property("rev_up_moi", &cr::VehiclePhysicsControl::rev_up_moi) - .add_property("rev_down_rate", &cr::VehiclePhysicsControl::rev_down_rate) - .add_property("differential_type", &cr::VehiclePhysicsControl::differential_type) - .add_property("front_rear_split", &cr::VehiclePhysicsControl::front_rear_split) - .add_property("use_automatic_gears", &cr::VehiclePhysicsControl::use_automatic_gears) - .add_property("gear_change_time", &cr::VehiclePhysicsControl::gear_change_time) - .add_property("final_ratio", &cr::VehiclePhysicsControl::final_ratio) + .def_readwrite("max_torque", &cr::VehiclePhysicsControl::max_torque) + .def_readwrite("max_rpm", &cr::VehiclePhysicsControl::max_rpm) + .def_readwrite("idle_rpm", &cr::VehiclePhysicsControl::idle_rpm) + .def_readwrite("brake_effect", &cr::VehiclePhysicsControl::brake_effect) + .def_readwrite("rev_up_moi", &cr::VehiclePhysicsControl::rev_up_moi) + .def_readwrite("rev_down_rate", &cr::VehiclePhysicsControl::rev_down_rate) + .def_readwrite("differential_type", &cr::VehiclePhysicsControl::differential_type) + .def_readwrite("front_rear_split", &cr::VehiclePhysicsControl::front_rear_split) + .def_readwrite("use_automatic_gears", &cr::VehiclePhysicsControl::use_automatic_gears) + .def_readwrite("gear_change_time", &cr::VehiclePhysicsControl::gear_change_time) + .def_readwrite("final_ratio", &cr::VehiclePhysicsControl::final_ratio) .add_property("forward_gear_ratios", &GetForwardGearRatios, &SetForwardGearRatios) .add_property("reverse_gear_ratios", &GetReverseGearRatios, &SetReverseGearRatios) - .add_property("change_up_rpm", &cr::VehiclePhysicsControl::change_up_rpm) - .add_property("change_down_rpm", &cr::VehiclePhysicsControl::change_down_rpm) - .add_property("transmission_efficiency", &cr::VehiclePhysicsControl::transmission_efficiency) - .add_property("mass", &cr::VehiclePhysicsControl::mass) - .add_property("drag_coefficient", &cr::VehiclePhysicsControl::drag_coefficient) - .add_property("center_of_mass", &cr::VehiclePhysicsControl::center_of_mass) - .add_property("chassis_width", &cr::VehiclePhysicsControl::chassis_width) - .add_property("chassis_height", &cr::VehiclePhysicsControl::chassis_height) - .add_property("downforce_coefficient", &cr::VehiclePhysicsControl::downforce_coefficient) - .add_property("drag_area", &cr::VehiclePhysicsControl::drag_area) - .add_property("inertia_tensor_scale", &cr::VehiclePhysicsControl::inertia_tensor_scale) - .add_property("sleep_threshold", &cr::VehiclePhysicsControl::sleep_threshold) - .add_property("sleep_slope_limit", &cr::VehiclePhysicsControl::sleep_slope_limit) + .def_readwrite("change_up_rpm", &cr::VehiclePhysicsControl::change_up_rpm) + .def_readwrite("change_down_rpm", &cr::VehiclePhysicsControl::change_down_rpm) + .def_readwrite("transmission_efficiency", &cr::VehiclePhysicsControl::transmission_efficiency) + .def_readwrite("mass", &cr::VehiclePhysicsControl::mass) + .def_readwrite("drag_coefficient", &cr::VehiclePhysicsControl::drag_coefficient) + .def_readwrite("center_of_mass", &cr::VehiclePhysicsControl::center_of_mass) + .def_readwrite("chassis_width", &cr::VehiclePhysicsControl::chassis_width) + .def_readwrite("chassis_height", &cr::VehiclePhysicsControl::chassis_height) + .def_readwrite("downforce_coefficient", &cr::VehiclePhysicsControl::downforce_coefficient) + .def_readwrite("drag_area", &cr::VehiclePhysicsControl::drag_area) + .def_readwrite("inertia_tensor_scale", &cr::VehiclePhysicsControl::inertia_tensor_scale) + .def_readwrite("sleep_threshold", &cr::VehiclePhysicsControl::sleep_threshold) + .def_readwrite("sleep_slope_limit", &cr::VehiclePhysicsControl::sleep_slope_limit) .add_property("steering_curve", &GetSteeringCurve, &SetSteeringCurve) .add_property("wheels", &GetWheels, &SetWheels) - .add_property("use_sweep_wheel_collision", &cr::VehiclePhysicsControl::use_sweep_wheel_collision) + .def_readwrite("use_sweep_wheel_collision", &cr::VehiclePhysicsControl::use_sweep_wheel_collision) .def("__eq__", &cr::VehiclePhysicsControl::operator==) .def("__ne__", &cr::VehiclePhysicsControl::operator!=) .def(self_ns::str(self_ns::self)) diff --git a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp index dacdad724..0e15774c2 100644 --- a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp +++ b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp @@ -387,9 +387,7 @@ FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl() const PhysicsControl.TransmissionEfficiency = TransmissionSetup.TransmissionEfficiency; PhysicsControl.Mass = VehicleMovComponent.Mass; PhysicsControl.DragCoefficient = VehicleMovComponent.DragCoefficient; - auto PrimitiveComponentPtr = Cast(VehicleMovComponent.UpdatedComponent); - check(PrimitiveComponentPtr != nullptr); - PhysicsControl.CenterOfMass = PrimitiveComponentPtr->GetCenterOfMass(); + PhysicsControl.CenterOfMass = GetCenterOfMass(VehicleMovComponent); PhysicsControl.ChassisWidth = VehicleMovComponent.ChassisWidth; PhysicsControl.ChassisHeight = VehicleMovComponent.ChassisHeight; PhysicsControl.DownforceCoefficient = VehicleMovComponent.DownforceCoefficient; @@ -455,6 +453,20 @@ FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl() const return PhysicsControl; } +FVector ACarlaWheeledVehicle::GetCenterOfMass(UChaosWheeledVehicleMovementComponent& VehicleMovComponent) const +{ + if (VehicleMovComponent.bEnableCenterOfMassOverride) + { + return VehicleMovComponent.CenterOfMassOverride; + } + else { + auto PrimitiveComponentPtr = Cast(VehicleMovComponent.UpdatedComponent); + check(PrimitiveComponentPtr != nullptr); + auto& PrimitiveComponent = *PrimitiveComponentPtr; + return PrimitiveComponent.BodyInstance.COMNudge; + } +} + FVehicleLightState ACarlaWheeledVehicle::GetVehicleLightState() const { return InputControl.LightState; @@ -494,10 +506,7 @@ void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl( TransmissionSetup.TransmissionEfficiency = PhysicsControl.TransmissionEfficiency; VehicleMovComponent.Mass = PhysicsControl.Mass; VehicleMovComponent.DragCoefficient = PhysicsControl.DragCoefficient; - auto PrimitiveComponentPtr = Cast(VehicleMovComponent.UpdatedComponent); - check(PrimitiveComponentPtr != nullptr); - auto& PrimitiveComponent = *PrimitiveComponentPtr; - PrimitiveComponent.SetCenterOfMass(PhysicsControl.CenterOfMass); + SetCenterOfMass(VehicleMovComponent, PhysicsControl); VehicleMovComponent.ChassisWidth = PhysicsControl.ChassisWidth; VehicleMovComponent.ChassisHeight = PhysicsControl.ChassisHeight; VehicleMovComponent.DownforceCoefficient = PhysicsControl.DownforceCoefficient; @@ -573,6 +582,7 @@ void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl( ESweepShape::Raycast; } + VehicleMovComponent.RecreatePhysicsState(); ResetConstraints(); auto* Recorder = UCarlaStatics::GetRecorder(GetWorld()); @@ -585,6 +595,20 @@ void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl( AckermannController.UpdateVehiclePhysics(this); } +void ACarlaWheeledVehicle::SetCenterOfMass(UChaosWheeledVehicleMovementComponent& VehicleMovComponent, const FVehiclePhysicsControl& PhysicsControl) +{ + if (VehicleMovComponent.bEnableCenterOfMassOverride) + { + VehicleMovComponent.CenterOfMassOverride = PhysicsControl.CenterOfMass; + } + else { + auto PrimitiveComponentPtr = Cast(VehicleMovComponent.UpdatedComponent); + check(PrimitiveComponentPtr != nullptr); + auto& PrimitiveComponent = *PrimitiveComponentPtr; + PrimitiveComponent.BodyInstance.COMNudge = PhysicsControl.CenterOfMass; + } +} + void ACarlaWheeledVehicle::ApplyVehicleControl(const FVehicleControl& Control, EVehicleInputPriority Priority) { if (bAckermannControlActive) { diff --git a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h index 0425d3b70..67b95f4ef 100644 --- a/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h +++ b/Unreal/CarlaUnreal/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.h @@ -177,6 +177,8 @@ public: UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable) FVehiclePhysicsControl GetVehiclePhysicsControl() const; + FVector GetCenterOfMass(UChaosWheeledVehicleMovementComponent &VehicleMovComponent) const; + UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable) FAckermannControllerSettings GetAckermannControllerSettings() const { return AckermannController.GetSettings(); @@ -190,6 +192,8 @@ public: void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl); + void SetCenterOfMass(UChaosWheeledVehicleMovementComponent &VehicleMovComponent, const FVehiclePhysicsControl &PhysicsControl); + void ApplyAckermannControllerSettings(const FAckermannControllerSettings &AckermannControllerSettings) { return AckermannController.ApplySettings(AckermannControllerSettings); }