From 70442e2436f7ecd85870d82af61ee17dd7fd6979 Mon Sep 17 00:00:00 2001 From: Manish Date: Wed, 17 Apr 2019 17:53:29 +0200 Subject: [PATCH] Friction trigger box is getting spawned --- .../Actor/ActorBlueprintFunctionLibrary.cpp | 27 +++++++ .../Actor/ActorBlueprintFunctionLibrary.h | 5 ++ .../FrictionTrigger.cpp} | 61 ++++++++++------ .../FrictionTrigger.h} | 22 ++++-- .../Source/Carla/Trigger/TriggerFactory.cpp | 71 +++++++++++++++++++ .../Source/Carla/Trigger/TriggerFactory.h | 30 ++++++++ 6 files changed, 188 insertions(+), 28 deletions(-) rename Unreal/CarlaUE4/Plugins/Carla/Source/Carla/{Vehicle/FrictionLayer.cpp => Trigger/FrictionTrigger.cpp} (55%) rename Unreal/CarlaUE4/Plugins/Carla/Source/Carla/{Vehicle/FrictionLayer.h => Trigger/FrictionTrigger.h} (53%) create mode 100644 Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.cpp create mode 100644 Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.h diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp index 45f4d766b..aa2985ed9 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.cpp @@ -230,6 +230,23 @@ static void AddVariationsForSensor(FActorDefinition &Def) Def.Variations.Emplace(Tick); } +static void AddVariationsForTrigger(FActorDefinition &Def) +{ + FString Extent("extent"); + FString Coordinates[3] = {FString("x"), FString("y"), FString("z")}; + + for (auto Coordinate : Coordinates) + { + FActorVariation ExtentCoordinate; + + ExtentCoordinate.Id = JoinStrings(TEXT("_"), Extent, Coordinate); + ExtentCoordinate.Type = EActorAttributeType::Float; + ExtentCoordinate.RecommendedValues = { TEXT("0.0f") }; + ExtentCoordinate.bRestrictToRecommended = false; + + Def.Variations.Emplace(ExtentCoordinate); + } +} FActorDefinition UActorBlueprintFunctionLibrary::MakeGenericSensorDefinition( const FString &Type, @@ -457,6 +474,16 @@ void UActorBlueprintFunctionLibrary::MakePedestrianDefinitions( FillActorDefinitionArray(ParameterArray, Definitions, &MakePedestrianDefinition); } +void UActorBlueprintFunctionLibrary::MakeTriggerDefinition( + const FString &Id, + FActorDefinition &Definition) +{ + FillIdAndTags(Definition, TEXT("trigger"), Id); + AddVariationsForTrigger(Definition); + bool Success = CheckActorDefinition(Definition); + check(Success); +} + void UActorBlueprintFunctionLibrary::MakePropDefinition( const FPropParameters &Parameters, bool &Success, diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.h index 1bee07b45..7e9dd46d3 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/ActorBlueprintFunctionLibrary.h @@ -92,6 +92,11 @@ public: const TArray &ParameterArray, TArray &Definitions); + UFUNCTION(Category = "Carla Actor", BlueprintCallable) + static void MakeTriggerDefinition( + const FString &Id, + FActorDefinition &Definition); + UFUNCTION(Category = "Carla Actor", BlueprintCallable) static void MakePropDefinition( const FPropParameters &Parameters, diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/FrictionLayer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.cpp similarity index 55% rename from Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/FrictionLayer.cpp rename to Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.cpp index 777c21fd8..50e7cbc9e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/FrictionLayer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.cpp @@ -1,14 +1,17 @@ -// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB). This work is licensed under the terms of the MIT license. For a copy, see . +// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). This work is licensed under the terms of the MIT license. +// For a copy, see . #include "Carla.h" -#include "FrictionLayer.h" -#include "CarlaWheeledVehicle.h" +#include "FrictionTrigger.h" +#include "Vehicle/CarlaWheeledVehicle.h" - AFrictionLayer::AFrictionLayer(const FObjectInitializer &ObjectInitializer) +AFrictionTrigger::AFrictionTrigger(const FObjectInitializer &ObjectInitializer) : Super(ObjectInitializer) { - RootComponent = ObjectInitializer.CreateDefaultSubobject(this,TEXT("SceneRootComponent")); + RootComponent = ObjectInitializer.CreateDefaultSubobject(this, TEXT("SceneRootComponent")); RootComponent->SetMobility(EComponentMobility::Static); + TriggerVolume = CreateDefaultSubobject(TEXT("TriggerVolume")); TriggerVolume->SetupAttachment(RootComponent); TriggerVolume->SetHiddenInGame(true); @@ -17,21 +20,22 @@ TriggerVolume->SetGenerateOverlapEvents(true); } -void AFrictionLayer::Init() { +void AFrictionTrigger::Init() +{ // Register delegate on begin overlap. - if (!TriggerVolume->OnComponentBeginOverlap.IsAlreadyBound(this, &AFrictionLayer::OnTriggerBeginOverlap)) + if (!TriggerVolume->OnComponentBeginOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerBeginOverlap)) { - TriggerVolume->OnComponentBeginOverlap.AddDynamic(this, &AFrictionLayer::OnTriggerBeginOverlap); + TriggerVolume->OnComponentBeginOverlap.AddDynamic(this, &AFrictionTrigger::OnTriggerBeginOverlap); } // Register delegate on end overlap. - if (!TriggerVolume->OnComponentEndOverlap.IsAlreadyBound(this, &AFrictionLayer::OnTriggerEndOverlap)) + if (!TriggerVolume->OnComponentEndOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerEndOverlap)) { - TriggerVolume->OnComponentEndOverlap.AddDynamic(this, &AFrictionLayer::OnTriggerEndOverlap); + TriggerVolume->OnComponentEndOverlap.AddDynamic(this, &AFrictionTrigger::OnTriggerEndOverlap); } } -void AFrictionLayer::OnTriggerBeginOverlap( +void AFrictionTrigger::OnTriggerBeginOverlap( UPrimitiveComponent * /*OverlappedComp*/, AActor *OtherActor, UPrimitiveComponent * /*OtherComp*/, @@ -39,10 +43,12 @@ void AFrictionLayer::OnTriggerBeginOverlap( bool /*bFromSweep*/, const FHitResult & /*SweepResult*/) { - ACarlaWheeledVehicle* Vehicle = CastChecked(OtherActor); - if (Vehicle) { + ACarlaWheeledVehicle *Vehicle = CastChecked(OtherActor); + if (Vehicle) + { TArray WheelsFrictionScale = Vehicle->GetWheelsFrictionScale(); - for (auto& FrictionScale : WheelsFrictionScale) { + for (auto &FrictionScale : WheelsFrictionScale) + { FrictionScale = 0.0f; } @@ -50,16 +56,18 @@ void AFrictionLayer::OnTriggerBeginOverlap( } } -void AFrictionLayer::OnTriggerEndOverlap( +void AFrictionTrigger::OnTriggerEndOverlap( UPrimitiveComponent * /*OverlappedComp*/, AActor *OtherActor, UPrimitiveComponent * /*OtherComp*/, int32 /*OtherBodyIndex*/) { - ACarlaWheeledVehicle* Vehicle = CastChecked(OtherActor); - if (Vehicle) { + ACarlaWheeledVehicle *Vehicle = CastChecked(OtherActor); + if (Vehicle) + { TArray WheelsFrictionScale = Vehicle->GetWheelsFrictionScale(); - for (auto& FrictionScale : WheelsFrictionScale) { + for (auto &FrictionScale : WheelsFrictionScale) + { FrictionScale = 3.5f; } @@ -67,18 +75,27 @@ void AFrictionLayer::OnTriggerEndOverlap( } } - // Called when the game starts or when spawned -void AFrictionLayer::BeginPlay() +void AFrictionTrigger::BeginPlay() { Super::BeginPlay(); Init(); } +void AFrictionTrigger::EndPlay(const EEndPlayReason::Type EndPlayReason) +{ + // Deregister the delegate + if (TriggerVolume->OnComponentBeginOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerBeginOverlap)) + { + TriggerVolume->OnComponentBeginOverlap.RemoveDynamic(this, &AFrictionTrigger::OnTriggerBeginOverlap); + } + + Super::EndPlay(EndPlayReason); +} + // Called every frame -void AFrictionLayer::Tick(float DeltaTime) +void AFrictionTrigger::Tick(float DeltaTime) { Super::Tick(DeltaTime); } - diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/FrictionLayer.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.h similarity index 53% rename from Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/FrictionLayer.h rename to Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.h index 5ec8e7f70..8304cce67 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Vehicle/FrictionLayer.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/FrictionTrigger.h @@ -1,22 +1,26 @@ -// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB). This work is licensed under the terms of the MIT license. For a copy, see . +// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). This work is licensed under the terms of the MIT license. +// For a copy, see . #pragma once #include "GameFramework/Actor.h" +#include "Components/BoxComponent.h" -#include "FrictionLayer.generated.h" +#include "FrictionTrigger.generated.h" UCLASS() -class CARLA_API AFrictionLayer : public AActor +class CARLA_API AFrictionTrigger : public AActor { GENERATED_BODY() private: + void Init(); public: - AFrictionLayer(const FObjectInitializer &ObjectInitializer); + AFrictionTrigger(const FObjectInitializer &ObjectInitializer); UFUNCTION() void OnTriggerBeginOverlap( @@ -34,10 +38,17 @@ public: UPrimitiveComponent *OtherComp, int32 OtherBodyIndex); + void SetEpisode(const UCarlaEpisode &InEpisode) + { + Episode = &InEpisode; + } + protected: virtual void BeginPlay() override; + virtual void EndPlay(EEndPlayReason::Type EndPlayReason) override; + public: UPROPERTY(EditAnywhere) @@ -45,6 +56,5 @@ public: 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 new file mode 100644 index 000000000..7e45cb39a --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.cpp @@ -0,0 +1,71 @@ +// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#include "Carla.h" +#include "Carla/Trigger/TriggerFactory.h" + +#include "Carla/Game/CarlaGameInstance.h" +#include "Carla/Game/CarlaStatics.h" +#include "Carla/Trigger/FrictionTrigger.h" + +#include "Carla/Game/CarlaGameInstance.h" + +// ============================================================================= +// -- ASensorFactory ----------------------------------------------------------- +// ============================================================================= + +TArray ATriggerFactory::GetDefinitions() +{ + FActorDefinition TriggerDefinition = FActorDefinition(); + TriggerDefinition.Class = AFrictionTrigger::StaticClass(); + + TArray TriggerDefinitions; + + UActorBlueprintFunctionLibrary::MakeTriggerDefinition(TEXT("friction"), TriggerDefinition); + TriggerDefinitions.Add(TriggerDefinition); + + return TriggerDefinitions; +} + +FActorSpawnResult ATriggerFactory::SpawnActor( + const FTransform &Transform, + const FActorDescription &Description) +{ + auto *World = GetWorld(); + if (World == nullptr) + { + UE_LOG(LogCarla, Error, TEXT("ATriggerFactory: cannot spawn trigger into an empty world.")); + return {}; + } + + UCarlaGameInstance *GameInstance = UCarlaStatics::GetGameInstance(World); + if (GameInstance == nullptr) + { + UE_LOG(LogCarla, Error, TEXT("ATriggerFactory:: cannot spawn trigger, incompatible game instance.")); + return {}; + } + + UE_LOG(LogCarla, Log, TEXT("%s"), *Description.Class->GetName()); + auto *Trigger = World->SpawnActorDeferred( + Description.Class, + Transform, + this, + nullptr, + ESpawnActorCollisionHandlingMethod::AlwaysSpawn); + + if (Trigger == nullptr) + { + UE_LOG(LogCarla, Error, TEXT("ATriggerFactory:: spawn trigger failed.")); + } + else + { + auto *Episode = GameInstance->GetCarlaEpisode(); + check(Episode != nullptr); + Trigger->SetEpisode(*Episode); + } + UGameplayStatics::FinishSpawningActor(Trigger, Transform); + return FActorSpawnResult{Trigger}; +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.h new file mode 100644 index 000000000..3b8e6a3ed --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Trigger/TriggerFactory.h @@ -0,0 +1,30 @@ +// Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Carla/Actor/ActorSpawnResult.h" +#include "Carla/Actor/CarlaActorFactory.h" + +#include "TriggerFactory.generated.h" + +/// Factory in charge of spawning sensors. This factory is able to spawn every +/// sensor registered in carla::sensor::SensorRegistry. +UCLASS() +class CARLA_API ATriggerFactory : public ACarlaActorFactory +{ + GENERATED_BODY() + + /// Retrieve the definitions of all the sensors registered in the + /// SensorRegistry. Sensors must implement a GetSensorDefinition() static + /// method. + TArray GetDefinitions() final; + + FActorSpawnResult SpawnActor( + const FTransform &SpawnAtTransform, + const FActorDescription &ActorDescription) final; + +};