From 90c624bc8caf441c13fb364fcfa49eac43768ace Mon Sep 17 00:00:00 2001 From: glopezdiest <58212725+glopezdiest@users.noreply.github.com> Date: Wed, 1 Jun 2022 15:11:29 +0200 Subject: [PATCH] Updated the information that traffic lights sent to vehicles --- CHANGELOG.md | 3 ++ .../Source/Carla/Traffic/TrafficLightBase.cpp | 7 +--- .../Carla/Traffic/TrafficLightComponent.cpp | 40 +++++++++++-------- .../Carla/Traffic/TrafficLightComponent.h | 10 ++++- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5976c8869..4ee38acb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## Latest + * Fixed bug at `Vehicle.get_traffic_light_state()` and `Vehicle.is_at_traffic_light()` causing vehicles to temporarily not lose the information of a traffic light if they moved away from it before it turned green. + * Fixed bug causing the `Vehicle.get_traffic_light_state()` function not notify about the green to yellow and yellow to red light state changes. + * Fixed bug causing the `Vehicle.is_at_traffic_light()` function to return *false* if the traffic light was green. * Fixed bug causing the scene lights to return an incorrect location at large maps. * Fixed bug causing the `world.ground_projection()` function to return an incorrect location at large maps. * Added failure state to vehicles, which can be retrieved by using `Vehicle.get_failure_state()`. Only Rollover failure state is currently supported. diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp index 877c38e6e..5f7ca1253 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp @@ -78,11 +78,8 @@ void ATrafficLightBase::NotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle) if (Controller != nullptr) { Controller->SetTrafficLightState(GetTrafficLightState()); - if (GetTrafficLightState() != ETrafficLightState::Green) - { - Vehicles.Add(Controller); - Controller->SetTrafficLight(this); - } + Vehicles.Add(Controller); + Controller->SetTrafficLight(this); } } } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp index 3cb94f9cf..aab65384e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp @@ -93,7 +93,8 @@ void UTrafficLightComponent::GenerateTrafficLightBox(const FTransform BoxTransfo const FVector BoxSize) { UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize); - BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UTrafficLightComponent::OnOverlapTriggerBox); + BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UTrafficLightComponent::OnBeginOverlapTriggerBox); + BoxComponent->OnComponentEndOverlap.AddDynamic(this, &UTrafficLightComponent::OnEndOverlapTriggerBox); AddEffectTriggerVolume(BoxComponent); } @@ -116,16 +117,8 @@ void UTrafficLightComponent::SetLightState(ETrafficLightState NewState) if (Controller != nullptr) { Controller->SetTrafficLightState(LightState); - if (LightState == ETrafficLightState::Green) - { - Controller->SetTrafficLight(nullptr); - } } } - if (LightState == ETrafficLightState::Green) - { - Vehicles.Empty(); - } } ETrafficLightState UTrafficLightComponent::GetLightState() const @@ -161,7 +154,7 @@ const UTrafficLightController* UTrafficLightComponent::GetController() const return TrafficLightController; } -void UTrafficLightComponent::OnOverlapTriggerBox(UPrimitiveComponent *OverlappedComp, +void UTrafficLightComponent::OnBeginOverlapTriggerBox(UPrimitiveComponent *OverlappedComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, @@ -176,12 +169,27 @@ void UTrafficLightComponent::OnOverlapTriggerBox(UPrimitiveComponent *Overlapped if (VehicleController) { VehicleController->SetTrafficLightState(LightState); - if (LightState != ETrafficLightState::Green) - { - Vehicles.Add(VehicleController); - VehicleController->SetTrafficLight( - Cast(GetOwner())); - } + Vehicles.Add(VehicleController); + VehicleController->SetTrafficLight(Cast(GetOwner())); + } + } +} + +void UTrafficLightComponent::OnEndOverlapTriggerBox(UPrimitiveComponent *OverlappedComp, + AActor *OtherActor, + UPrimitiveComponent *OtherComp, + int32 OtherBodyIndex) +{ + ACarlaWheeledVehicle * Vehicle = Cast(OtherActor); + if (Vehicle) + { + AWheeledVehicleAIController* VehicleController = + Cast(Vehicle->GetController()); + if (VehicleController) + { + VehicleController->SetTrafficLightState(ETrafficLightState::Green); + VehicleController->SetTrafficLight(nullptr); + Vehicles.Remove(VehicleController); } } } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h index 2efc79770..8038d54c4 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h @@ -55,13 +55,21 @@ public: protected: UFUNCTION(BlueprintCallable) - void OnOverlapTriggerBox(UPrimitiveComponent *OverlappedComp, + void OnBeginOverlapTriggerBox(UPrimitiveComponent *OverlappedComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult); + UFUNCTION(BlueprintCallable) + void OnEndOverlapTriggerBox(UPrimitiveComponent *OverlappedComp, + AActor *OtherActor, + UPrimitiveComponent *OtherComp, + int32 OtherBodyIndex); + + + private: friend ATrafficLightManager;