11UVehicleAccelerationControl::UVehicleAccelerationControl()
13 PrimaryComponentTick.bCanEverTick =
true;
16void UVehicleAccelerationControl::BeginPlay()
20 SetComponentTickEnabled(
false);
22 SetTickGroup(ETickingGroup::TG_PostPhysics);
24 OwnerVehicle = GetOwner();
26 if (CarlaVehicle && CarlaVehicle->GetMesh())
28 PrimitiveComponent = Cast<UPrimitiveComponent>(CarlaVehicle->GetMesh());
30 if (PrimitiveComponent ==
nullptr)
32 PrimitiveComponent = Cast<UPrimitiveComponent>(OwnerVehicle->GetRootComponent());
34 ControlledForwardSpeed = 0.f;
37void UVehicleAccelerationControl::Activate(
bool bReset)
39 Super::Activate(bReset);
41 TargetAcceleration = FVector();
42 SetComponentTickEnabled(
true);
45void UVehicleAccelerationControl::Activate(FVector Acceleration,
bool bReset)
47 Super::Activate(bReset);
49 TargetAcceleration = Acceleration;
50 if (PrimitiveComponent !=
nullptr)
52 const FVector Vel = PrimitiveComponent->GetPhysicsLinearVelocity();
53 const FVector
Forward = OwnerVehicle->GetActorTransform().TransformVectorNoScale(FVector(1, 0, 0));
54 const FVector ForwardDir =
Forward.GetSafeNormal();
55 ControlledForwardSpeed = FVector::DotProduct(Vel, ForwardDir);
59 ControlledForwardSpeed = 0.f;
61 SetComponentTickEnabled(
true);
64void UVehicleAccelerationControl::Deactivate()
66 SetComponentTickEnabled(
false);
70void UVehicleAccelerationControl::TickComponent(
float DeltaTime,
enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
72 TRACE_CPUPROFILER_EVENT_SCOPE(UVehicleAccelerationControl::TickComponent);
73 if (PrimitiveComponent ==
nullptr)
77 PrimitiveComponent->WakeRigidBody(NAME_None);
79 const FTransform Transf = OwnerVehicle->GetActorTransform();
80 const FVector ForwardDir = Transf.TransformVectorNoScale(FVector(1, 0, 0)).GetSafeNormal();
81 const FVector WorldAcceleration = Transf.TransformVector(TargetAcceleration);
82 const float ForwardAccel = FVector::DotProduct(WorldAcceleration, ForwardDir);
85 ControlledForwardSpeed += ForwardAccel * DeltaTime;
88 ControlledForwardSpeed = FMath::Max(0.f, ControlledForwardSpeed);
90 const FVector CurrentVel = PrimitiveComponent->GetPhysicsLinearVelocity();
91 const FVector LateralVel = CurrentVel - FVector::DotProduct(CurrentVel, ForwardDir) * ForwardDir;
92 const FVector NewVel = (ControlledForwardSpeed * ForwardDir) + LateralVel;
94 PrimitiveComponent->SetPhysicsLinearVelocity(NewVel,
false,
"None");
Base class for CARLA wheeled vehicles.