CARLA
VehicleVelocityControl.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #include "Kismet/KismetMathLibrary.h"
8 
10 
11 
12 UVehicleVelocityControl::UVehicleVelocityControl()
13 {
14  PrimaryComponentTick.bCanEverTick = true;
15 }
16 
17 void UVehicleVelocityControl::BeginPlay()
18 {
19  Super::BeginPlay();
20 
21  SetComponentTickEnabled(false);
22  SetTickGroup(ETickingGroup::TG_PrePhysics);
23 
24  OwnerVehicle = GetOwner();
25  PrimitiveComponent = Cast<UPrimitiveComponent>(OwnerVehicle->GetRootComponent());
26 }
27 
28 void UVehicleVelocityControl::Activate(bool bReset)
29 {
30  Super::Activate(bReset);
31 
32  TargetVelocity = FVector();
33  SetComponentTickEnabled(true);
34 }
35 
36 void UVehicleVelocityControl::Activate(FVector Velocity, bool bReset)
37 {
38  Super::Activate(bReset);
39 
40  TargetVelocity = Velocity;
41  SetComponentTickEnabled(true);
42 }
43 
44 void UVehicleVelocityControl::Deactivate()
45 {
46  SetComponentTickEnabled(false);
47  Super::Deactivate();
48 }
49 
50 void UVehicleVelocityControl::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
51 {
52  TRACE_CPUPROFILER_EVENT_SCOPE(UVehicleVelocityControl::TickComponent);
53  FTransform Transf = OwnerVehicle->GetActorTransform();
54  const FVector LocVel = Transf.TransformVector(TargetVelocity);
55  PrimitiveComponent->SetPhysicsLinearVelocity(LocVel, false, "None");
56 }