CARLA
 
载入中...
搜索中...
未找到
ActorData.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 "ActorData.h"
16#include "GameFramework/CharacterMovementComponent.h"
19#include "Carla/Sensor/Sensor.h"
20#include "CarlaActor.h"
21
23{
24 FTransform SpawnTransform = GetLocalTransform(CarlaEpisode);
25 // hack to prevent wheel intersection with geometry
26 SpawnTransform.AddToTranslation(FVector(0,0,15));
27 return CarlaEpisode->ReSpawnActorWithInfo(SpawnTransform, Info.Description);
28}
29
31{
32 AActor* Actor = CarlaActor->GetActor();
33 FTransform Transform = Actor->GetTransform();
34 Location = FDVector(Transform.GetLocation()) + CarlaEpisode->GetCurrentMapOrigin();
35 Rotation = Transform.GetRotation();
36 Scale = Transform.GetScale3D();
37 UPrimitiveComponent* Component = Cast<UPrimitiveComponent>(Actor->GetRootComponent());
38 if (Component)
39 {
40 bSimulatePhysics = Component->IsSimulatingPhysics();
41 AngularVelocity = Component->GetPhysicsAngularVelocityInDegrees();
42 }
43 Velocity = Actor->GetVelocity();
44}
45
47{
48 AActor* Actor = CarlaActor->GetActor();
49 Actor->SetActorTransform(GetLocalTransform(CarlaEpisode));
50 UPrimitiveComponent* Component = Cast<UPrimitiveComponent>(Actor->GetRootComponent());
51 if (Component)
52 {
53 Component->SetPhysicsLinearVelocity(
54 Velocity, false, "None");
55 Component->SetPhysicsAngularVelocityInDegrees(
56 AngularVelocity, false, "None");
57 }
58 auto* Character = Cast<ACharacter>(Actor);
59 auto* CarlaVehicle = Cast<ACarlaWheeledVehicle>(Actor);
60 // The physics in the vehicles works in a different way so to disable them.
61 if (CarlaVehicle != nullptr){
62 CarlaVehicle->SetSimulatePhysics(bSimulatePhysics);
63 }
64 // The physics in the walkers also works in a different way so to disable them,
65 // we need to do it in the UCharacterMovementComponent.
66 else if (Character != nullptr)
67 {
68
69 }
70 // In the rest of actors, the physics is controlled with the UPrimitiveComponent, so we use
71 // that for disable it.
72 else
73 {
74 auto RootComponent = Component;
75 if (RootComponent != nullptr)
76 {
77 RootComponent->SetSimulatePhysics(bSimulatePhysics);
78 RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
79 }
80 }
81}
82
83FTransform FActorData::GetLocalTransform(UCarlaEpisode* CarlaEpisode) const
84{
85 FVector LocalLocation = (Location - CarlaEpisode->GetCurrentMapOrigin()).ToFVector();
86 return FTransform(Rotation, LocalLocation, Scale);
87}
88
90{
91 FActorData::RecordActorData(CarlaActor, CarlaEpisode);
92 AActor* Actor = CarlaActor->GetActor();
93 ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(Actor);
95 {
96 PhysicsControl = Vehicle->GetVehiclePhysicsControl();
97 }
98 Control = Vehicle->GetVehicleControl();
99 AckermannControl = Vehicle->GetVehicleAckermannControl();
100 bAckermannControlActive = Vehicle->IsAckermannControlActive();
101 AckermannControllerSettings = Vehicle->GetAckermannControllerSettings();
102 LightState = Vehicle->GetVehicleLightState();
103 auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
104 if (Controller)
105 {
106 SpeedLimit = Controller->GetSpeedLimit();
107 }
108 FailureState = Vehicle->GetFailureState();
109}
110
112{
113 FActorData::RestoreActorData(CarlaActor, CarlaEpisode);
114 AActor* Actor = CarlaActor->GetActor();
115 ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(Actor);
116 Vehicle->SetSimulatePhysics(bSimulatePhysics);
118 {
119 Vehicle->ApplyVehiclePhysicsControl(PhysicsControl);
120 }
121 Vehicle->ApplyAckermannControllerSettings(AckermannControllerSettings);
123 {
124 Vehicle->ApplyVehicleControl(Control, EVehicleInputPriority::Client);
125 }
126 else
127 {
128 Vehicle->ApplyVehicleAckermannControl(AckermannControl, EVehicleInputPriority::Client);
129 }
130 Vehicle->SetVehicleLightState(LightState);
131 auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
132 if (Controller)
133 {
134 Controller->SetSpeedLimit(SpeedLimit);
135 }
136 Vehicle->SetFailureState(FailureState);
137}
138
140{
141 FActorData::RecordActorData(CarlaActor, CarlaEpisode);
142 AActor* Actor = CarlaActor->GetActor();
143 auto Walker = Cast<AWalkerBase>(Actor);
144 auto Controller = Walker != nullptr ? Cast<AWalkerController>(Walker->GetController()) : nullptr;
145 if (Controller != nullptr)
146 {
147 WalkerControl = carla::rpc::WalkerControl{Controller->GetWalkerControl()};
148 }
149 bAlive = Walker->bAlive;
150}
151
153{
154 FActorData::RestoreActorData(CarlaActor, CarlaEpisode);
155 AActor* Actor = CarlaActor->GetActor();
156 auto Walker = Cast<ACharacter>(Actor);
157 auto Controller = Walker != nullptr ? Cast<AWalkerController>(Walker->GetController()) : nullptr;
158 if (Controller != nullptr)
159 {
160 Controller->ApplyWalkerControl(WalkerControl);
161 }
162 auto CharacterMovement = Cast<UCharacterMovementComponent>(Walker->GetCharacterMovement());
163 // TODO: Handle death timer
164}
165
167{
168 FTransform SpawnTransform = GetLocalTransform(CarlaEpisode);
169 FActorSpawnParameters SpawnParams;
170 SpawnParams.SpawnCollisionHandlingOverride =
171 ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
172 return CarlaEpisode->GetWorld()->SpawnActor<ATrafficSignBase>(
173 Model,
174 SpawnTransform,
175 SpawnParams);
176}
177
179{
180 FActorData::RecordActorData(CarlaActor, CarlaEpisode);
181 AActor* Actor = CarlaActor->GetActor();
182 Model = Actor->GetClass();
183 ATrafficSignBase* TrafficSign = Cast<ATrafficSignBase>(Actor);
184 USignComponent* TrafficSignComponent =
185 Cast<USignComponent>(TrafficSign->FindComponentByClass<USignComponent>());
186 if (TrafficSignComponent)
187 {
188 SignModel = TrafficSignComponent->GetClass();
189 SignId = TrafficSignComponent->GetSignId();
190 }
191}
192
194{
195 AActor* Actor = CarlaActor->GetActor();
196 if (SignId.Len())
197 {
198 USignComponent* SignComponent =
199 NewObject<USignComponent>(Actor, SignModel);
200 SignComponent->SetSignId(SignId);
201 SignComponent->RegisterComponent();
202 SignComponent->AttachToComponent(
203 Actor->GetRootComponent(),
204 FAttachmentTransformRules::KeepRelativeTransform);
205 ACarlaGameModeBase *GameMode = UCarlaStatics::GetGameMode(CarlaEpisode->GetWorld());
206 SignComponent->InitializeSign(GameMode->GetMap().get());
207 }
208}
209
211{
212 FTransform SpawnTransform = GetLocalTransform(CarlaEpisode);
213 FActorSpawnParameters SpawnParams;
214 SpawnParams.SpawnCollisionHandlingOverride =
215 ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
216 return CarlaEpisode->GetWorld()->SpawnActor<ATrafficLightBase>(
217 Model,
218 SpawnTransform,
219 SpawnParams);
220}
221
223{
224 FActorData::RecordActorData(CarlaActor, CarlaEpisode);
225 AActor* Actor = CarlaActor->GetActor();
226 Model = Actor->GetClass();
227 ATrafficLightBase* TrafficLight = Cast<ATrafficLightBase>(Actor);
228 UTrafficLightComponent* Component = TrafficLight->GetTrafficLightComponent();
229 SignId = Component->GetSignId();
230 Controller = Component->GetController();
231 Controller->RemoveTrafficLight(Component);
233 LightState = TrafficLight->GetTrafficLightState();
234 PoleIndex = TrafficLight->GetPoleIndex();
235}
236
238{
239 AActor* Actor = CarlaActor->GetActor();
240 ATrafficLightBase* TrafficLight = Cast<ATrafficLightBase>(Actor);
241 UTrafficLightComponent* Component = TrafficLight->GetTrafficLightComponent();
242 Component->SetSignId(SignId);
244 Controller->AddTrafficLight(Component);
245 ACarlaGameModeBase *GameMode = UCarlaStatics::GetGameMode(CarlaEpisode->GetWorld());
246 Component->InitializeSign(GameMode->GetMap().get());
247 Component->SetLightState(Controller->GetCurrentState().State);
248 TrafficLight->SetPoleIndex(PoleIndex);
249 TrafficLight->SetTrafficLightState(LightState);
250}
251
253{
254 FActorData::RecordActorData(CarlaActor, CarlaEpisode);
255 AActor* Actor = CarlaActor->GetActor();
256 ASensor* Sensor = Cast<ASensor>(Actor);
257 Stream = Sensor->MoveDataStream();
258}
259
261{
262 FActorData::RestoreActorData(CarlaActor, CarlaEpisode);
263 AActor* Actor = CarlaActor->GetActor();
264 ASensor* Sensor = Cast<ASensor>(Actor);
265 Sensor->SetDataStream(std::move(Stream));
266}
Base class for the CARLA Game Mode.
const boost::optional< carla::road::Map > & GetMap() const
Base class for CARLA wheeled vehicles.
void SetDataStream(FDataStream InStream)
Replace the FDataStream associated with this sensor.
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode)
Definition ActorData.cpp:30
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info)
Definition ActorData.cpp:22
FQuat Rotation
Definition ActorData.h:34
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode)
Definition ActorData.cpp:46
FVector Velocity
Definition ActorData.h:38
bool bSimulatePhysics
Definition ActorData.h:42
FTransform GetLocalTransform(UCarlaEpisode *CarlaEpisode) const
Definition ActorData.cpp:83
FVector Scale
Definition ActorData.h:36
FDVector Location
Definition ActorData.h:32
FDataStream Stream
Definition ActorData.h:135
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
A view over an actor and its properties.
Definition CarlaActor.h:25
AActor * GetActor()
Definition CarlaActor.h:91
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
ETrafficLightState LightState
Definition ActorData.h:115
UTrafficLightController * Controller
Definition ActorData.h:113
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
TSubclassOf< AActor > Model
Definition ActorData.h:119
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info) override
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
TSubclassOf< UObject > SignModel
Definition ActorData.h:100
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info) override
TSubclassOf< AActor > Model
Definition ActorData.h:98
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
bool bAckermannControlActive
Definition ActorData.h:65
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition ActorData.cpp:89
FVehicleLightState LightState
Definition ActorData.h:69
FVehicleControl Control
Definition ActorData.h:61
float SpeedLimit
Definition ActorData.h:71
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
carla::rpc::VehicleFailureState FailureState
Definition ActorData.h:73
FVehicleAckermannControl AckermannControl
Definition ActorData.h:63
FAckermannControllerSettings AckermannControllerSettings
Definition ActorData.h:67
FVehiclePhysicsControl PhysicsControl
Definition ActorData.h:59
carla::rpc::WalkerControl WalkerControl
Definition ActorData.h:84
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
bool bAlive
Definition ActorData.h:86
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
A simulation episode.
FIntVector GetCurrentMapOrigin() const
AActor * SpawnActor(const FTransform &Transform, FActorDescription ActorDescription)
Spawns an actor based on ActorDescription at Transform.
AActor * ReSpawnActorWithInfo(const FTransform &Transform, FActorDescription thisActorDescription)
Spawns an actor based on ActorDescription at Transform.
static ACarlaGameModeBase * GetGameMode(const UObject *WorldContextObject)
void RemoveCarlaActorTrafficLight(FCarlaActor *CarlaActor)
const FTrafficLightStage & GetCurrentState() const
void AddCarlaActorTrafficLight(FCarlaActor *CarlaActor)
void AddTrafficLight(UTrafficLightComponent *TrafficLight)
void RemoveTrafficLight(UTrafficLightComponent *TrafficLight)
A view over an actor and its properties.
Definition ActorInfo.h:23
FActorDescription Description
Definition ActorInfo.h:26
ETrafficLightState State