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  auto CharacterMovement = Cast<UCharacterMovementComponent>(Character->GetCharacterMovement());
69 
70  if(bSimulatePhysics) {
71  CharacterMovement->SetDefaultMovementMode();
72  }
73  else {
74  CharacterMovement->DisableMovement();
75  }
76  }
77  // In the rest of actors, the physics is controlled with the UPrimitiveComponent, so we use
78  // that for disable it.
79  else
80  {
81  auto RootComponent = Component;
82  if (RootComponent != nullptr)
83  {
84  RootComponent->SetSimulatePhysics(bSimulatePhysics);
85  RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
86  }
87  }
88 }
89 
90 FTransform FActorData::GetLocalTransform(UCarlaEpisode* CarlaEpisode) const
91 {
92  FVector LocalLocation = (Location - CarlaEpisode->GetCurrentMapOrigin()).ToFVector();
93  return FTransform(Rotation, LocalLocation, Scale);
94 }
95 
97 {
98  FActorData::RecordActorData(CarlaActor, CarlaEpisode);
99  AActor* Actor = CarlaActor->GetActor();
100  ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(Actor);
101  if (bSimulatePhysics)
102  {
104  }
105  Control = Vehicle->GetVehicleControl();
106  LightState = Vehicle->GetVehicleLightState();
107  auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
108  if (Controller)
109  {
110  SpeedLimit = Controller->GetSpeedLimit();
111  }
112 }
113 
115 {
116  FActorData::RestoreActorData(CarlaActor, CarlaEpisode);
117  AActor* Actor = CarlaActor->GetActor();
118  ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(Actor);
120  if (bSimulatePhysics)
121  {
123  }
124  Vehicle->ApplyVehicleControl(Control, EVehicleInputPriority::Client);
125  Vehicle->SetVehicleLightState(LightState);
126  auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
127  if (Controller)
128  {
129  Controller->SetSpeedLimit(SpeedLimit);
130  }
131 }
132 
134 {
135  FActorData::RecordActorData(CarlaActor, CarlaEpisode);
136  AActor* Actor = CarlaActor->GetActor();
137  auto Walker = Cast<APawn>(Actor);
138  auto Controller = Walker != nullptr ? Cast<AWalkerController>(Walker->GetController()) : nullptr;
139  if (Controller != nullptr)
140  {
141  WalkerControl = carla::rpc::WalkerControl{Controller->GetWalkerControl()};
142  }
143 }
144 
146 {
147  FActorData::RestoreActorData(CarlaActor, CarlaEpisode);
148  AActor* Actor = CarlaActor->GetActor();
149  auto Walker = Cast<APawn>(Actor);
150  auto Controller = Walker != nullptr ? Cast<AWalkerController>(Walker->GetController()) : nullptr;
151  if (Controller != nullptr)
152  {
153  Controller->ApplyWalkerControl(WalkerControl);
154  }
155 }
156 
158 {
159  FTransform SpawnTransform = GetLocalTransform(CarlaEpisode);
160  FActorSpawnParameters SpawnParams;
161  SpawnParams.SpawnCollisionHandlingOverride =
162  ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
163  return CarlaEpisode->GetWorld()->SpawnActor<ATrafficSignBase>(
164  Model,
165  SpawnTransform,
166  SpawnParams);
167 }
168 
170 {
171  FActorData::RecordActorData(CarlaActor, CarlaEpisode);
172  AActor* Actor = CarlaActor->GetActor();
173  Model = Actor->GetClass();
174  ATrafficSignBase* TrafficSign = Cast<ATrafficSignBase>(Actor);
175  USignComponent* TrafficSignComponent =
176  Cast<USignComponent>(TrafficSign->FindComponentByClass<USignComponent>());
177  if (TrafficSignComponent)
178  {
179  SignModel = TrafficSignComponent->GetClass();
180  SignId = TrafficSignComponent->GetSignId();
181  }
182 }
183 
185 {
186  AActor* Actor = CarlaActor->GetActor();
187  if (SignId.Len())
188  {
189  USignComponent* SignComponent =
190  NewObject<USignComponent>(Actor, SignModel);
191  SignComponent->SetSignId(SignId);
192  SignComponent->RegisterComponent();
193  SignComponent->AttachToComponent(
194  Actor->GetRootComponent(),
195  FAttachmentTransformRules::KeepRelativeTransform);
196  ACarlaGameModeBase *GameMode = UCarlaStatics::GetGameMode(CarlaEpisode->GetWorld());
197  SignComponent->InitializeSign(GameMode->GetMap().get());
198  }
199 }
200 
202 {
203  FTransform SpawnTransform = GetLocalTransform(CarlaEpisode);
204  FActorSpawnParameters SpawnParams;
205  SpawnParams.SpawnCollisionHandlingOverride =
206  ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
207  return CarlaEpisode->GetWorld()->SpawnActor<ATrafficLightBase>(
208  Model,
209  SpawnTransform,
210  SpawnParams);
211 }
212 
214 {
215  FActorData::RecordActorData(CarlaActor, CarlaEpisode);
216  AActor* Actor = CarlaActor->GetActor();
217  Model = Actor->GetClass();
218  ATrafficLightBase* TrafficLight = Cast<ATrafficLightBase>(Actor);
219  UTrafficLightComponent* Component = TrafficLight->GetTrafficLightComponent();
220  SignId = Component->GetSignId();
221  Controller = Component->GetController();
222  Controller->RemoveTrafficLight(Component);
223  Controller->AddCarlaActorTrafficLight(CarlaActor);
224  LightState = TrafficLight->GetTrafficLightState();
225  PoleIndex = TrafficLight->GetPoleIndex();
226 }
227 
229 {
230  AActor* Actor = CarlaActor->GetActor();
231  ATrafficLightBase* TrafficLight = Cast<ATrafficLightBase>(Actor);
232  UTrafficLightComponent* Component = TrafficLight->GetTrafficLightComponent();
233  Component->SetSignId(SignId);
234  Controller->RemoveCarlaActorTrafficLight(CarlaActor);
235  Controller->AddTrafficLight(Component);
236  ACarlaGameModeBase *GameMode = UCarlaStatics::GetGameMode(CarlaEpisode->GetWorld());
237  Component->InitializeSign(GameMode->GetMap().get());
238  Component->SetLightState(Controller->GetCurrentState().State);
239  TrafficLight->SetPoleIndex(PoleIndex);
240  TrafficLight->SetTrafficLightState(LightState);
241 }
242 
244 {
245  FActorData::RecordActorData(CarlaActor, CarlaEpisode);
246  AActor* Actor = CarlaActor->GetActor();
247  ASensor* Sensor = Cast<ASensor>(Actor);
248  Stream = Sensor->MoveDataStream();
249 }
250 
252 {
253  FActorData::RestoreActorData(CarlaActor, CarlaEpisode);
254  AActor* Actor = CarlaActor->GetActor();
255  ASensor* Sensor = Cast<ASensor>(Actor);
256  Sensor->SetDataStream(std::move(Stream));
257 }
void ApplyVehicleControl(const FVehicleControl &Control, EVehicleInputPriority Priority)
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:169
std::string SignId
Definition: RoadTypes.h:25
bool bSimulatePhysics
Definition: ActorData.h:39
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:228
UTrafficLightComponent * GetTrafficLightComponent()
FVehiclePhysicsControl GetVehiclePhysicsControl() const
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode)
Definition: ActorData.cpp:30
void SetSimulatePhysics(bool enabled)
AActor * GetActor()
Definition: CarlaActor.h:90
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info)
Definition: ActorData.cpp:22
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:184
ETrafficLightState GetTrafficLightState() const
FVector Velocity
Definition: ActorData.h:35
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info) override
Definition: ActorData.cpp:157
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:213
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:34
FVector Scale
Definition: ActorData.h:33
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:37
FActorDescription Description
Definition: ActorInfo.h:26
FIntVector GetCurrentMapOrigin() const
Definition: CarlaEpisode.h:288
FTransform GetLocalTransform(UCarlaEpisode *CarlaEpisode) const
Definition: ActorData.cpp:90
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:251
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:114
void SetVehicleLightState(const FVehicleLightState &LightState)
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:133
virtual AActor * RespawnActor(UCarlaEpisode *CarlaEpisode, const FActorInfo &Info) override
Definition: ActorData.cpp:201
virtual void RestoreActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:145
A view over an actor and its properties.
Definition: ActorInfo.h:22
Base class for the CARLA Game Mode.
FQuat Rotation
Definition: ActorData.h:31
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:243
AActor * ReSpawnActorWithInfo(const FTransform &Transform, FActorDescription thisActorDescription)
Spawns an actor based on ActorDescription at Transform.
Definition: CarlaEpisode.h:184
virtual void RecordActorData(FCarlaActor *CarlaActor, UCarlaEpisode *CarlaEpisode) override
Definition: ActorData.cpp:96
Base class for CARLA wheeled vehicles.
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:205
A view over an actor and its properties.
Definition: CarlaActor.h:23
FVehicleLightState GetVehicleLightState() const
FDVector Location
Definition: ActorData.h:29