CARLA
ActorData.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 "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 
30 void FActorData::RecordActorData(FCarlaActor* CarlaActor, UCarlaEpisode* CarlaEpisode)
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 
46 void FActorData::RestoreActorData(FCarlaActor* CarlaActor, UCarlaEpisode* CarlaEpisode)
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 
83 FTransform 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);
94  if (bSimulatePhysics)
95  {
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);
117  if (bSimulatePhysics)
118  {
120  }
121  Vehicle->ApplyAckermannControllerSettings(AckermannControllerSettings);
122  if (!bAckermannControlActive)
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);
232  Controller->AddCarlaActorTrafficLight(CarlaActor);
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);
243  Controller->RemoveCarlaActorTrafficLight(CarlaActor);
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 }
void ApplyVehicleControl(const FVehicleControl &Control, EVehicleInputPriority Priority)
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:178
std::string SignId
Definition: RoadTypes.h:25
carla::rpc::VehicleFailureState GetFailureState() const
bool bSimulatePhysics
Definition: ActorData.h:41
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:237
UTrafficLightComponent * GetTrafficLightComponent()
FVehiclePhysicsControl GetVehiclePhysicsControl() const
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode)
Definition: ActorData.cpp:30
bool IsAckermannControlActive() const
void SetSimulatePhysics(bool enabled)
AActor * GetActor()
Definition: CarlaActor.h:90
void ApplyVehicleAckermannControl(const FVehicleAckermannControl &AckermannControl, EVehicleInputPriority Priority)
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info)
Definition: ActorData.cpp:22
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:193
ETrafficLightState GetTrafficLightState() const
FVector Velocity
Definition: ActorData.h:37
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info) override
Definition: ActorData.cpp:166
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:222
static ACarlaGameModeBase * GetGameMode(const UObject *WorldContextObject)
Definition: CarlaStatics.h:58
void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl)
carla::SharedPtr< cc::Actor > Actor
A simulation episode.
Definition: CarlaEpisode.h:37
void SetFailureState(const carla::rpc::VehicleFailureState &FailureState)
FVector Scale
Definition: ActorData.h:35
const FVehicleControl & GetVehicleControl() const
Vehicle control currently applied to this vehicle.
const boost::optional< carla::road::Map > & GetMap() const
detail::Stream< detail::MultiStreamState > Stream
A stream represents an unidirectional channel for sending data from server to client.
Definition: Stream.h:19
FVector AngularVelocity
Definition: ActorData.h:39
FAckermannControllerSettings GetAckermannControllerSettings() const
FActorDescription Description
Definition: ActorInfo.h:26
FIntVector GetCurrentMapOrigin() const
Definition: CarlaEpisode.h:321
FTransform GetLocalTransform(UCarlaEpisode *CarlaEpisode) const
Definition: ActorData.cpp:83
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:260
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:111
void SetVehicleLightState(const FVehicleLightState &LightState)
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:139
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info) override
Definition: ActorData.cpp:210
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:152
A view over an actor and its properties.
Definition: ActorInfo.h:22
Base class for the CARLA Game Mode.
FQuat Rotation
Definition: ActorData.h:33
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode)
Definition: ActorData.cpp:46
void SetDataStream(FDataStream InStream)
Replace the FDataStream associated with this sensor.
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:252
AActor * ReSpawnActorWithInfo(const FTransform &Transform, FActorDescription thisActorDescription)
Spawns an actor based on ActorDescription at Transform.
Definition: CarlaEpisode.h:212
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:89
Base class for CARLA wheeled vehicles.
const FVehicleAckermannControl & GetVehicleAckermannControl() const
Vehicle Ackermann control currently applied to this vehicle.
void SetTrafficLightState(ETrafficLightState State)
void SetPoleIndex(int InPoleIndex)
geom::Transform Transform
Definition: rpc/Transform.h:16
AActor * SpawnActor(const FTransform &Transform, FActorDescription ActorDescription)
Spawns an actor based on ActorDescription at Transform.
Definition: CarlaEpisode.h:233
void ApplyAckermannControllerSettings(const FAckermannControllerSettings &AckermannControllerSettings)
A view over an actor and its properties.
Definition: CarlaActor.h:23
FVehicleLightState GetVehicleLightState() const
FDVector Location
Definition: ActorData.h:31