Added actor offset for carsim simulation.

This commit is contained in:
Axel 2020-12-15 18:15:31 +01:00 committed by bernat
parent 5170ca5880
commit 2fe34e0b5a
4 changed files with 74 additions and 2 deletions

View File

@ -0,0 +1,16 @@
// Copyright (c) 2020 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 "EmptyActor.h"
AEmptyActor::AEmptyActor(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryActorTick.bCanEverTick = false;
RootSceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRootComponent"));
RootComponent = RootSceneComponent;
RootComponent->SetMobility(EComponentMobility::Movable);
}

View File

@ -0,0 +1,23 @@
// Copyright (c) 2020 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/Pawn.h"
#include "EmptyActor.generated.h"
UCLASS()
class CARLA_API AEmptyActor : public APawn
{
GENERATED_BODY()
public:
AEmptyActor(const FObjectInitializer &ObjectInitializer);
private:
UPROPERTY(Category="Empty Actor", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
USceneComponent* RootSceneComponent;
};

View File

@ -15,6 +15,8 @@
#include "PhysXVehicleManager.h"
#include "TireConfig.h"
#include "VehicleWheel.h"
#include "Carla/Util/ActorAttacher.h"
#include "Carla/Util/EmptyActor.h"
#include "Rendering/SkeletalMeshRenderData.h"
@ -587,8 +589,18 @@ void ACarlaWheeledVehicle::RevertToCarSimPhysics()
void ACarlaWheeledVehicle::EnableCarSim(FString SimfilePath)
{
#ifdef WITH_CARSIM
CarSimMovementComponent = NewObject<UCarSimMovementComponent>(this);
ExternalMovementComponent = CarSimMovementComponent;
// workarround to compensate carsim coordinate origin offset
FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride =
ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
OffsetActor = GetWorld()->SpawnActor<AEmptyActor>(
GetActorLocation() + GetActorForwardVector() * CarSimOriginOffset,
GetActorRotation(),
SpawnParams);
CarSimMovementComponent = NewObject<UCarSimMovementComponent>(OffsetActor);
// CarSimMovementComponent = NewObject<UCarSimMovementComponent>(this);
// ExternalMovementComponent = CarSimMovementComponent;
carla::log_warning("Loading simfile:", carla::rpc::FromFString(SimfilePath));
GetVehicleMovementComponent()->SetComponentTickEnabled(false);
GetVehicleMovementComponent()->Deactivate();
@ -612,6 +624,13 @@ void ACarlaWheeledVehicle::EnableCarSim(FString SimfilePath)
OnActorHit.AddDynamic(this, &ACarlaWheeledVehicle::OnCarSimHit);
GetMesh()->OnComponentBeginOverlap.AddDynamic(this, &ACarlaWheeledVehicle::OnCarSimOverlap);
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Overlap);
// workaround to prevent carsim from interacting with its own car
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Overlap);
// attach to actor with an offset
AttachToActor(OffsetActor, FAttachmentTransformRules::KeepWorldTransform);
bCarSimEnabled = true;
#endif
}
@ -644,4 +663,12 @@ bool ACarlaWheeledVehicle::IsCarSimEnabled() const
{
return bCarSimEnabled;
}
void ACarlaWheeledVehicle::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if (OffsetActor)
{
OffsetActor->Destroy();
}
}
//-------------------------------------------

View File

@ -262,6 +262,8 @@ public:
UFUNCTION(Category="CARLA Wheeled Vehicle", BlueprintPure)
bool IsCarSimEnabled() const;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);
private:
// On car mesh hit, only works when carsim is enabled
@ -290,11 +292,15 @@ private:
UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere)
bool bCarSimEnabled = false;
UPROPERTY(Category="CARLA Wheeled Vehicle", EditAnywhere)
float CarSimOriginOffset = 150.f;
// Small workarround to allow optional CarSim plugin usage
UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UMovementComponent * ExternalMovementComponent;
#ifdef WITH_CARSIM
AActor* OffsetActor;
// Casted version of ExternalMovementComponent
UCarSimMovementComponent * CarSimMovementComponent;
#endif