CARLA
CarlaWheeledVehicle.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 // Copyright (c) 2019 Intel Corporation
4 //
5 // This work is licensed under the terms of the MIT license.
6 // For a copy, see <https://opensource.org/licenses/MIT>.
7 
8 #include "Components/BoxComponent.h"
9 #include "Engine/CollisionProfile.h"
11 #include "Rendering/SkeletalMeshRenderData.h"
12 #include "UObject/UObjectGlobals.h"
13 
14 #include "PhysXPublic.h"
15 #include "PhysXVehicleManager.h"
16 #include "TireConfig.h"
17 #include "VehicleWheel.h"
18 
19 #include "Carla.h"
20 #include "Carla/Game/CarlaHUD.h"
24 #include "Carla/Util/EmptyActor.h"
27 
28 // =============================================================================
29 // -- Constructor and destructor -----------------------------------------------
30 // =============================================================================
31 
32 ACarlaWheeledVehicle::ACarlaWheeledVehicle(const FObjectInitializer& ObjectInitializer) :
33  Super(ObjectInitializer)
34 {
35  VehicleBounds = CreateDefaultSubobject<UBoxComponent>(TEXT("VehicleBounds"));
36  VehicleBounds->SetupAttachment(RootComponent);
37  VehicleBounds->SetHiddenInGame(true);
38  VehicleBounds->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
39 
40  VelocityControl = CreateDefaultSubobject<UVehicleVelocityControl>(TEXT("VelocityControl"));
41  VelocityControl->Deactivate();
42 
43  GetVehicleMovementComponent()->bReverseAsBrake = false;
44 
45  BaseMovementComponent = CreateDefaultSubobject<UBaseCarlaMovementComponent>(TEXT("BaseMovementComponent"));
46 
47 }
48 
50 
51 void ACarlaWheeledVehicle::SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W,
53 
54  #ifdef WHEEL_SWEEP_ENABLED
55  const bool IsBike = IsTwoWheeledVehicle();
56 
57  if (IsBike)
58  return;
59 
60  const bool IsEqual = Vehicle4W->UseSweepWheelCollision == PhysicsControl.UseSweepWheelCollision;
61 
62  if (IsEqual)
63  return;
64 
65  Vehicle4W->UseSweepWheelCollision = PhysicsControl.UseSweepWheelCollision;
66 
67  #else
68 
69  if (PhysicsControl.UseSweepWheelCollision)
70  UE_LOG(LogCarla, Warning, TEXT("Error: Sweep for wheel collision is not available. \
71  Make sure you have installed the required patch.") );
72 
73  #endif
74 
75 }
76 
78 {
79  Super::BeginPlay();
80 
82 
83  // Get constraint components and their initial transforms
84  FTransform ActorInverseTransform = GetActorTransform().Inverse();
85  ConstraintsComponents.Empty();
87  ConstraintDoor.Empty();
88  for (FName& ComponentName : ConstraintComponentNames)
89  {
90  UPhysicsConstraintComponent* ConstraintComponent =
91  Cast<UPhysicsConstraintComponent>(GetDefaultSubobjectByName(ComponentName));
92  if (ConstraintComponent)
93  {
94  UPrimitiveComponent* DoorComponent = Cast<UPrimitiveComponent>(
95  GetDefaultSubobjectByName(ConstraintComponent->ComponentName1.ComponentName));
96  if(DoorComponent)
97  {
98  UE_LOG(LogCarla, Warning, TEXT("Door name: %s"), *(DoorComponent->GetName()));
99  FTransform ComponentWorldTransform = DoorComponent->GetComponentTransform();
100  FTransform RelativeTransform = ComponentWorldTransform * ActorInverseTransform;
101  DoorComponentsTransform.Add(DoorComponent, RelativeTransform);
102  ConstraintDoor.Add(ConstraintComponent, DoorComponent);
103  ConstraintsComponents.Add(ConstraintComponent);
104  ConstraintComponent->TermComponentConstraint();
105  }
106  else
107  {
108  UE_LOG(LogCarla, Error, TEXT("Missing component for constraint: %s"), *(ConstraintComponent->GetName()));
109  }
110  }
111  }
113 
114  // get collision disable constraints (used to prevent doors from colliding with each other)
116  TArray<UPhysicsConstraintComponent*> Constraints;
117  GetComponents(Constraints);
118  for (UPhysicsConstraintComponent* Constraint : Constraints)
119  {
120  if (!ConstraintsComponents.Contains(Constraint))
121  {
122  UPrimitiveComponent* CollisionDisabledComponent1 = Cast<UPrimitiveComponent>(
123  GetDefaultSubobjectByName(Constraint->ComponentName1.ComponentName));
124  UPrimitiveComponent* CollisionDisabledComponent2 = Cast<UPrimitiveComponent>(
125  GetDefaultSubobjectByName(Constraint->ComponentName2.ComponentName));
126  if (CollisionDisabledComponent1)
127  {
128  CollisionDisableConstraints.Add(CollisionDisabledComponent1, Constraint);
129  }
130  if (CollisionDisabledComponent2)
131  {
132  CollisionDisableConstraints.Add(CollisionDisabledComponent2, Constraint);
133  }
134  }
135  }
136 
137  float FrictionScale = 3.5f;
138 
139  UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
140  GetVehicleMovementComponent());
141  check(Vehicle4W != nullptr);
142 
143  // Setup Tire Configs with default value. This is needed to avoid getting
144  // friction values of previously created TireConfigs for the same vehicle
145  // blueprint.
146  TArray<float> OriginalFrictions;
147  OriginalFrictions.Init(FrictionScale, Vehicle4W->Wheels.Num());
148  SetWheelsFrictionScale(OriginalFrictions);
149 
150  // Check if it overlaps with a Friction trigger, if so, update the friction
151  // scale.
152  TArray<AActor *> OverlapActors;
153  GetOverlappingActors(OverlapActors, AFrictionTrigger::StaticClass());
154  for (const auto &Actor : OverlapActors)
155  {
156  AFrictionTrigger *FrictionTrigger = Cast<AFrictionTrigger>(Actor);
157  if (FrictionTrigger)
158  {
159  FrictionScale = FrictionTrigger->Friction;
160  }
161  }
162 
163  // Set the friction scale to Wheel CDO and update wheel setups
164  TArray<FWheelSetup> NewWheelSetups = Vehicle4W->WheelSetups;
165 
166  for (const auto &WheelSetup : NewWheelSetups)
167  {
168  UVehicleWheel *Wheel = WheelSetup.WheelClass.GetDefaultObject();
169  check(Wheel != nullptr);
170  }
171 
172  Vehicle4W->WheelSetups = NewWheelSetups;
173 
175 }
176 
178 {
180 
181  const FTransform& CompToWorldTransform = RootComponent->GetComponentTransform();
182  const FRotator Rotation = CompToWorldTransform.GetRotation().Rotator();
183  const FVector Translation = CompToWorldTransform.GetLocation();
184  const FVector Scale = CompToWorldTransform.GetScale3D();
185 
186  // Invert BB origin to local space
187  BoundingBox.Origin -= Translation;
188  BoundingBox.Origin = Rotation.UnrotateVector(BoundingBox.Origin);
189  BoundingBox.Origin /= Scale;
190 
191  // Prepare Box Collisions
192  FTransform Transform;
193  Transform.SetTranslation(BoundingBox.Origin);
194  VehicleBounds->SetRelativeTransform(Transform);
195  VehicleBounds->SetBoxExtent(BoundingBox.Extent);
196 }
197 
198 // =============================================================================
199 // -- Get functions ------------------------------------------------------------
200 // =============================================================================
201 
203 {
205 }
206 
208 {
209  return GetVehicleTransform().GetRotation().GetForwardVector();
210 }
211 
213 {
215 }
216 
218 {
219  return VehicleBounds->GetRelativeTransform();
220 }
221 
223 {
224  return VehicleBounds->GetScaledBoxExtent();
225 }
226 
228 {
229  const auto &Wheels = GetVehicleMovementComponent()->Wheels;
230  check(Wheels.Num() > 0);
231  const auto *FrontWheel = Wheels[0];
232  check(FrontWheel != nullptr);
233  return FrontWheel->SteerAngle;
234 }
235 
236 // =============================================================================
237 // -- Set functions ------------------------------------------------------------
238 // =============================================================================
239 
241 {
243  InputControl.Control.bReverse = InputControl.Control.Gear < 0;
246 }
247 
249 {
251  Control.Throttle = Value;
252  ApplyVehicleControl(Control, EVehicleInputPriority::User);
253 }
254 
256 {
258  Control.Steer = Value;
259  ApplyVehicleControl(Control, EVehicleInputPriority::User);
260 }
261 
262 void ACarlaWheeledVehicle::SetBrakeInput(const float Value)
263 {
265  Control.Brake = Value;
266  ApplyVehicleControl(Control, EVehicleInputPriority::User);
267 }
268 
269 void ACarlaWheeledVehicle::SetReverse(const bool Value)
270 {
272  Control.bReverse = Value;
273  ApplyVehicleControl(Control, EVehicleInputPriority::User);
274 }
275 
277 {
279  Control.bHandBrake = Value;
280  ApplyVehicleControl(Control, EVehicleInputPriority::User);
281 }
282 
284 {
285  UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
286  GetVehicleMovement());
287  check(Vehicle4W != nullptr);
288 
289  TArray<float> WheelsFrictionScale;
290  for (auto &Wheel : Vehicle4W->Wheels)
291  {
292  WheelsFrictionScale.Add(Wheel->TireConfig->GetFrictionScale());
293  }
294  return WheelsFrictionScale;
295 }
296 
297 void ACarlaWheeledVehicle::SetWheelsFrictionScale(TArray<float> &WheelsFrictionScale)
298 {
299  UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
300  GetVehicleMovement());
301  check(Vehicle4W != nullptr);
302  check(Vehicle4W->Wheels.Num() == WheelsFrictionScale.Num());
303 
304  for (int32 i = 0; i < Vehicle4W->Wheels.Num(); ++i)
305  {
306  Vehicle4W->Wheels[i]->TireConfig->SetFrictionScale(WheelsFrictionScale[i]);
307  }
308 }
309 
311 {
312  UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
313  GetVehicleMovement());
314  check(Vehicle4W != nullptr);
315 
317 
318  // Engine Setup
319  PhysicsControl.TorqueCurve = Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData;
320  PhysicsControl.MaxRPM = Vehicle4W->EngineSetup.MaxRPM;
321  PhysicsControl.MOI = Vehicle4W->EngineSetup.MOI;
322  PhysicsControl.DampingRateFullThrottle = Vehicle4W->EngineSetup.DampingRateFullThrottle;
323  PhysicsControl.DampingRateZeroThrottleClutchEngaged =
324  Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchEngaged;
326  Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchDisengaged;
327 
328  // Transmission Setup
329  PhysicsControl.bUseGearAutoBox = Vehicle4W->TransmissionSetup.bUseGearAutoBox;
330  PhysicsControl.GearSwitchTime = Vehicle4W->TransmissionSetup.GearSwitchTime;
331  PhysicsControl.ClutchStrength = Vehicle4W->TransmissionSetup.ClutchStrength;
332  PhysicsControl.FinalRatio = Vehicle4W->TransmissionSetup.FinalRatio;
333 
334  TArray<FGearPhysicsControl> ForwardGears;
335 
336  for (const auto &Gear : Vehicle4W->TransmissionSetup.ForwardGears)
337  {
338  FGearPhysicsControl GearPhysicsControl;
339 
340  GearPhysicsControl.Ratio = Gear.Ratio;
341  GearPhysicsControl.UpRatio = Gear.UpRatio;
342  GearPhysicsControl.DownRatio = Gear.DownRatio;
343 
344  ForwardGears.Add(GearPhysicsControl);
345  }
346 
347  PhysicsControl.ForwardGears = ForwardGears;
348 
349  // Vehicle Setup
350  PhysicsControl.Mass = Vehicle4W->Mass;
351  PhysicsControl.DragCoefficient = Vehicle4W->DragCoefficient;
352 
353  // Center of mass offset (Center of mass is always zero vector in local
354  // position)
355  UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
356  check(UpdatedPrimitive != nullptr);
357 
358  PhysicsControl.CenterOfMass = UpdatedPrimitive->BodyInstance.COMNudge;
359 
360  // Transmission Setup
361  PhysicsControl.SteeringCurve = Vehicle4W->SteeringCurve.EditorCurveData;
362 
363  // Wheels Setup
364  TArray<FWheelPhysicsControl> Wheels;
365 
366  for (int32 i = 0; i < Vehicle4W->WheelSetups.Num(); ++i)
367  {
368  FWheelPhysicsControl PhysicsWheel;
369 
370  PxVehicleWheelData PWheelData = Vehicle4W->PVehicle->mWheelsSimData.getWheelData(i);
371  PhysicsWheel.DampingRate = Cm2ToM2(PWheelData.mDampingRate);
372  PhysicsWheel.MaxSteerAngle = FMath::RadiansToDegrees(PWheelData.mMaxSteer);
373  PhysicsWheel.Radius = PWheelData.mRadius;
374  PhysicsWheel.MaxBrakeTorque = Cm2ToM2(PWheelData.mMaxBrakeTorque);
375  PhysicsWheel.MaxHandBrakeTorque = Cm2ToM2(PWheelData.mMaxHandBrakeTorque);
376 
377  PxVehicleTireData PTireData = Vehicle4W->PVehicle->mWheelsSimData.getTireData(i);
378  PhysicsWheel.LatStiffMaxLoad = PTireData.mLatStiffX;
379  PhysicsWheel.LatStiffValue = PTireData.mLatStiffY;
380  PhysicsWheel.LongStiffValue = PTireData.mLongitudinalStiffnessPerUnitGravity;
381 
382  PhysicsWheel.TireFriction = Vehicle4W->Wheels[i]->TireConfig->GetFrictionScale();
383  PhysicsWheel.Position = Vehicle4W->Wheels[i]->Location;
384 
385  Wheels.Add(PhysicsWheel);
386  }
387 
388  PhysicsControl.Wheels = Wheels;
389 
390  return PhysicsControl;
391 }
392 
394 {
395  return InputControl.LightState;
396 }
397 
399 {
401 }
402 
404 {
406 
407  UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
408  GetVehicleMovement());
409  check(Vehicle4W != nullptr);
410 
411  // Engine Setup
412  Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData = PhysicsControl.TorqueCurve;
413  Vehicle4W->EngineSetup.MaxRPM = PhysicsControl.MaxRPM;
414 
415  Vehicle4W->EngineSetup.MOI = PhysicsControl.MOI;
416 
417  Vehicle4W->EngineSetup.DampingRateFullThrottle = PhysicsControl.DampingRateFullThrottle;
418  Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchEngaged =
420  Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchDisengaged =
422 
423  // Transmission Setup
424  Vehicle4W->TransmissionSetup.bUseGearAutoBox = PhysicsControl.bUseGearAutoBox;
425  Vehicle4W->TransmissionSetup.GearSwitchTime = PhysicsControl.GearSwitchTime;
426  Vehicle4W->TransmissionSetup.ClutchStrength = PhysicsControl.ClutchStrength;
427  Vehicle4W->TransmissionSetup.FinalRatio = PhysicsControl.FinalRatio;
428 
429  TArray<FVehicleGearData> ForwardGears;
430 
431  for (const auto &Gear : PhysicsControl.ForwardGears)
432  {
433  FVehicleGearData GearData;
434 
435  GearData.Ratio = Gear.Ratio;
436  GearData.UpRatio = Gear.UpRatio;
437  GearData.DownRatio = Gear.DownRatio;
438 
439  ForwardGears.Add(GearData);
440  }
441 
442  Vehicle4W->TransmissionSetup.ForwardGears = ForwardGears;
443 
444  // Vehicle Setup
445  Vehicle4W->Mass = PhysicsControl.Mass;
446  Vehicle4W->DragCoefficient = PhysicsControl.DragCoefficient;
447 
448  // Center of mass
449  UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
450  check(UpdatedPrimitive != nullptr);
451 
452  UpdatedPrimitive->BodyInstance.COMNudge = PhysicsControl.CenterOfMass;
453 
454  // Transmission Setup
455  Vehicle4W->SteeringCurve.EditorCurveData = PhysicsControl.SteeringCurve;
456 
457  // Wheels Setup
458  const int PhysicsWheelsNum = PhysicsControl.Wheels.Num();
459  if (PhysicsWheelsNum != 4)
460  {
461  UE_LOG(LogCarla, Error, TEXT("Number of WheelPhysicsControl is not 4."));
462  return;
463  }
464 
465  // Change, if required, the collision mode for wheels
466  SetWheelCollision(Vehicle4W, PhysicsControl);
467 
468  TArray<FWheelSetup> NewWheelSetups = Vehicle4W->WheelSetups;
469 
470  for (int32 i = 0; i < PhysicsWheelsNum; ++i)
471  {
472  UVehicleWheel *Wheel = NewWheelSetups[i].WheelClass.GetDefaultObject();
473  check(Wheel != nullptr);
474 
475  // Assigning new tire config
476  Wheel->TireConfig = DuplicateObject<UTireConfig>(Wheel->TireConfig, nullptr);
477 
478  // Setting a new value to friction
479  Wheel->TireConfig->SetFrictionScale(PhysicsControl.Wheels[i].TireFriction);
480  }
481 
482  Vehicle4W->WheelSetups = NewWheelSetups;
483 
484  // Recreate Physics State for vehicle setup
485  GetWorld()->GetPhysicsScene()->GetPxScene()->lockWrite();
486  Vehicle4W->RecreatePhysicsState();
487  GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
488 
489  for (int32 i = 0; i < PhysicsWheelsNum; ++i)
490  {
491  PxVehicleWheelData PWheelData = Vehicle4W->PVehicle->mWheelsSimData.getWheelData(i);
492 
493  PWheelData.mRadius = PhysicsControl.Wheels[i].Radius;
494  PWheelData.mMaxSteer = FMath::DegreesToRadians(PhysicsControl.Wheels[i].MaxSteerAngle);
495  PWheelData.mDampingRate = M2ToCm2(PhysicsControl.Wheels[i].DampingRate);
496  PWheelData.mMaxBrakeTorque = M2ToCm2(PhysicsControl.Wheels[i].MaxBrakeTorque);
497  PWheelData.mMaxHandBrakeTorque = M2ToCm2(PhysicsControl.Wheels[i].MaxHandBrakeTorque);
498  Vehicle4W->PVehicle->mWheelsSimData.setWheelData(i, PWheelData);
499 
500  PxVehicleTireData PTireData = Vehicle4W->PVehicle->mWheelsSimData.getTireData(i);
501  PTireData.mLatStiffX = PhysicsControl.Wheels[i].LatStiffMaxLoad;
502  PTireData.mLatStiffY = PhysicsControl.Wheels[i].LatStiffValue;
503  PTireData.mLongitudinalStiffnessPerUnitGravity = PhysicsControl.Wheels[i].LongStiffValue;
504  Vehicle4W->PVehicle->mWheelsSimData.setTireData(i, PTireData);
505  }
506 
508 
509  auto * Recorder = UCarlaStatics::GetRecorder(GetWorld());
510  if (Recorder && Recorder->IsEnabled())
511  {
512  Recorder->AddPhysicsControl(*this);
513  }
514 }
515 
517 {
518  VelocityControl->Activate(Velocity);
519 }
520 
522 {
523  VelocityControl->Deactivate();
524 }
525 
527 {
528  if (GetWorld()->GetFirstPlayerController())
529  {
530  ACarlaHUD* hud = Cast<ACarlaHUD>(GetWorld()->GetFirstPlayerController()->GetHUD());
531  if (hud) {
532 
533  // Set/Unset the car movement component in HUD to show the temetry
534  if (Enabled) {
535  hud->AddDebugVehicleForTelemetry(GetVehicleMovementComponent());
536  }
537  else{
538  if (hud->DebugVehicle == GetVehicleMovementComponent()) {
539  hud->AddDebugVehicleForTelemetry(nullptr);
540  GetVehicleMovementComponent()->StopTelemetry();
541  }
542  }
543 
544  }
545  else {
546  UE_LOG(LogCarla, Warning, TEXT("ACarlaWheeledVehicle::ShowDebugTelemetry:: Cannot find HUD for debug info"));
547  }
548  }
549 }
550 
552 {
553  InputControl.LightState = LightState;
554  RefreshLightState(LightState);
555 }
556 
558 {
560  {
561  BaseMovementComponent->DestroyComponent();
562  }
563  BaseMovementComponent = MovementComponent;
564 }
565 
567 
568  if (bPhysicsEnabled == false)
569  {
570  check((uint8)WheelLocation >= 0)
571  check((uint8)WheelLocation < 4)
572  UVehicleAnimInstance *VehicleAnim = Cast<UVehicleAnimInstance>(GetMesh()->GetAnimInstance());
573  check(VehicleAnim != nullptr)
574  VehicleAnim->SetWheelRotYaw((uint8)WheelLocation, AngleInDeg);
575  }
576  else
577  {
578  UE_LOG(LogTemp, Warning, TEXT("Cannot set wheel steer direction. Physics are enabled."))
579  }
580 }
581 
583 
584  check((uint8)WheelLocation >= 0)
585  check((uint8)WheelLocation < 4)
586  UVehicleAnimInstance *VehicleAnim = Cast<UVehicleAnimInstance>(GetMesh()->GetAnimInstance());
587  check(VehicleAnim != nullptr)
588  check(VehicleAnim->GetWheeledVehicleMovementComponent() != nullptr)
589 
590  if (bPhysicsEnabled == true)
591  {
592  return VehicleAnim->GetWheeledVehicleMovementComponent()->Wheels[(uint8)WheelLocation]->GetSteerAngle();
593  }
594  else
595  {
596  return VehicleAnim->GetWheelRotAngle((uint8)WheelLocation);
597  }
598 }
599 
601  if(!GetCarlaMovementComponent<UDefaultMovementComponent>())
602  {
603  return;
604  }
605 
606  UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
607  GetVehicleMovement());
608  check(Vehicle4W != nullptr);
609 
610  if(bPhysicsEnabled == enabled)
611  return;
612 
613  SetActorEnableCollision(true);
614  auto RootComponent = Cast<UPrimitiveComponent>(GetRootComponent());
615  RootComponent->SetSimulatePhysics(enabled);
616  RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
617 
618  UVehicleAnimInstance *VehicleAnim = Cast<UVehicleAnimInstance>(GetMesh()->GetAnimInstance());
619  check(VehicleAnim != nullptr)
620 
621  GetWorld()->GetPhysicsScene()->GetPxScene()->lockWrite();
622  if (enabled)
623  {
624  Vehicle4W->RecreatePhysicsState();
625  VehicleAnim->ResetWheelCustomRotations();
626  }
627  else
628  {
629  Vehicle4W->DestroyPhysicsState();
630  }
631 
632  GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
633 
634  bPhysicsEnabled = enabled;
635 
637 
638 }
639 
641 {
642  for (int i = 0; i < ConstraintsComponents.Num(); i++)
643  {
645  }
646  for (int i = 0; i < ConstraintsComponents.Num(); i++)
647  {
649  }
650 }
651 
653 {
655 }
656 
657 void ACarlaWheeledVehicle::EndPlay(const EEndPlayReason::Type EndPlayReason)
658 {
659  ShowDebugTelemetry(false);
660 }
661 
663  if (int(DoorIdx) >= ConstraintsComponents.Num() && DoorIdx != EVehicleDoor::All) {
664  UE_LOG(LogTemp, Warning, TEXT("This door is not configured for this car."));
665  return;
666  }
667 
668  if (DoorIdx == EVehicleDoor::All) {
669  for (int i = 0; i < ConstraintsComponents.Num(); i++)
670  {
672  }
673  return;
674  }
675 
676  OpenDoorPhys(DoorIdx);
677 }
678 
680  if (int(DoorIdx) >= ConstraintsComponents.Num() && DoorIdx != EVehicleDoor::All) {
681  UE_LOG(LogTemp, Warning, TEXT("This door is not configured for this car."));
682  return;
683  }
684 
685  if (DoorIdx == EVehicleDoor::All) {
686  for (int i = 0; i < ConstraintsComponents.Num(); i++)
687  {
689  }
690  return;
691  }
692 
693  CloseDoorPhys(DoorIdx);
694 }
695 
697 {
698  UPhysicsConstraintComponent* Constraint = ConstraintsComponents[static_cast<int>(DoorIdx)];
699  UPrimitiveComponent* DoorComponent = ConstraintDoor[Constraint];
700  DoorComponent->DetachFromComponent(
701  FDetachmentTransformRules(EDetachmentRule::KeepWorld, false));
702  FTransform DoorInitialTransform =
703  DoorComponentsTransform[DoorComponent] * GetActorTransform();
704  DoorComponent->SetWorldTransform(DoorInitialTransform);
705  DoorComponent->SetSimulatePhysics(true);
706  DoorComponent->SetCollisionProfileName(TEXT("BlockAll"));
707  float AngleLimit = Constraint->ConstraintInstance.GetAngularSwing1Limit();
708  FRotator AngularRotationOffset = Constraint->ConstraintInstance.AngularRotationOffset;
709 
710  if (Constraint->ConstraintInstance.AngularRotationOffset.Yaw < 0.0f)
711  {
712  AngleLimit = -AngleLimit;
713  }
714  Constraint->SetAngularOrientationTarget(FRotator(0, AngleLimit, 0));
715  Constraint->SetAngularDriveParams(DoorOpenStrength, 1.0, 0.0);
716 
717  Constraint->InitComponentConstraint();
718 
719  UPhysicsConstraintComponent** CollisionDisable =
720  CollisionDisableConstraints.Find(DoorComponent);
721  if (CollisionDisable)
722  {
723  (*CollisionDisable)->InitComponentConstraint();
724  }
725 }
726 
728 {
729  UPhysicsConstraintComponent* Constraint = ConstraintsComponents[static_cast<int>(DoorIdx)];
730  UPrimitiveComponent* DoorComponent = ConstraintDoor[Constraint];
731  FTransform DoorInitialTransform =
732  DoorComponentsTransform[DoorComponent] * GetActorTransform();
733  DoorComponent->SetSimulatePhysics(false);
734  DoorComponent->SetCollisionProfileName(TEXT("NoCollision"));
735  DoorComponent->SetWorldTransform(DoorInitialTransform);
736  DoorComponent->AttachToComponent(
737  GetMesh(), FAttachmentTransformRules(EAttachmentRule::KeepWorld, true));
738 }
void ApplyVehicleControl(const FVehicleControl &Control, EVehicleInputPriority Priority)
virtual void ProcessControl(FVehicleControl &Control)
void SetThrottleInput(float Value)
UBaseCarlaMovementComponent * BaseMovementComponent
FTransform GetVehicleBoundingBoxTransform() const
Transform of the vehicle&#39;s bounding box relative to the vehicle.
float GetVehicleForwardSpeed() const
Forward speed in cm/s. Might be negative if goes backwards.
struct ACarlaWheeledVehicle::@0 InputControl
FVehiclePhysicsControl GetVehiclePhysicsControl() const
TArray< FName > ConstraintComponentNames
virtual FVector GetVelocity() const override
TArray< UPhysicsConstraintComponent * > ConstraintsComponents
FVehicleLightState LightState
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason)
void SetSimulatePhysics(bool enabled)
void OpenDoor(const EVehicleDoor DoorIdx)
FVector GetVehicleBoundingBoxExtent() const
Extent of the vehicle&#39;s bounding box.
void ShowDebugTelemetry(bool Enabled)
float GetMaximumSteerAngle() const
Get the maximum angle at which the front wheel can steer.
TMap< UPhysicsConstraintComponent *, UPrimitiveComponent * > ConstraintDoor
void AddDebugVehicleForTelemetry(UWheeledVehicleMovementComponent *Veh)
Definition: CarlaHUD.h:54
static FBoundingBox GetVehicleBoundingBox(const ACarlaWheeledVehicle *Vehicle, uint8 InTagQueried=0xFF)
void CloseDoor(const EVehicleDoor DoorIdx)
ACarlaWheeledVehicle(const FObjectInitializer &ObjectInitializer)
void SetWheelSteerDirection(EVehicleWheelLocation WheelLocation, float AngleInDeg)
Set the rotation of the car wheels indicated by the user 0 = FL_VehicleWheel, 1 = FR_VehicleWheel...
TArray< float > GetWheelsFrictionScale()
FVector GetVehicleOrientation() const
Orientation vector of the vehicle, pointing forward.
void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl)
void ActivateVelocityControl(const FVector &Velocity)
carla::SharedPtr< cc::Actor > Actor
void SetSteeringInput(float Value)
virtual void BeginPlay() override
void SetHandbrakeInput(bool Value)
float GetWheelSteerAngle(EVehicleWheelLocation WheelLocation)
FVehicleControl LastAppliedControl
void RefreshLightState(const FVehicleLightState &VehicleLightState)
TMap< UPrimitiveComponent *, UPhysicsConstraintComponent * > CollisionDisableConstraints
void SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W, const FVehiclePhysicsControl &PhysicsControl)
UBoxComponent * VehicleBounds
EVehicleWheelLocation
geom::Rotation Rotation
Definition: rpc/Transform.h:14
void SetVehicleLightState(const FVehicleLightState &LightState)
TMap< UPrimitiveComponent *, FTransform > DoorComponentsTransform
int32 GetVehicleCurrentGear() const
Active gear of the vehicle.
UVehicleVelocityControl * VelocityControl
void SetBrakeInput(float Value)
void CloseDoorPhys(const EVehicleDoor DoorIdx)
virtual float GetVehicleForwardSpeed() const
FVehiclePhysicsControl LastPhysicsControl
FVector Origin
Origin of the bounding box relative to its owner.
void OpenDoorPhys(const EVehicleDoor DoorIdx)
EVehicleDoor
Type of door to open/close.
static void CreateDefaultMovementComponent(ACarlaWheeledVehicle *Vehicle)
void SetWheelsFrictionScale(TArray< float > &WheelsFrictionScale)
Class to draw on HUD.
Definition: CarlaHUD.h:39
FVector Extent
Radii extent of the bounding box.
UWheeledVehicleMovementComponent * DebugVehicle
Definition: CarlaHUD.h:53
geom::Transform Transform
Definition: rpc/Transform.h:16
void SetCarlaMovementComponent(UBaseCarlaMovementComponent *MoementComponent)
FVehicleLightState GetVehicleLightState() const
FTransform GetVehicleTransform() const
Transform of the vehicle.
static ACarlaRecorder * GetRecorder(const UObject *WorldContextObject)
Definition: CarlaStatics.h:80