From da9e8d41366f200229dc63bdc92f93a2d3ea8ed9 Mon Sep 17 00:00:00 2001 From: Manish Date: Mon, 6 May 2019 16:12:27 +0200 Subject: [PATCH] Fixing friction bug + refactor --- .../Actor/ActorBlueprintFunctionLibrary.cpp | 4 +- .../Source/Carla/Trigger/FrictionTrigger.cpp | 7 ++- .../Source/Carla/Trigger/FrictionTrigger.h | 4 +- .../Source/Carla/Trigger/TriggerFactory.cpp | 5 +-- .../Carla/Vehicle/CarlaWheeledVehicle.cpp | 45 ++++++++++++++----- .../Carla/Vehicle/WheelPhysicsControl.h | 6 +-- 6 files changed, 47 insertions(+), 24 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp index 6b92a5d8b..acca66ec1 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp @@ -236,7 +236,7 @@ static void AddVariationsForTrigger(FActorDefinition &Def) FActorVariation Friction; Friction.Id = FString("friction"); Friction.Type = EActorAttributeType::Float; - Friction.RecommendedValues = { TEXT("0.0f") }; + Friction.RecommendedValues = { TEXT("3.5f") }; Friction.bRestrictToRecommended = false; Def.Variations.Emplace(Friction); @@ -250,7 +250,7 @@ static void AddVariationsForTrigger(FActorDefinition &Def) ExtentCoordinate.Id = JoinStrings(TEXT("_"), Extent, Coordinate); ExtentCoordinate.Type = EActorAttributeType::Float; - ExtentCoordinate.RecommendedValues = { TEXT("0.0f") }; + ExtentCoordinate.RecommendedValues = { TEXT("1.0f") }; ExtentCoordinate.bRestrictToRecommended = false; Def.Variations.Emplace(ExtentCoordinate); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.cpp index c08667faa..c6012959d 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.cpp @@ -80,12 +80,17 @@ void AFrictionTrigger::BeginPlay() void AFrictionTrigger::EndPlay(const EEndPlayReason::Type EndPlayReason) { - // Deregister the delegate + // Deregister delegates if (TriggerVolume->OnComponentBeginOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerBeginOverlap)) { TriggerVolume->OnComponentBeginOverlap.RemoveDynamic(this, &AFrictionTrigger::OnTriggerBeginOverlap); } + if (TriggerVolume->OnComponentEndOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerEndOverlap)) + { + TriggerVolume->OnComponentEndOverlap.RemoveDynamic(this, &AFrictionTrigger::OnTriggerEndOverlap); + } + Super::EndPlay(EndPlayReason); } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.h index 68d8e7ea3..3d0230ce2 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.h @@ -61,6 +61,8 @@ protected: virtual void EndPlay(EEndPlayReason::Type EndPlayReason) override; + virtual void Tick(float DeltaTime) override; + public: UPROPERTY(EditAnywhere) @@ -69,7 +71,5 @@ public: UPROPERTY(EditAnywhere) UBoxComponent *TriggerVolume; - virtual void Tick(float DeltaTime) override; - const UCarlaEpisode *Episode = nullptr; }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.cpp index 947f2425f..0ae9761cf 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.cpp @@ -11,8 +11,6 @@ #include "Carla/Game/CarlaStatics.h" #include "Carla/Trigger/FrictionTrigger.h" -#include "Carla/Game/CarlaGameInstance.h" - // ============================================================================= // -- ASensorFactory ----------------------------------------------------------- // ============================================================================= @@ -48,7 +46,6 @@ FActorSpawnResult ATriggerFactory::SpawnActor( return {}; } - UE_LOG(LogCarla, Log, TEXT("%s"), *Description.Class->GetName()); auto *Trigger = World->SpawnActorDeferred( Description.Class, Transform, @@ -70,7 +67,7 @@ FActorSpawnResult ATriggerFactory::SpawnActor( // Retrieve Friction float Friction = UActorBlueprintFunctionLibrary::RetrieveActorAttributeToFloat("friction", Description.Variations, - 0.0f); + 3.5f); Trigger->SetFriction(Friction); // Retrieve Extent diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp index f5c82cb33..59fc676fd 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/CarlaWheeledVehicle.cpp @@ -32,9 +32,23 @@ ACarlaWheeledVehicle::~ACarlaWheeledVehicle() {} void ACarlaWheeledVehicle::BeginPlay() { Super::BeginPlay(); + + // Check if it overlaps with a Friction trigger + float FrictionScale = 3.5f; + TArray OverlapActors; + GetOverlappingActors(OverlapActors, AFrictionTrigger::StaticClass()); + for (auto &Actor : OverlapActors) + { + AFrictionTrigger* FrictionTrigger = Cast(Actor); + if (FrictionTrigger) { + FrictionScale = FrictionTrigger->Friction; + } + } + // Setup Tire Configs - UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked( + UWheeledVehicleMovementComponent4W *Vehicle4W = Cast( GetVehicleMovementComponent()); + check (Vehicle4W != nullptr); // Wheels Setup TArray NewWheelSetups = Vehicle4W->WheelSetups; @@ -44,15 +58,14 @@ void ACarlaWheeledVehicle::BeginPlay() UVehicleWheel *Wheel = NewWheelSetups[i].WheelClass.GetDefaultObject(); // Assigning new tire config - float Friction = Wheel->TireConfig->GetFrictionScale(); Wheel->TireConfig = NewObject(); // Setting a new value to friction - Wheel->TireConfig->SetFrictionScale(Friction); + Wheel->TireConfig->SetFrictionScale(FrictionScale); } Vehicle4W->WheelSetups = NewWheelSetups; - Vehicle4W->VehicleSetupTag++; + Vehicle4W->RecreatePhysicsState(); } // ============================================================================= @@ -160,8 +173,9 @@ void ACarlaWheeledVehicle::SetHandbrakeInput(const bool Value) TArray ACarlaWheeledVehicle::GetWheelsFrictionScale() { - UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked( + UWheeledVehicleMovementComponent4W *Vehicle4W = Cast( GetVehicleMovement()); + check (Vehicle4W != nullptr); TArray WheelsFrictionScale; for (auto &Wheel : Vehicle4W->Wheels) @@ -173,8 +187,10 @@ TArray ACarlaWheeledVehicle::GetWheelsFrictionScale() void ACarlaWheeledVehicle::SetWheelsFrictionScale(TArray &WheelsFrictionScale) { - UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked( + UWheeledVehicleMovementComponent4W *Vehicle4W = Cast( GetVehicleMovement()); + check (Vehicle4W != nullptr); + for (int32 i = 0; i < Vehicle4W->Wheels.Num(); ++i) { Vehicle4W->Wheels[i]->TireConfig->SetFrictionScale(WheelsFrictionScale[i]); @@ -183,8 +199,9 @@ void ACarlaWheeledVehicle::SetWheelsFrictionScale(TArray &WheelsFrictionS FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl() { - UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked( + UWheeledVehicleMovementComponent4W *Vehicle4W = Cast( GetVehicleMovement()); + check(Vehicle4W != nullptr); FVehiclePhysicsControl PhysicsControl; @@ -210,6 +227,8 @@ FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl() // Center of mass offset (Center of mass is always zero vector in local // position) UPrimitiveComponent *UpdatedPrimitive = Cast(Vehicle4W->UpdatedComponent); + check(UpdatedPrimitive != nullptr); + PhysicsControl.CenterOfMass = UpdatedPrimitive->BodyInstance.COMNudge; // Transmission Setup @@ -239,8 +258,9 @@ FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl() void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl) { - UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked( + UWheeledVehicleMovementComponent4W *Vehicle4W = Cast( GetVehicleMovement()); + check(Vehicle4W != nullptr); // Engine Setup Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData = PhysicsControl.TorqueCurve; @@ -271,8 +291,9 @@ void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl(const FVehiclePhysicsContr Vehicle4W->SteeringCurve.EditorCurveData = PhysicsControl.SteeringCurve; // Wheels Setup - TArray NewWheelSetups; + check(Vehicle4W->WheelSetups.Num() == PhysicsControl.Wheels.Num()); + TArray NewWheelSetups; for (int32 i = 0; i < Vehicle4W->WheelSetups.Num(); ++i) { FWheelSetup WheelSetup = Vehicle4W->WheelSetups[i]; @@ -284,9 +305,9 @@ void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl(const FVehiclePhysicsContr WheelSetup.bDisableSteering = !PhysicsControl.Wheels[i].IsSteerable; - NewWheelSetups.Add(WheelSetup); + NewWheelSetups.Add(std::move(WheelSetup)); } Vehicle4W->WheelSetups = NewWheelSetups; - Vehicle4W->VehicleSetupTag++; -} + Vehicle4W->RecreatePhysicsState(); +} \ No newline at end of file diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/WheelPhysicsControl.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/WheelPhysicsControl.h index ef9788dbd..c719364b7 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/WheelPhysicsControl.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/WheelPhysicsControl.h @@ -14,13 +14,13 @@ struct CARLA_API FWheelPhysicsControl GENERATED_BODY() UPROPERTY(Category = "Wheel Tire Friction", EditAnywhere, BlueprintReadWrite) - float TireFriction = 0.0f; + float TireFriction = 3.5f; UPROPERTY(Category = "Wheel Damping Rate", EditAnywhere, BlueprintReadWrite) - float DampingRate = 0.0f; + float DampingRate = 1.0f; UPROPERTY(Category = "Wheel Max Steer Angle", EditAnywhere, BlueprintReadWrite) - float MaxSteerAngle = 0.0f; + float MaxSteerAngle = 70.0f; UPROPERTY(Category = "Wheel Is Steerable", EditAnywhere, BlueprintReadWrite) bool IsSteerable = true;