14 #include "CarSimMovementComponent.h" 25 CarSimManagerComponent->RegisterComponent();
27 UE_LOG(LogCarla, Warning, TEXT(
"Error: CarSim plugin is not enabled") );
38 UE_LOG(LogCarla, Warning, TEXT(
"Error: Owner is not properly set for UCarSimManagerComponent") );
43 FActorSpawnParameters SpawnParams;
44 SpawnParams.SpawnCollisionHandlingOverride =
45 ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
50 CarSimMovementComponent = NewObject<UCarSimMovementComponent>(OffsetActor);
53 CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(
false);
54 CarlaVehicle->GetVehicleMovementComponent()->Deactivate();
55 CarSimMovementComponent->DisableVehicle =
false;
56 CarSimMovementComponent->VsConfigFile =
SimfilePath;
57 CarSimMovementComponent->Activate();
58 CarSimMovementComponent->RegisterComponent();
60 CarSimMovementComponent->ResetVsVehicle(
false);
62 CarSimMovementComponent->SyncVsVehicleLocOri();
63 CarSimMovementComponent->SetComponentTickEnabled(
true);
65 CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::ComponentTransformIsKinematic;
66 auto * Bone =
CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
69 Bone->SetInstanceSimulatePhysics(
false);
75 CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Overlap);
78 CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Overlap);
81 CarlaVehicle->AttachToActor(OffsetActor, FAttachmentTransformRules::KeepWorldTransform);
83 UE_LOG(LogCarla, Warning, TEXT(
"Error: CarSim plugin is not enabled") );
95 CarSimMovementComponent->SetThrottleInput(Control.
Throttle);
96 CarSimMovementComponent->SetSteeringInput(Control.
Steer);
97 CarSimMovementComponent->SetBrakeInput(Control.
Brake);
100 CarSimMovementComponent->SetBrakeInput(Control.
Brake + 1.0);
102 Control.
Gear = CarSimMovementComponent->GetCurrentGear();
108 FVector NormalImpulse,
109 const FHitResult &Hit)
114 CarlaVehicle->GetMesh()->SetPhysicsLinearVelocity(FVector(0,0,0),
false,
"Vehicle_Base");
115 CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(
true);
116 CarlaVehicle->GetVehicleMovementComponent()->Activate();
117 CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::SimulationUpatesComponentTransform;
118 auto * Bone =
CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
121 Bone->SetInstanceSimulatePhysics(
true);
127 CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
128 CarlaVehicle->GetMesh()->SetCollisionProfileName(
"Vehicle");
137 int32 OtherBodyIndex,
139 const FHitResult & SweepResult)
141 if (OtherComp->GetCollisionResponseToChannel(
142 ECollisionChannel::ECC_WorldDynamic) ==
143 ECollisionResponse::ECR_Block)
148 CarlaVehicle->GetMesh()->SetPhysicsLinearVelocity(FVector(0,0,0),
false,
"Vehicle_Base");
149 CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(
true);
150 CarlaVehicle->GetVehicleMovementComponent()->Activate();
151 CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::SimulationUpatesComponentTransform;
152 auto * Bone =
CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
155 Bone->SetInstanceSimulatePhysics(
true);
161 CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
162 CarlaVehicle->GetMesh()->SetCollisionProfileName(
"Vehicle");
171 int32 OtherBodyIndex)
178 CarSimMovementComponent->UseVehicleSimRoad = bEnabled;
179 CarSimMovementComponent->ResetVsVehicle(
false);
180 CarSimMovementComponent->SyncVsVehicleLocOri();
190 OffsetActor->Destroy();
195 UE_LOG(LogCarla, Warning, TEXT(
"Error: Owner is not properly set for UCarSimManagerComponent") );
210 return GetOwner()->GetActorForwardVector() * CarSimMovementComponent->GetForwardSpeed();
219 return CarSimMovementComponent->GetCurrentGear();
228 return CarSimMovementComponent->GetForwardSpeed();
void RestoreVehiclePhysicsControl()
virtual void BeginPlay() override
ACarlaWheeledVehicle * CarlaVehicle
int32 GetVehicleCurrentGear() const override
void ProcessControl(FVehicleControl &Control) override
static void CreateCarsimComponent(ACarlaWheeledVehicle *Vehicle, FString Simfile)
float GetVehicleForwardSpeed() const override
void OnCarSimHit(AActor *Actor, AActor *OtherActor, FVector NormalImpulse, const FHitResult &Hit)
carla::SharedPtr< cc::Actor > Actor
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
FVector GetVelocity() const override
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
static void log_warning(Args &&... args)
void UseCarSimRoad(bool bEnabled)
void OnCarSimEndOverlap(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
Base class for CARLA wheeled vehicles.
static void CreateDefaultMovementComponent(ACarlaWheeledVehicle *Vehicle)
void OnCarSimOverlap(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
void SetCarlaMovementComponent(UBaseCarlaMovementComponent *MoementComponent)