CARLA
 
载入中...
搜索中...
未找到
VehicleVelocityControl.cpp
浏览该文件的文档.
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// 构造函数,初始化组件的属性
12UVehicleVelocityControl::UVehicleVelocityControl()
13{
14 // 允许该组件在每帧更新时进行计算
15 PrimaryComponentTick.bCanEverTick = true;
16}
17// 初始化时调用,设置初始状态
18void UVehicleVelocityControl::BeginPlay()
19{
20 Super::BeginPlay();); // 调用基类的 BeginPlay 方法
21
22 // 初始时禁用组件的 Tick,手动控制启用
23 SetComponentTickEnabled(false);
24
25 // 设置 Tick 的执行时机为物理更新之前
26 SetTickGroup(ETickingGroup::TG_PrePhysics);
27
28 // 获取当前拥有该组件的车辆(Actor)
29 OwnerVehicle = GetOwner();
30 // 获取车辆的根组件,并强制转换为 UPrimitiveComponent 以便操作物理特性
31 PrimitiveComponent = Cast<UPrimitiveComponent>(OwnerVehicle->GetRootComponent());
32}
33
34// 激活组件并设置目标速度为零向量
35void UVehicleVelocityControl::Activate(bool bReset)
36{
37 Super::Activate(bReset);// 调用基类的 Activate 方法
38
39 // 设置目标速度为零
40 TargetVelocity = FVector();
41
42 // 启用该组件的 Tick
43 SetComponentTickEnabled(true);
44}
45
46// 激活组件并设置目标速度为指定的速度
47void UVehicleVelocityControl::Activate(FVector Velocity, bool bReset)
48{
49 Super::Activate(bReset);
50
51 TargetVelocity = Velocity;
52 SetComponentTickEnabled(true);
53}
54
55void UVehicleVelocityControl::Deactivate()
56{
57 SetComponentTickEnabled(false);
58 Super::Deactivate();
59}
60
61void UVehicleVelocityControl::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
62{
63 TRACE_CPUPROFILER_EVENT_SCOPE(UVehicleVelocityControl::TickComponent);
64 FTransform Transf = OwnerVehicle->GetActorTransform();
65 const FVector LocVel = Transf.TransformVector(TargetVelocity);
66 // 设置车辆根组件的线性速度(以世界坐标为单位)
67 PrimitiveComponent->SetPhysicsLinearVelocity(LocVel, false, "None");
68}