10#include <util/ue-header-guard-begin.h>
11#include "Kismet/KismetMathLibrary.h"
12#include <util/ue-header-guard-end.h>
14UVehicleVelocityControl::UVehicleVelocityControl()
16 PrimaryComponentTick.bCanEverTick =
true;
19void UVehicleVelocityControl::BeginPlay()
23 SetComponentTickEnabled(
false);
24 SetTickGroup(ETickingGroup::TG_PrePhysics);
26 OwnerVehicle = GetOwner();
28 if (CarlaVehicle && CarlaVehicle->GetMesh())
30 PrimitiveComponent = Cast<UPrimitiveComponent>(CarlaVehicle->GetMesh());
32 if (PrimitiveComponent ==
nullptr)
34 PrimitiveComponent = Cast<UPrimitiveComponent>(OwnerVehicle->GetRootComponent());
38void UVehicleVelocityControl::Activate(
bool bReset)
40 Super::Activate(bReset);
42 TargetVelocity = FVector();
43 SetComponentTickEnabled(
true);
46void UVehicleVelocityControl::Activate(FVector Velocity,
bool bReset)
48 Super::Activate(bReset);
50 TargetVelocity = Velocity;
51 SetComponentTickEnabled(
true);
54void UVehicleVelocityControl::Deactivate()
56 SetComponentTickEnabled(
false);
60void UVehicleVelocityControl::TickComponent(
float DeltaTime,
enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
62 TRACE_CPUPROFILER_EVENT_SCOPE(UVehicleVelocityControl::TickComponent);
63 if (PrimitiveComponent ==
nullptr)
67 PrimitiveComponent->WakeRigidBody(NAME_None);
68 const FTransform Transf = OwnerVehicle->GetActorTransform();
71 FVector Local = FVector::ZeroVector;
72 const FVector Dir = TargetVelocity.GetSafeNormal();
75 const FVector Current = Transf.InverseTransformVectorNoScale(PrimitiveComponent->GetPhysicsLinearVelocity());
76 Local = Current - FVector::DotProduct(Current, Dir) * Dir + TargetVelocity;
78 PrimitiveComponent->SetPhysicsLinearVelocity(Transf.TransformVectorNoScale(Local),
false,
"None");
Base class for CARLA wheeled vehicles.