Friction trigger box is getting spawned

This commit is contained in:
Manish 2019-04-17 17:53:29 +02:00
parent 2d352612e9
commit 70442e2436
6 changed files with 188 additions and 28 deletions

View File

@ -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,

View File

@ -92,6 +92,11 @@ public:
const TArray<FPedestrianParameters> &ParameterArray,
TArray<FActorDefinition> &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,

View File

@ -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 <https://opensource.org/licenses/MIT>.
// 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 <https://opensource.org/licenses/MIT>.
#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<USceneComponent>(this,TEXT("SceneRootComponent"));
RootComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneRootComponent"));
RootComponent->SetMobility(EComponentMobility::Static);
TriggerVolume = CreateDefaultSubobject<UBoxComponent>(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<ACarlaWheeledVehicle>(OtherActor);
if (Vehicle) {
ACarlaWheeledVehicle *Vehicle = CastChecked<ACarlaWheeledVehicle>(OtherActor);
if (Vehicle)
{
TArray<float> 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<ACarlaWheeledVehicle>(OtherActor);
if (Vehicle) {
ACarlaWheeledVehicle *Vehicle = CastChecked<ACarlaWheeledVehicle>(OtherActor);
if (Vehicle)
{
TArray<float> 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);
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
// 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 <https://opensource.org/licenses/MIT>.
#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;
};

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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<FActorDefinition> ATriggerFactory::GetDefinitions()
{
FActorDefinition TriggerDefinition = FActorDefinition();
TriggerDefinition.Class = AFrictionTrigger::StaticClass();
TArray<FActorDefinition> 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<AFrictionTrigger>(
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};
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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<FActorDefinition> GetDefinitions() final;
FActorSpawnResult SpawnActor(
const FTransform &SpawnAtTransform,
const FActorDescription &ActorDescription) final;
};