CARLA
Loading...
Searching...
No Matches
VehicleAccelerationControl.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 TIER IV, Inc.
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6// TODO: Add gear-aware input (e.g. allow reverse when in reverse gear, keep forward-only in drive).
7
10
11UVehicleAccelerationControl::UVehicleAccelerationControl()
12{
13 PrimaryComponentTick.bCanEverTick = true;
14}
15
16void UVehicleAccelerationControl::BeginPlay()
17{
18 Super::BeginPlay();
19
20 SetComponentTickEnabled(false);
21 // Run after physics so our velocity override is applied after the vehicle simulation
22 SetTickGroup(ETickingGroup::TG_PostPhysics);
23
24 OwnerVehicle = GetOwner();
25 ACarlaWheeledVehicle* CarlaVehicle = Cast<ACarlaWheeledVehicle>(OwnerVehicle);
26 if (CarlaVehicle && CarlaVehicle->GetMesh())
27 {
28 PrimitiveComponent = Cast<UPrimitiveComponent>(CarlaVehicle->GetMesh());
29 }
30 if (PrimitiveComponent == nullptr)
31 {
32 PrimitiveComponent = Cast<UPrimitiveComponent>(OwnerVehicle->GetRootComponent());
33 }
34 ControlledForwardSpeed = 0.f;
35}
36
37void UVehicleAccelerationControl::Activate(bool bReset)
38{
39 Super::Activate(bReset);
40
41 TargetAcceleration = FVector();
42 SetComponentTickEnabled(true);
43}
44
45void UVehicleAccelerationControl::Activate(FVector Acceleration, bool bReset)
46{
47 Super::Activate(bReset);
48
49 TargetAcceleration = Acceleration;
50 if (PrimitiveComponent != nullptr)
51 {
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);
56 }
57 else
58 {
59 ControlledForwardSpeed = 0.f;
60 }
61 SetComponentTickEnabled(true);
62}
63
64void UVehicleAccelerationControl::Deactivate()
65{
66 SetComponentTickEnabled(false);
67 Super::Deactivate();
68}
69
70void UVehicleAccelerationControl::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
71{
72 TRACE_CPUPROFILER_EVENT_SCOPE(UVehicleAccelerationControl::TickComponent);
73 if (PrimitiveComponent == nullptr)
74 {
75 return;
76 }
77 PrimitiveComponent->WakeRigidBody(NAME_None);
78
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);
83
84 // Integrate only forward speed; preserve lateral velocity from physics so tires can generate cornering force
85 ControlledForwardSpeed += ForwardAccel * DeltaTime;
86 // Clamp to non-negative so we never go into reverse
87 // TODO: Add gear-aware input (e.g. allow reverse when in reverse gear, keep forward-only in drive).
88 ControlledForwardSpeed = FMath::Max(0.f, ControlledForwardSpeed);
89
90 const FVector CurrentVel = PrimitiveComponent->GetPhysicsLinearVelocity();
91 const FVector LateralVel = CurrentVel - FVector::DotProduct(CurrentVel, ForwardDir) * ForwardDir;
92 const FVector NewVel = (ControlledForwardSpeed * ForwardDir) + LateralVel;
93
94 PrimitiveComponent->SetPhysicsLinearVelocity(NewVel, false, "None");
95}
Base class for CARLA wheeled vehicles.