27#include "Components/BoxComponent.h"
28#include "Engine/StaticMeshActor.h"
35#include "EngineUtils.h"
38std::pair<int, FCarlaActor*>CarlaReplayerHelper::TryToCreateReplayerActor(
45 check(Episode !=
nullptr);
48 if (ActorDesc.
Id.StartsWith(
"traffic."))
50 FCarlaActor* CarlaActor = FindTrafficLightAt(Location);
51 if (CarlaActor !=
nullptr)
54 return std::pair<int, FCarlaActor*>(2, CarlaActor);
59 UE_LOG(LogCarla, Log, TEXT(
"TrafficLight not found"));
60 return std::pair<int, FCarlaActor*>(0,
nullptr);
63 else if (SpawnSensors || !ActorDesc.
Id.StartsWith(
"sensor."))
66 if (Episode->GetActorRegistry().Contains(DesiredId))
68 auto* CarlaActor = Episode->FindCarlaActor(DesiredId);
70 if (desc->
Id == ActorDesc.
Id)
74 FRotator Rot = FRotator::MakeFromEuler(Rotation);
75 FTransform Trans2(Rot, Location, FVector(1, 1, 1));
77 return std::pair<int, FCarlaActor*>(2, CarlaActor);
81 FRotator Rot = FRotator::MakeFromEuler(Rotation);
82 FTransform Trans(Rot, FVector(0, 0, 100000), FVector(1, 1, 1));
84 TPair<EActorSpawnResultStatus, FCarlaActor*> Result = Episode->SpawnActorWithInfo(Trans, ActorDesc, DesiredId);
85 if (Result.Key == EActorSpawnResultStatus::Success)
88 FTransform Trans2(Rot, Location, FVector(1, 1, 1));
89 Result.Value->SetActorGlobalTransform(Trans2);
95 return std::pair<int, FCarlaActor*>(1, Result.Value);
99 UE_LOG(LogCarla, Log, TEXT(
"Actor could't be created by replayer"));
100 return std::pair<int, FCarlaActor*>(0, Result.Value);
106 return std::pair<int, FCarlaActor*>(0,
nullptr);
110FCarlaActor *CarlaReplayerHelper::FindTrafficLightAt(FVector Location)
112 check(Episode !=
nullptr);
113 auto World = Episode->GetWorld();
114 check(
World !=
nullptr);
117 int x =
static_cast<int>(
Location.X);
118 int y =
static_cast<int>(
Location.Y);
119 int z =
static_cast<int>(
Location.Z);
123 for (
auto It = Registry.begin(); It != Registry.end(); ++It)
129 int x2 =
static_cast<int>(vec.X);
130 int y2 =
static_cast<int>(vec.Y);
131 int z2 =
static_cast<int>(vec.Z);
132 if ((x2 == x) && (y2 == y) && (z2 == z))
144bool CarlaReplayerHelper::SetActorSimulatePhysics(
FCarlaActor* CarlaActor,
bool bEnabled)
160bool CarlaReplayerHelper::SetActorAutopilot(
FCarlaActor* CarlaActor,
bool bEnabled,
bool bKeepState)
176std::pair<int, uint32_t> CarlaReplayerHelper::ProcessReplayerEventAdd(
182 bool bIgnoreSpectator,
185 check(Episode !=
nullptr);
190 ActorDesc.
UId = Description.
UId;
191 ActorDesc.
Id = Description.
Id;
192 for (
const auto &Item : Description.
Attributes)
197 Attr.
Value = Item.Value;
200 if (Item.Id ==
"role_name" && Item.Value ==
"hero")
205 if ((bIgnoreHero && IsHero) ||
206 (bIgnoreSpectator && ActorDesc.
Id.StartsWith(
"spectator")))
208 return std::make_pair(3, 0);
211 auto result = TryToCreateReplayerActor(
218 if (result.first != 0)
225 if (!(bIgnoreHero && IsHero))
228 SetActorSimulatePhysics(result.second,
false);
230 result.second->GetActor()->SetActorEnableCollision(
true);
231 auto RootComponent = Cast<UPrimitiveComponent>(
232 result.second->GetActor()->GetRootComponent());
233 RootComponent->SetSimulatePhysics(
false);
234 RootComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
237 SetActorAutopilot(result.second,
false,
false);
242 SetActorSimulatePhysics(result.second,
true);
244 result.second->GetActor()->SetActorEnableCollision(
true);
247 return std::make_pair(result.first, result.second->GetActorId());
249 return std::make_pair(result.first, 0);
253bool CarlaReplayerHelper::ProcessReplayerEventDel(uint32_t DatabaseId)
255 check(Episode !=
nullptr);
256 FCarlaActor* CarlaActor = Episode->FindCarlaActor(DatabaseId);
257 if (CarlaActor ==
nullptr)
259 UE_LOG(LogCarla, Log, TEXT(
"Actor %d not found to destroy"), DatabaseId);
262 Episode->DestroyActor(CarlaActor->
GetActorId());
267bool CarlaReplayerHelper::ProcessReplayerEventParent(uint32_t ChildId, uint32_t ParentId)
269 check(Episode !=
nullptr);
270 FCarlaActor * Child = Episode->FindCarlaActor(ChildId);
271 FCarlaActor * Parent = Episode->FindCarlaActor(ParentId);
274 UE_LOG(LogCarla, Log, TEXT(
"Parenting Child actors not found"));
279 UE_LOG(LogCarla, Log, TEXT(
"Parenting Parent actors not found"));
289 Episode->AttachActors(
297 Episode->PutActorToSleep(Child->
GetActorId());
305 check(Episode !=
nullptr);
312 if(bIgnoreSpectator && CarlaActor->
GetActor()->GetClass()->GetFName().ToString().Contains(
"Spectator"))
330 FTransform Trans(Rotation, Location, FVector(1, 1, 1));
339 check(Episode !=
nullptr)
341 if (CarlaActor ==
nullptr)
346 check(CarlaVehicle !=
nullptr)
347 USkeletalMeshComponent* SkeletalMesh = CarlaVehicle->GetMesh();
348 check(SkeletalMesh !=
nullptr)
349 UVehicleAnimInstance* VehicleAnim = Cast<UVehicleAnimInstance>(SkeletalMesh->GetAnimInstance());
350 check(VehicleAnim !=
nullptr)
352 for (uint32_t i = 0; i < VehicleAnimWheels.WheelValues.size(); ++i)
361bool CarlaReplayerHelper::SetCameraPosition(uint32_t Id, FVector Offset, FQuat Rotation)
363 check(Episode !=
nullptr);
366 FCarlaActor* CarlaActor = Episode->FindCarlaActor(Id);
370 APawn *Spectator = Episode->GetSpectatorPawn();
374 FCarlaActor* CarlaSpectator = Episode->FindCarlaActor(Spectator);
380 FQuat ActorRot = ActorTransform.GetRotation();
381 FVector Pos = ActorTransform.GetTranslation() + (ActorRot.RotateVector(Offset));
389 check(Episode !=
nullptr);
412 check(Episode !=
nullptr);
431 check(Episode !=
nullptr);
449 check(Episode !=
nullptr);
460 check(Episode !=
nullptr);
461 UWorld*
World = Episode->GetWorld();
465 if (!CarlaLightSubsystem)
472 CarlaLight->SetLightIntensity(LightScene.
Intensity);
473 CarlaLight->SetLightColor(LightScene.
Color);
474 CarlaLight->SetLightOn(LightScene.
bOn);
475 CarlaLight->SetLightType(
static_cast<ELightType>(LightScene.
Type));
488 check(Episode !=
nullptr);
490 if (CarlaActor ==
nullptr)
493 check(CarlaVehicle !=
nullptr)
494 CarlaVehicle->SetSpeedAnim(Biker.ForwardSpeed);
495 CarlaVehicle->SetRotationAnim(Biker.EngineRotation);
501 check(Episode !=
nullptr);
504 if (!CarlaActor)
return;
507 auto Walker = Cast<APawn>(
Actor);
510 AWalkerController *Controller = Cast<AWalkerController>(Walker->GetController());
511 if (!Controller)
return;
517 FTransform Trans(FRotator::MakeFromEuler(Bone.Rotation), Bone.Location, FVector(1, 1, 1));
527bool CarlaReplayerHelper::ProcessReplayerFinish(
bool bApplyAutopilot,
bool bIgnoreHero, std::unordered_map<uint32_t, bool> &IsHero)
531 for (
auto& It : Registry)
542 if (!(bIgnoreHero && IsHero[CarlaActor->
GetActorId()]))
545 SetActorSimulatePhysics(CarlaActor,
true);
546 SetActorVelocity(CarlaActor, FVector(0, 0, 0));
562 SetWalkerSpeed(CarlaActor->
GetActorId(), 0.0f);
569void CarlaReplayerHelper::SetActorVelocity(
FCarlaActor *CarlaActor, FVector Velocity)
579void CarlaReplayerHelper::SetWalkerSpeed(uint32_t ActorId,
float Speed)
581 check(Episode !=
nullptr);
582 FCarlaActor * CarlaActor = Episode->FindCarlaActor(ActorId);
592void CarlaReplayerHelper::RemoveStaticProps()
594 check(Episode !=
nullptr);
595 auto World = Episode->GetWorld();
596 for (TActorIterator<AStaticMeshActor> It(
World); It; ++It)
599 check(
Actor !=
nullptr);
600 auto MeshComponent =
Actor->GetStaticMeshComponent();
601 check(MeshComponent !=
nullptr);
602 if (MeshComponent->Mobility == EComponentMobility::Movable)
FVehicleLightState LightState
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
if(!Actor||!Actor->Destroy())
const FCarlaActor * FindCarlaActor(const AActor *Actor) const
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld * World
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld Actor
for(int32 i=0;i< ResultPropJsonArray.Num();++i)
EVehicleDoor
Type of door to open/close
EActorAttributeType
List of valid types for actor attributes.(角色属性有效类型的列表)
Base class for CARLA wheeled vehicles.
void OnActorSpawned(const FCarlaActor &CarlaActor)
如果你的类继承自UObject,你的类名上方需要加入 UCLASS() 宏 实现交通信号灯状态改变的类
void SetFrozenGroup(bool InFreeze)
void BlendPose(float Blend)
void SetBonesTransform(const FWalkerBoneControlIn &WalkerBones)
ActorType GetActorType() const
virtual ECarlaServerResponse SetActorAutopilot(bool, bool bKeepState=false)
virtual ECarlaServerResponse SetVehicleLightState(const FVehicleLightState &)
virtual ECarlaServerResponse ApplyControlToVehicle(const FVehicleControl &, const EVehicleInputPriority &)
void SetParent(IdType InParentId)
void SetAttachmentType(carla::rpc::AttachmentType InAttachmentType)
FTransform GetActorGlobalTransform() const
void SetActorGlobalTransform(const FTransform &Transform, ETeleportType Teleport=ETeleportType::TeleportPhysics)
virtual ECarlaServerResponse ApplyControlToWalker(const FWalkerControl &)
FVector GetActorGlobalLocation() const
void AddChildren(IdType ChildId)
virtual UTrafficLightController * GetTrafficLightController()
const FActorInfo * GetActorInfo() const
IdType GetActorId() const
ECarlaServerResponse SetActorTargetVelocity(const FVector &Velocity)
virtual ECarlaServerResponse SetActorSimulatePhysics(bool bEnabled)
virtual ECarlaServerResponse SetTrafficLightState(const ETrafficLightState &)
UCarlaLight * GetLight(int Id)
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
从 OpenDrive 映射一个控制器。控制相关交通信号灯并包含其循环
void SetElapsedTime(float InElapsedTime)
ATrafficLightGroup * GetGroup()
std::vector< WheelInfo > WheelValues
VehicleLightStateType State
An actor attribute, may be an intrinsic (non-modifiable) attribute of the actor or an user-defined ac...
TMap< FString, FActorAttribute > Variations
用户选择了参与者的变化版本。请注意,此时是 由不可修改的属性表示
FActorDescription Description
TMap< FString, FTransform > BoneTransforms
EVehicleWheelLocation Location