CARLA
 
载入中...
搜索中...
未找到
CarlaActor.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 "CarlaActor.h" // 包含CARLA中Actor类的头文件
8#include "Carla/OpenDrive/OpenDrive.h" // 包含OpenDrive地图解析器的头文件
9#include "Carla/Util/NavigationMesh.h" // 包含导航网格的头文件
10#include "Carla/Vehicle/CarlaWheeledVehicle.h" // 包含CARLA中轮式车辆的头文件
11#include "Carla/Walker/WalkerController.h" // 包含行人控制器的头文件
12#include "Carla/Walker/WalkerBase.h" // 包含行人基础类的头文件
13#include "GameFramework/CharacterMovementComponent.h" // 包含角色移动组件的头文件
14#include "Carla/Game/Tagger.h" // 包含标签器的头文件
15#include "Carla/Vehicle/MovementComponents/CarSimManagerComponent.h" // 包含CarSim管理组件的头文件
16#include "Carla/Vehicle/MovementComponents/ChronoMovementComponent.h" // 包含Chrono物理组件的头文件
17#include "Carla/Traffic/TrafficLightBase.h" // 包含交通灯基础类的头文件
18#include "Carla/Game/CarlaStatics.h" // 包含CARLA静态数据的头文件
19#include "Components/CapsuleComponent.h" // 包含胶囊组件的头文件
20
21#include <compiler/disable-ue4-macros.h> // 禁用UE4宏,防止与carla库的宏冲突
22#include <carla/rpc/AckermannControllerSettings.h> // 包含Ackermann控制器设置的RPC结构
23#include "carla/rpc/LabelledPoint.h" // 包含标记点的RPC结构
24#include <carla/rpc/LightState.h> // 包含灯光状态的RPC结构
25#include <carla/rpc/MapInfo.h> // 包含地图信息的RPC结构
26#include <carla/rpc/MapLayer.h> // 包含地图层的RPC结构
27#include <carla/rpc/VehicleAckermannControl.h> // 包含Ackermann控制的RPC结构
28#include <carla/rpc/VehicleControl.h> // 包含车辆控制的RPC结构
29#include <carla/rpc/VehiclePhysicsControl.h> // 包含车辆物理控制的RPC结构
30#include <carla/rpc/VehicleLightState.h> // 包含车辆灯光状态的RPC结构
31#include <carla/rpc/VehicleLightStateList.h> // 包含车辆灯光状态列表的RPC结构
32#include <carla/rpc/WalkerBoneControlIn.h> // 包含行人骨骼控制输入的RPC结构
33#include <carla/rpc/WalkerBoneControlOut.h> // 包含行人骨骼控制输出的RPC结构
34#include <carla/rpc/WalkerControl.h> // 包含行人控制的RPC结构
35#include <carla/rpc/VehicleWheels.h> // 包含车辆轮子信息的RPC结构
36#include <carla/rpc/WeatherParameters.h> // 包含天气参数的RPC结构
37#include <compiler/enable-ue4-macros.h> // 启用UE4宏
38
39// FCarlaActor类的构造函数
41 IdType ActorId,
43 TSharedPtr<const FActorInfo> Info,
45 UWorld* World)
46 : TheActor(Actor),
47 Info(std::move(Info)),
48 Id(ActorId),
51{
52 // 构造函数体内为空,初始化列表已经完成了所有成员变量的初始化
53}
54
55// FVehicleActor类的构造函数
57 IdType ActorId,
59 TSharedPtr<const FActorInfo> Info,
61 UWorld* World)
62 : FCarlaActor(ActorId, Actor, Info, InState, World) // 调用基类构造函数
63{
64 Type = ActorType::Vehicle; // 设置Actor类型为车辆
65 ActorData = MakeShared<FVehicleData>(); // 创建车辆数据的共享指针
66}
67
68// FSensorActor类的构造函数
70 IdType ActorId,
72 TSharedPtr<const FActorInfo> Info,
74 UWorld* World)
75 : FCarlaActor(ActorId, Actor, Info, InState, World) // 调用基类构造函数
76{
77 // FSensorActor构造函数实现
78}
79
80 TSharedPtr<const FActorInfo> Info,
82 UWorld* World)
83 : FCarlaActor(ActorId, Actor, Info, InState, World)// 调用基类构造函数
84{
85 Type = ActorType::Sensor; // 设置Actor类型为传感器
86 ActorData = MakeShared<FActorSensorData>(); // 创建传感器数据的共享指针
87}
88// FTrafficSignActor类的构造函数
90 IdType ActorId,
92 TSharedPtr<const FActorInfo> Info,
94 UWorld* World)
95 : FCarlaActor(ActorId, Actor, Info, InState, World)// 调用基类构造函数
96{
97 Type = ActorType::TrafficSign; // 设置Actor类型为交通标志
98 ActorData = MakeShared<FTrafficSignData>(); // 创建交通标志数据的共享指针
99}
100// FTrafficLightActor类的构造函数
102 IdType ActorId,
103 AActor* Actor,
104 TSharedPtr<const FActorInfo> Info,
106 UWorld* World)
107 : FCarlaActor(ActorId, Actor, Info, InState, World) // 调用基类构造函数
108{
109 Type = ActorType::TrafficLight; // 设置Actor类型为交通灯
110 ActorData = MakeShared<FTrafficLightData>(); // 创建交通灯数据的共享指针
111}
112// FWalkerActor类的构造函数
114 IdType ActorId,
115 AActor* Actor,
116 TSharedPtr<const FActorInfo> Info,
118 UWorld* World)
119 : FCarlaActor(ActorId, Actor, Info, InState, World)
120{
121 Type = ActorType::Walker;// 设置Actor类型为行人
122 ActorData = MakeShared<FWalkerData>(); // 创建行人数据的共享指针
123}
124// FOtherActor类的构造函数
126 IdType ActorId,
127 AActor* Actor,
128 TSharedPtr<const FActorInfo> Info,
130 UWorld* World)
131 : FCarlaActor(ActorId, Actor, Info, InState, World)
132{
133 Type = ActorType::Other; // 设置Actor类型为其他
134 ActorData = MakeShared<FActorData>(); // 创建其他Actor数据的共享指针
135}
136
137// FCarlaActor类的静态函数,用于根据不同的Actor类型构造相应的FCarlaActor派生类实例
138TSharedPtr<FCarlaActor> FCarlaActor::ConstructCarlaActor(
139 IdType ActorId,
140 AActor* Actor,
141 TSharedPtr<const FActorInfo> Info,
142 ActorType Type,
144 UWorld* World)
145{
146 // 根据传入的Actor类型进行判断,并创建对应的派生类实例
147 switch(Type)
148 {
149 case ActorType::TrafficSign:// 创建交通标志Actor的实例
150 return MakeShared<FTrafficSignActor>(ActorId, Actor, std::move(Info), InState, World);
151 break;
152 case ActorType::TrafficLight: // 创建交通灯Actor的实例
153 return MakeShared<FTrafficLightActor>(ActorId, Actor, std::move(Info), InState, World);
154 break;
155 case ActorType::Vehicle: // 创建车辆Actor的实例
156 return MakeShared<FVehicleActor>(ActorId, Actor, std::move(Info), InState, World);
157 break;
158 case ActorType::Walker: // 创建行人Actor的实例
159 return MakeShared<FWalkerActor>(ActorId, Actor, std::move(Info), InState, World);
160 break;
161 case ActorType::Sensor:// 创建传感器Actor的实例
162 return MakeShared<FSensorActor>(ActorId, Actor, std::move(Info), InState, World);
163 break;
164 default: // 如果类型不匹配以上任何一种,则创建其他类型的Actor实例
165 return MakeShared<FOtherActor>(ActorId, Actor, std::move(Info), InState, World);
166 break;
167 }
168}
169
170// Base FCarlaActor functions ---------------------
171
172void FCarlaActor::PutActorToSleep(UCarlaEpisode* CarlaEpisode)
173{
175 if (ActorData)
176 {
177 ActorData->RecordActorData(this, CarlaEpisode);
178 }
179 TheActor->Destroy();
180 TheActor = nullptr;
181}
182
183void FCarlaActor::WakeActorUp(UCarlaEpisode* CarlaEpisode)
184{
185 TheActor = ActorData->RespawnActor(CarlaEpisode, *Info);
186 if (TheActor == nullptr)
187 {
188 UE_LOG(LogCarla, Error, TEXT("Could not wake up dormant actor %d at location %s"), GetActorId(), *(ActorData->GetLocalTransform(CarlaEpisode).GetLocation().ToString()));
189 return;
190 }
192 ActorData->RestoreActorData(this, CarlaEpisode);
193}
194
196{
197 if (IsDormant())
198 {
199 FTransform Transform = FTransform(
200 ActorData->Rotation,
201 ActorData->Location.ToFVector(),
202 ActorData->Scale);
203 ALargeMapManager* LargeMap =
205 if (LargeMap)
206 {
207 Transform = LargeMap->GlobalToLocalTransform(Transform);
208 }
209 return Transform;
210 }
211 else
212 {
213 return GetActor()->GetActorTransform();
214 }
215}
216
218{
219 if (IsDormant())
220 {
221 return FTransform(
222 ActorData->Rotation,
223 ActorData->Location.ToFVector(),
224 ActorData->Scale);
225 }
226 else
227 {
228 FTransform Transform = GetActor()->GetActorTransform();
229 ALargeMapManager* LargeMap =
231 if (LargeMap)
232 {
233 Transform = LargeMap->LocalToGlobalTransform(Transform);
234 }
235 return Transform;
236 }
237}
238
240{
241 if (IsDormant())
242 {
243 FVector Location = ActorData->Location.ToFVector();
244 ALargeMapManager* LargeMap =
246 if (LargeMap)
247 {
248 Location = LargeMap->GlobalToLocalLocation(Location);
249 }
250 return Location;
251 }
252 else
253 {
254 return GetActor()->GetActorLocation();
255 }
256}
257
259{
260 if (IsDormant())
261 {
262 return ActorData->Location.ToFVector();
263 }
264 else
265 {
266 FVector Location = GetActor()->GetActorLocation();
267 ALargeMapManager* LargeMap =
269 if (LargeMap)
270 {
271 Location = LargeMap->LocalToGlobalLocation(Location);
272 }
273 return Location;
274 }
275}
276
277void FCarlaActor::SetActorLocalLocation(const FVector& Location, ETeleportType TeleportType)
278{
279 if (IsDormant())
280 {
281 FVector GlobalLocation = Location;
282 ALargeMapManager* LargeMap =
284 if (LargeMap)
285 {
286 GlobalLocation = LargeMap->LocalToGlobalLocation(GlobalLocation);
287 }
288 ActorData->Location = FDVector(GlobalLocation);
289 }
290 else
291 {
292 GetActor()->SetActorRelativeLocation(
293 Location,
294 false,
295 nullptr,
296 TeleportType);
297 }
298}
299
301 const FVector& Location, ETeleportType TeleportType)
302{
303 if (IsDormant())
304 {
305 ActorData->Location = FDVector(Location);;
306 }
307 else
308 {
309 FVector LocalLocation = Location;
310 ALargeMapManager* LargeMap =
312 if (LargeMap)
313 {
314 LocalLocation = LargeMap->GlobalToLocalLocation(Location);
315 }
316 GetActor()->SetActorRelativeLocation(
317 LocalLocation,
318 false,
319 nullptr,
320 TeleportType);
321 }
322}
323
325 const FTransform& Transform, ETeleportType TeleportType)
326{
327 if (IsDormant())
328 {
329 FTransform GlobalTransform = Transform;
330 ALargeMapManager* LargeMap =
332 if (LargeMap)
333 {
334 GlobalTransform =
335 LargeMap->LocalToGlobalTransform(GlobalTransform);
336 }
337 ActorData->Location = FDVector(GlobalTransform.GetLocation());
338 ActorData->Rotation = GlobalTransform.GetRotation();
339 ActorData->Scale = GlobalTransform.GetScale3D();
340 }
341 else
342 {
343 GetActor()->SetActorRelativeTransform(
344 Transform,
345 false,
346 nullptr,
347 TeleportType);
348 }
349}
350
352 const FTransform& Transform, ETeleportType TeleportType)
353{
354 if (IsDormant())
355 {
356 ActorData->Location = FDVector(Transform.GetLocation());
357 ActorData->Rotation = Transform.GetRotation();
358 ActorData->Scale = Transform.GetScale3D();
359 }
360 else
361 {
362 FTransform LocalTransform = Transform;
363 ALargeMapManager* LargeMap =
365 if (LargeMap)
366 {
367 LocalTransform =
368 LargeMap->GlobalToLocalTransform(LocalTransform);
369 }
370 GetActor()->SetActorRelativeTransform(
371 LocalTransform,
372 false,
373 nullptr,
374 TeleportType);
375 }
376}
377
379{
380 if (IsDormant())
381 {
382 return ActorData->Velocity;
383 }
384 else
385 {
386 return GetActor()->GetVelocity();
387 }
388}
389
391{
392 if (IsDormant())
393 {
394 return ActorData->AngularVelocity;
395 }
396 else
397 {
398 UPrimitiveComponent* Primitive =
399 Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
400 if (Primitive)
401 {
402 return Primitive->GetPhysicsAngularVelocityInDegrees();
403 }
404 }
405 return FVector();
406}
407
409{
410 if (IsDormant())
411 {
412 ActorData->Velocity = Velocity;
413 }
414 else
415 {
416 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
417 if (RootComponent == nullptr)
418 {
420 }
421 RootComponent->SetPhysicsLinearVelocity(
422 Velocity,
423 false,
424 "None");
425 }
427}
428
430{
431 if (IsDormant())
432 {
433 ActorData->AngularVelocity = AngularVelocity;
434 }
435 else
436 {
437 UPrimitiveComponent* RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
438 if (RootComponent == nullptr)
439 {
441 }
442 RootComponent->SetPhysicsAngularVelocityInDegrees(
443 AngularVelocity,
444 false,
445 "None");
446 }
448}
449
451{
452 if (IsDormant())
453 {
454 }
455 else
456 {
457 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
458 if (RootComponent == nullptr)
459 {
461 }
462 RootComponent->AddImpulse(
463 Impulse,
464 "None",
465 false);
466 }
468}
469
471 const FVector& Impulse, const FVector& Location)
472{
473 if (IsDormant())
474 {
475 }
476 else
477 {
478 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
479 if (RootComponent == nullptr)
480 {
482 }
483
484 UE_LOG(LogCarla, Warning, TEXT("AddImpulseAtLocation: Experimental feature, use carefully."));
485
486 RootComponent->AddImpulseAtLocation(
487 Impulse,
488 Location,
489 "None");
490 }
492}
493
495{
496 if (IsDormant())
497 {
498 }
499 else
500 {
501 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
502 if (RootComponent == nullptr)
503 {
505 }
506 RootComponent->AddForce(
507 Force,
508 "None",
509 false);
510 }
512}
513
515 const FVector& Force, const FVector& Location)
516{
517 if (IsDormant())
518 {
519 }
520 else
521 {
522 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
523 if (RootComponent == nullptr)
524 {
526 }
527
528 UE_LOG(LogCarla, Warning, TEXT("AddForceAtLocation: Experimental feature, use carefully."));
529
530 RootComponent->AddForceAtLocation(
531 Force,
532 Location,
533 "None");
534 }
536}
537
539{
540 if (IsDormant())
541 {
542 }
543 else
544 {
545 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
546 if (RootComponent == nullptr)
547 {
549 }
550 RootComponent->AddAngularImpulseInDegrees(
551 AngularInpulse,
552 "None",
553 false);
554 }
556}
557
559{
560 if (IsDormant())
561 {
562 }
563 else
564 {
565 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
566 if (RootComponent == nullptr)
567 {
569 }
570 RootComponent->AddTorqueInDegrees(
571 Torque,
572 "None",
573 false);
574 }
576}
577
579{
580 if (IsDormant())
581 {
582 ActorData->bSimulatePhysics = bEnabled;
583 }
584 else
585 {
586 // In the rest of actors, the physics is controlled with the UPrimitiveComponent, so we use
587 // that for disable it.
588 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
589 if (RootComponent == nullptr)
590 {
592 }
593
594 RootComponent->SetSimulatePhysics(bEnabled);
595 RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
596 }
598}
599
601{
602 if (IsDormant())
603 {
604 }
605 else
606 {
607 GetActor()->SetActorEnableCollision(bEnabled);
608 }
610}
611
613{
614 if (IsDormant())
615 {
616 }
617 else
618 {
619 auto RootComponent = Cast<UPrimitiveComponent>(GetActor()->GetRootComponent());
620 if (RootComponent == nullptr)
621 {
623 }
624 RootComponent->SetEnableGravity(bEnabled);
625 }
627}
628
629// FVehicleActor functions ---------------------
630
632{
633 if (IsDormant())
634 {
635 }
636 else
637 {
638 auto CarlaVehicle = Cast<ACarlaWheeledVehicle>(GetActor());
639 if (CarlaVehicle == nullptr)
640 {
642 }
643 CarlaVehicle->ActivateVelocityControl(Velocity);
644 }
646}
647
649{
650 if (IsDormant())
651 {
652 }
653 else
654 {
655 auto CarlaVehicle = Cast<ACarlaWheeledVehicle>(GetActor());
656 if (CarlaVehicle == nullptr)
657 {
659 }
660 CarlaVehicle->DeactivateVelocityControl();
661 }
663}
664
666{
667 if (IsDormant())
668 {
669 FVehicleData* ActorData = GetActorData<FVehicleData>();
670 PhysicsControl = ActorData->PhysicsControl;
671 }
672 else
673 {
674 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
675 if (Vehicle == nullptr)
676 {
678 }
679 PhysicsControl = Vehicle->GetVehiclePhysicsControl();
680 }
682}
683
685{
686 if (IsDormant())
687 {
688 FVehicleData* ActorData = GetActorData<FVehicleData>();
689 FailureState = ActorData->FailureState;
690 }
691 else
692 {
693 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
694 if (Vehicle == nullptr)
695 {
697 }
698 FailureState = Vehicle->GetFailureState();
699 }
701}
702
704{
705 if (IsDormant())
706 {
707 FVehicleData* ActorData = GetActorData<FVehicleData>();
708 LightState = ActorData->LightState;
709 }
710 else
711 {
712 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
713 if (Vehicle == nullptr)
714 {
716 }
717
718 LightState = Vehicle->GetVehicleLightState();
719 }
721}
722
724{
725 if (!IsDormant())
726 {
727 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
728 if (Vehicle == nullptr)
729 {
731 }
732 Vehicle->OpenDoor(DoorIdx);
733 }
735}
736
738{
739 if (!IsDormant())
740 {
741 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
742 if (Vehicle == nullptr)
743 {
745 }
746 Vehicle->CloseDoor(DoorIdx);
747 }
749}
750
753{
754 if (IsDormant())
755 {
756 FVehicleData* ActorData = GetActorData<FVehicleData>();
757 ActorData->PhysicsControl = PhysicsControl;
758 }
759 else
760 {
761 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
762 if (Vehicle == nullptr)
763 {
765 }
766
767 Vehicle->ApplyVehiclePhysicsControl(PhysicsControl);
768 }
770}
771
774{
775 if (IsDormant())
776 {
777 FVehicleData* ActorData = GetActorData<FVehicleData>();
778 ActorData->LightState = LightState;
779 }
780 else
781 {
782 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
783 if (Vehicle == nullptr)
784 {
786 }
787
788 Vehicle->SetVehicleLightState(LightState);
789 }
791}
792
794 const EVehicleWheelLocation& WheelLocation, float AngleInDeg)
795{
796 if (IsDormant())
797 {
798 }
799 else
800 {
801 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
802 if(Vehicle == nullptr){
804 }
805 Vehicle->SetWheelSteerDirection(WheelLocation, AngleInDeg);
806 }
808}
809
811 const EVehicleWheelLocation& WheelLocation, float& Angle)
812{
813 if (IsDormant())
814 {
815 Angle = 0;
816 }
817 else
818 {
819 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
820 if(Vehicle == nullptr){
822 }
823
824 Angle = Vehicle->GetWheelSteerAngle(WheelLocation);
825 }
827}
828
830{
831 if (IsDormant())
832 {
833 ActorData->bSimulatePhysics = bEnabled;
834 }
835 else
836 {
837 auto* CarlaVehicle = Cast<ACarlaWheeledVehicle>(GetActor());
838 // The physics in the vehicles works in a different way so to disable them.
839 if (CarlaVehicle == nullptr){
841 }
842 CarlaVehicle->SetSimulatePhysics(bEnabled);
843 }
845}
846
848 const FVehicleControl& Control, const EVehicleInputPriority& Priority)
849{
850 if (IsDormant())
851 {
852 FVehicleData* ActorData = GetActorData<FVehicleData>();
853 ActorData->Control = Control;
854 ActorData->bAckermannControlActive = false;
855 }
856 else
857 {
858 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
859 if (Vehicle == nullptr)
860 {
862 }
863 Vehicle->ApplyVehicleControl(Control, Priority);
864 }
866}
867
870{
871 if (IsDormant())
872 {
873 FVehicleData* ActorData = GetActorData<FVehicleData>();
874 ActorData->AckermannControl = AckermannControl;
875 ActorData->bAckermannControlActive = true;
876 }
877 else
878 {
879 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
880 if (Vehicle == nullptr)
881 {
883 }
884 Vehicle->ApplyVehicleAckermannControl(AckermannControl, Priority);
885 }
887}
888
890{
891 if (IsDormant())
892 {
893 FVehicleData* ActorData = GetActorData<FVehicleData>();
894 VehicleControl = ActorData->Control;
895 }
896 else
897 {
898 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
899 if (Vehicle == nullptr)
900 {
902 }
903 VehicleControl = Vehicle->GetVehicleControl();
904 }
906}
907
909{
910 if (IsDormant())
911 {
912 FVehicleData* ActorData = GetActorData<FVehicleData>();
913 VehicleAckermannControl = ActorData->AckermannControl;
914 }
915 else
916 {
917 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
918 if (Vehicle == nullptr)
919 {
921 }
922 VehicleAckermannControl = Vehicle->GetVehicleAckermannControl();
923 }
925}
926
928 FAckermannControllerSettings& AckermannSettings)
929{
930 if (IsDormant())
931 {
932 FVehicleData* ActorData = GetActorData<FVehicleData>();
933 AckermannSettings = ActorData->AckermannControllerSettings;
934 }
935 else
936 {
937 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
938 if (Vehicle == nullptr)
939 {
941 }
942 AckermannSettings = Vehicle->GetAckermannControllerSettings();
943 }
945}
946
948 const FAckermannControllerSettings& AckermannSettings)
949{
950 if (IsDormant())
951 {
952 FVehicleData* ActorData = GetActorData<FVehicleData>();
953 ActorData->AckermannControllerSettings = AckermannSettings;
954 }
955 else
956 {
957 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
958 if (Vehicle == nullptr)
959 {
961 }
962
963 Vehicle->ApplyAckermannControllerSettings(AckermannSettings);
964 }
966}
967
969{
970 if (IsDormant())
971 {
972 }
973 else
974 {
975 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
976 if (Vehicle == nullptr)
977 {
979 }
980 auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
981 if (Controller == nullptr)
982 {
984 }
985 Controller->SetAutopilot(bEnabled, bKeepState);
986 }
988}
989
991{
992 if (IsDormant())
993 {
994 FVehicleTelemetryData EmptyTelemetryData;
995 TelemetryData = EmptyTelemetryData;
996 }
997 else
998 {
999 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
1000 if (Vehicle == nullptr)
1001 {
1003 }
1004 TelemetryData = Vehicle->GetVehicleTelemetryData();
1005 }
1007}
1008
1010{
1011 if (IsDormant())
1012 {
1013 }
1014 else
1015 {
1016 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
1017 if (Vehicle == nullptr)
1018 {
1020 }
1021 Vehicle->ShowDebugTelemetry(bEnabled);
1022 }
1024}
1025
1027{
1028 if (IsDormant())
1029 {
1030 }
1031 else
1032 {
1033 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
1034 if (Vehicle == nullptr)
1035 {
1037 }
1039 }
1041}
1042
1044{
1045 if (IsDormant())
1046 {
1047 }
1048 else
1049 {
1050 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
1051 if (Vehicle == nullptr)
1052 {
1054 }
1055 auto* CarSimComponent = Vehicle->GetCarlaMovementComponent<UCarSimManagerComponent>();
1056 if(CarSimComponent)
1057 {
1058 CarSimComponent->UseCarSimRoad(bEnabled);
1059 }
1060 else
1061 {
1063 }
1064 }
1066}
1067
1069 uint64_t MaxSubsteps, float MaxSubstepDeltaTime,
1070 const FString& VehicleJSON, const FString& PowertrainJSON,
1071 const FString& TireJSON, const FString& BaseJSONPath)
1072{
1073 if (IsDormant())
1074 {
1075 }
1076 else
1077 {
1078 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
1079 if (Vehicle == nullptr)
1080 {
1082 }
1084 Vehicle,
1085 MaxSubsteps,
1086 MaxSubstepDeltaTime,
1087 VehicleJSON,
1088 PowertrainJSON,
1089 TireJSON,
1090 BaseJSONPath);
1091 }
1093}
1094
1096{
1097 if (IsDormant())
1098 {
1099 }
1100 else
1101 {
1102 auto Vehicle = Cast<ACarlaWheeledVehicle>(GetActor());
1103 if (Vehicle == nullptr)
1104 {
1106 }
1107 UBaseCarlaMovementComponent* MovementComponent =
1108 Vehicle->GetCarlaMovementComponent<UBaseCarlaMovementComponent>();
1109 if(MovementComponent)
1110 {
1111 MovementComponent->DisableSpecialPhysics();
1112 }
1113 }
1115}
1116
1117// FSensorActor functions ---------------------
1118
1119// FtrafficSignActor functions ---------------------
1120
1121// FTrafficLightActor functions ---------------------
1122
1124{
1125 if (IsDormant())
1126 {
1127 FTrafficLightData* ActorData = GetActorData<FTrafficLightData>();
1128 ActorData->LightState = State;
1129 }
1130 else
1131 {
1132 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1133 if (TrafficLight == nullptr)
1134 {
1136 }
1137 TrafficLight->SetTrafficLightState(State);
1138 }
1140}
1141
1143{
1144 if (IsDormant())
1145 {
1146 const FTrafficLightData* ActorData = GetActorData<FTrafficLightData>();
1147 return ActorData->LightState;
1148 }
1149 else
1150 {
1151 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1152 if (TrafficLight == nullptr)
1153 {
1154 return ETrafficLightState::Off;
1155 }
1156 return TrafficLight->GetTrafficLightState();
1157 }
1158}
1159
1161{
1162 if (IsDormant())
1163 {
1164 FTrafficLightData* ActorData = GetActorData<FTrafficLightData>();
1165 return ActorData->Controller;
1166 }
1167 else
1168 {
1169 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1170 if (TrafficLight == nullptr)
1171 {
1172 return nullptr;
1173 }
1174 return TrafficLight->GetTrafficLightComponent()->GetController();
1175 }
1176}
1177
1179{
1180 if (IsDormant())
1181 {
1182 // Todo: implement
1183 }
1184 else
1185 {
1186 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1187 if (TrafficLight == nullptr)
1188 {
1190 }
1191 TrafficLight->SetGreenTime(time);
1192 }
1194}
1195
1197{
1198 if (IsDormant())
1199 {
1200 // Todo: implement
1201 }
1202 else
1203 {
1204 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1205 if (TrafficLight == nullptr)
1206 {
1208 }
1209 TrafficLight->SetYellowTime(time);
1210 }
1212}
1213
1215{
1216 if (IsDormant())
1217 {
1218 // Todo: implement
1219 }
1220 else
1221 {
1222 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1223 if (TrafficLight == nullptr)
1224 {
1226 }
1227 TrafficLight->SetRedTime(time);
1228 }
1230}
1231
1233{
1234 if (IsDormant())
1235 {
1236 // Todo: implement
1237 }
1238 else
1239 {
1240 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1241 if (TrafficLight == nullptr)
1242 {
1244 }
1245 TrafficLight->SetTimeIsFrozen(bFreeze);
1246 }
1248}
1249
1251{
1252 if (IsDormant())
1253 {
1254 // Todo: implement
1255 }
1256 else
1257 {
1258 auto TrafficLight = Cast<ATrafficLightBase>(GetActor());
1259 if (TrafficLight == nullptr)
1260 {
1262 }
1263 TrafficLight->GetTrafficLightComponent()->GetGroup()->ResetGroup();
1264 }
1266}
1267
1268// FWalkerActor functions ---------------------
1269
1271 const FTransform& Transform,
1272 carla::rpc::WalkerControl WalkerControl)
1273{
1274 FVector NewLocation = Transform.GetLocation();
1275 FVector CurrentLocation = GetActorGlobalLocation();
1276
1277 // adjust position up by half of capsule height
1278 // (because in Unreal walker is centered at the capsule middle,
1279 // while Recast uses the bottom point)
1280 UCapsuleComponent* Capsule = Cast<UCapsuleComponent>(GetActor()->GetRootComponent());
1281 if (Capsule)
1282 {
1283 NewLocation.Z += Capsule->GetScaledCapsuleHalfHeight();
1284 }
1285
1286 FTransform NewTransform = Transform;
1287 NewTransform.SetLocation(NewLocation);
1288
1289 if (IsDormant())
1290 {
1291 FWalkerData* WalkerData = GetActorData<FWalkerData>();
1292 WalkerData->WalkerControl = WalkerControl;
1293 }
1294 else
1295 {
1296 auto * Walker = Cast<AWalkerBase>(GetActor());
1297 if (Walker && !Walker->bAlive)
1298 {
1300 }
1301
1302 // apply walker speed
1303 auto Pawn = Cast<APawn>(GetActor());
1304 if (Pawn == nullptr)
1305 {
1307 }
1308 auto Controller = Cast<AWalkerController>(Pawn->GetController());
1309 if (Controller == nullptr)
1310 {
1312 }
1313 Controller->ApplyWalkerControl(WalkerControl);
1314 }
1315 SetActorGlobalTransform(NewTransform);
1317}
1318
1321{
1322 if (IsDormant())
1323 {
1324 FWalkerData* WalkerData = GetActorData<FWalkerData>();
1325 Control = WalkerData->WalkerControl;
1326 }
1327 else
1328 {
1329 auto * Walker = Cast<AWalkerBase>(GetActor());
1330 if (Walker && !Walker->bAlive)
1331 {
1333 }
1334
1335 // apply walker speed
1336 auto Pawn = Cast<APawn>(GetActor());
1337 if (Pawn == nullptr)
1338 {
1340 }
1341 auto Controller = Cast<AWalkerController>(Pawn->GetController());
1342 if (Controller == nullptr)
1343 {
1345 }
1346 Control = Controller->GetWalkerControl();
1347 }
1349}
1350
1352{
1353 if (IsDormant())
1354 {
1355 ActorData->bSimulatePhysics = bEnabled;
1356 }
1357 else
1358 {
1359 auto* Character = Cast<ACharacter>(GetActor());
1360 // The physics in the walkers also works in a different way so to disable them,
1361 // we need to do it in the UCharacterMovementComponent.
1362 if (Character == nullptr)
1363 {
1365 }
1366 auto CharacterMovement = Cast<UCharacterMovementComponent>(Character->GetCharacterMovement());
1367 if(bEnabled) {
1368 CharacterMovement->SetDefaultMovementMode();
1369 }
1370 else {
1371 CharacterMovement->DisableMovement();
1372 }
1373 }
1375}
1376
1378{
1379 if (IsDormant())
1380 {
1381 }
1382 else
1383 {
1384 auto Character = Cast<ACharacter>(GetActor());
1385 // The physics in the walkers works in a different way so to disable them,
1386 // we need to do it in the UCharacterMovementComponent.
1387 if (Character == nullptr)
1388 {
1390 }
1391 auto CharacterMovement = Cast<UCharacterMovementComponent>(Character->GetCharacterMovement());
1392
1393 if(bEnabled) {
1394 CharacterMovement->SetDefaultMovementMode();
1395 }
1396 else {
1397 if (CharacterMovement->IsFlying() || CharacterMovement->IsFalling())
1398 CharacterMovement->DisableMovement();
1399 }
1400 }
1402}
1403
1405 const FWalkerControl& Control)
1406{
1407 if (IsDormant())
1408 {
1409 FWalkerData* ActorData = GetActorData<FWalkerData>();
1410 ActorData->WalkerControl = Control;
1411 }
1412 else
1413 {
1414 auto Pawn = Cast<APawn>(GetActor());
1415 if (Pawn == nullptr)
1416 {
1418 }
1419 auto Controller = Cast<AWalkerController>(Pawn->GetController());
1420 if (Controller == nullptr)
1421 {
1423 }
1424 Controller->ApplyWalkerControl(Control);
1425 }
1427}
1428
1430{
1431 if (IsDormant())
1432 {
1433 }
1434 else
1435 {
1436 auto Pawn = Cast<APawn>(GetActor());
1437 if (Pawn == nullptr)
1438 {
1440 }
1441 auto Controller = Cast<AWalkerController>(Pawn->GetController());
1442 if (Controller == nullptr)
1443 {
1445 }
1446 Controller->GetBonesTransform(Bones);
1447 }
1449}
1450
1452{
1453 if (IsDormant())
1454 {
1455 }
1456 else
1457 {
1458 auto Pawn = Cast<APawn>(GetActor());
1459 if (Pawn == nullptr)
1460 {
1462 }
1463 auto Controller = Cast<AWalkerController>(Pawn->GetController());
1464 if (Controller == nullptr)
1465 {
1467 }
1468 Controller->SetBonesTransform(Bones);
1469 }
1471}
1472
1474{
1475 if (IsDormant())
1476 {
1477 }
1478 else
1479 {
1480 auto Pawn = Cast<APawn>(GetActor());
1481 if (Pawn == nullptr)
1482 {
1484 }
1485 auto Controller = Cast<AWalkerController>(Pawn->GetController());
1486 if (Controller == nullptr)
1487 {
1489 }
1490 Controller->BlendPose(Blend);
1491 }
1493}
1494
1496{
1497 if (IsDormant())
1498 {
1499 }
1500 else
1501 {
1502 auto Pawn = Cast<APawn>(GetActor());
1503 if (Pawn == nullptr)
1504 {
1506 }
1507 auto Controller = Cast<AWalkerController>(Pawn->GetController());
1508 if (Controller == nullptr)
1509 {
1511 }
1512 Controller->GetPoseFromAnimation();
1513 }
1515}
1516
1518{
1519 if (IsDormant())
1520 {
1521 }
1522 else
1523 {
1524 auto Pawn = Cast<APawn>(GetActor());
1525 if (Pawn == nullptr)
1526 {
1528 }
1529 auto Walker = Cast<AWalkerBase>(Pawn);
1530 if (Walker == nullptr)
1531 {
1533 }
1534 Walker->StartDeathLifeSpan();
1535 UE_LOG(LogCarla, Warning, TEXT("Walker starting life span by dead"));
1536 }
1538}
carla::rpc::VehicleFailureState FailureState
Definition ActorData.h:137
FVehicleLightState LightState
Definition ActorData.h:131
FVehiclePhysicsControl PhysicsControl
Definition ActorData.h:116
FVehicleControl Control
Definition ActorData.h:119
FVehicleAckermannControl AckermannControl
Definition ActorData.h:122
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
TSharedPtr< const FActorInfo > carla::rpc::ActorState InState
ActorData
TSharedPtr< const FActorInfo > Info
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld Actor
ECarlaServerResponse
EVehicleWheelLocation
EVehicleDoor
Type of door to open/close
EVehicleInputPriority
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
FVector GlobalToLocalLocation(const FVector &InLocation) const
FTransform LocalToGlobalTransform(const FTransform &InTransform) const
FVector LocalToGlobalLocation(const FVector &InLocation) const
查看一个参与者和它的属性
Definition CarlaActor.h:23
carla::rpc::ActorState State
Definition CarlaActor.h:426
UWorld * World
Definition CarlaActor.h:438
ECarlaServerResponse AddActorImpulseAtLocation(const FVector &Impulse, const FVector &Location)
static TSharedPtr< FCarlaActor > ConstructCarlaActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, ActorType Type, carla::rpc::ActorState InState, UWorld *World)
ECarlaServerResponse AddActorForceAtLocation(const FVector &Force, const FVector &Location)
virtual ECarlaServerResponse SetActorEnableGravity(bool bEnabled)
FVector GetActorLocalLocation() const
void SetActorLocalTransform(const FTransform &Transform, ETeleportType Teleport=ETeleportType::TeleportPhysics)
ECarlaServerResponse AddActorTorque(const FVector &Torque)
ECarlaServerResponse SetActorTargetAngularVelocity(const FVector &AngularVelocity)
bool IsDormant() const
Definition CarlaActor.h:59
ECarlaServerResponse AddActorImpulse(const FVector &Impulse)
AActor * GetActor()
Definition CarlaActor.h:75
uint32 IdType
Definition CarlaActor.h:25
virtual ECarlaServerResponse SetActorCollisions(bool bEnabled)
AActor * TheActor
Definition CarlaActor.h:418
FVector GetActorAngularVelocity() const
FTransform GetActorGlobalTransform() const
FVector GetActorVelocity() const
void SetActorGlobalTransform(const FTransform &Transform, ETeleportType Teleport=ETeleportType::TeleportPhysics)
void SetActorGlobalLocation(const FVector &Location, ETeleportType Teleport=ETeleportType::TeleportPhysics)
TSharedPtr< const FActorInfo > Info
Definition CarlaActor.h:420
FVector GetActorGlobalLocation() const
void SetActorLocalLocation(const FVector &Location, ETeleportType Teleport=ETeleportType::TeleportPhysics)
TSharedPtr< FActorData > ActorData
Definition CarlaActor.h:436
IdType GetActorId() const
Definition CarlaActor.h:67
FTransform GetActorLocalTransform() const
ECarlaServerResponse SetActorTargetVelocity(const FVector &Velocity)
ECarlaServerResponse AddActorForce(const FVector &Force)
FCarlaActor()=default
virtual ECarlaServerResponse SetActorSimulatePhysics(bool bEnabled)
void WakeActorUp(UCarlaEpisode *CarlaEpisode)
ECarlaServerResponse AddActorAngularImpulse(const FVector &AngularInpulse)
void PutActorToSleep(UCarlaEpisode *CarlaEpisode)
FOtherActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, carla::rpc::ActorState InState, UWorld *World)
FSensorActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, carla::rpc::ActorState InState, UWorld *World)
virtual ECarlaServerResponse SetLightGreenTime(float time) final
virtual ECarlaServerResponse FreezeTrafficLight(bool bFreeze) final
virtual ETrafficLightState GetTrafficLightState() const final
FTrafficLightActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, carla::rpc::ActorState InState, UWorld *World)
virtual ECarlaServerResponse SetTrafficLightState(const ETrafficLightState &State) final
virtual ECarlaServerResponse ResetTrafficLightGroup() final
virtual UTrafficLightController * GetTrafficLightController() final
virtual ECarlaServerResponse SetLightRedTime(float time) final
virtual ECarlaServerResponse SetLightYellowTime(float time) final
FTrafficSignActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, carla::rpc::ActorState InState, UWorld *World)
virtual ECarlaServerResponse SetActorAutopilot(bool bEnabled, bool bKeepState=false) final
virtual ECarlaServerResponse EnableChronoPhysics(uint64_t MaxSubsteps, float MaxSubstepDeltaTime, const FString &VehicleJSON, const FString &PowertrainJSON, const FString &TireJSON, const FString &BaseJSONPath) final
virtual ECarlaServerResponse SetActorSimulatePhysics(bool bSimulatePhysics) final
FVehicleActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, carla::rpc::ActorState InState, UWorld *World)
virtual ECarlaServerResponse DisableActorConstantVelocity() final
virtual ECarlaServerResponse GetVehicleLightState(FVehicleLightState &LightState) final
virtual ECarlaServerResponse GetFailureState(carla::rpc::VehicleFailureState &) final
virtual ECarlaServerResponse GetWheelSteerAngle(const EVehicleWheelLocation &WheelLocation, float &Angle)
virtual ECarlaServerResponse ApplyControlToVehicle(const FVehicleControl &, const EVehicleInputPriority &) final
virtual ECarlaServerResponse EnableActorConstantVelocity(const FVector &Velocity) final
virtual ECarlaServerResponse SetVehicleLightState(const FVehicleLightState &LightState) final
virtual ECarlaServerResponse EnableCarSim(const FString &SimfilePath) final
virtual ECarlaServerResponse GetPhysicsControl(FVehiclePhysicsControl &PhysicsControl) final
virtual ECarlaServerResponse SetWheelSteerDirection(const EVehicleWheelLocation &WheelLocation, float AngleInDeg) final
virtual ECarlaServerResponse UseCarSimRoad(bool bEnabled) final
virtual ECarlaServerResponse GetVehicleControl(FVehicleControl &) final
virtual ECarlaServerResponse ApplyAckermannControllerSettings(const FAckermannControllerSettings &) final
virtual ECarlaServerResponse GetVehicleTelemetryData(FVehicleTelemetryData &) final
virtual ECarlaServerResponse ApplyAckermannControlToVehicle(const FVehicleAckermannControl &, const EVehicleInputPriority &) final
virtual ECarlaServerResponse ApplyPhysicsControl(const FVehiclePhysicsControl &PhysicsControl) final
virtual ECarlaServerResponse OpenVehicleDoor(const EVehicleDoor DoorIdx) final
virtual ECarlaServerResponse CloseVehicleDoor(const EVehicleDoor DoorIdx) final
virtual ECarlaServerResponse GetVehicleAckermannControl(FVehicleAckermannControl &) final
virtual ECarlaServerResponse RestorePhysXPhysics()
virtual ECarlaServerResponse ShowVehicleDebugTelemetry(bool bEnabled) final
virtual ECarlaServerResponse GetAckermannControllerSettings(FAckermannControllerSettings &) final
virtual ECarlaServerResponse BlendPose(float Blend)
virtual ECarlaServerResponse SetActorSimulatePhysics(bool bSimulatePhysics) final
virtual ECarlaServerResponse SetActorEnableGravity(bool bEnabled) final
virtual ECarlaServerResponse GetPoseFromAnimation()
FWalkerActor(IdType ActorId, AActor *Actor, TSharedPtr< const FActorInfo > Info, carla::rpc::ActorState InState, UWorld *World)
virtual ECarlaServerResponse SetBonesTransform(const FWalkerBoneControlIn &) final
virtual ECarlaServerResponse GetWalkerControl(FWalkerControl &) final
virtual ECarlaServerResponse SetWalkerState(const FTransform &Transform, carla::rpc::WalkerControl WalkerControl) final
virtual ECarlaServerResponse SetActorDead()
virtual ECarlaServerResponse ApplyControlToWalker(const FWalkerControl &) final
virtual ECarlaServerResponse GetBonesTransform(FWalkerBoneControlOut &) final
carla::rpc::WalkerControl WalkerControl
Definition ActorData.h:165
static void CreateCarsimComponent(ACarlaWheeledVehicle *Vehicle, FString Simfile)
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
static void CreateChronoMovementComponent(ACarlaWheeledVehicle *Vehicle, uint64_t MaxSubsteps, float MaxSubstepDeltaTime, FString VehicleJSON="", FString PowertrainJSON="", FString TireJSON="", FString BaseJSONPath="")
从 OpenDrive 映射一个控制器。控制相关交通信号灯并包含其循环