8 #include "Components/BoxComponent.h" 9 #include "Engine/CollisionProfile.h" 11 #include "Rendering/SkeletalMeshRenderData.h" 12 #include "UObject/UObjectGlobals.h" 14 #include "PhysXPublic.h" 15 #include "PhysXVehicleManager.h" 16 #include "TireConfig.h" 17 #include "VehicleWheel.h" 33 Super(ObjectInitializer)
35 VehicleBounds = CreateDefaultSubobject<UBoxComponent>(TEXT(
"VehicleBounds"));
38 VehicleBounds->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName);
40 VelocityControl = CreateDefaultSubobject<UVehicleVelocityControl>(TEXT(
"VelocityControl"));
43 GetVehicleMovementComponent()->bReverseAsBrake =
false;
45 BaseMovementComponent = CreateDefaultSubobject<UBaseCarlaMovementComponent>(TEXT(
"BaseMovementComponent"));
54 #ifdef WHEEL_SWEEP_ENABLED 70 UE_LOG(LogCarla, Warning, TEXT(
"Error: Sweep for wheel collision is not available. \ 71 Make sure you have installed the required patch.") );
84 FTransform ActorInverseTransform = GetActorTransform().Inverse();
90 UPhysicsConstraintComponent* ConstraintComponent =
91 Cast<UPhysicsConstraintComponent>(GetDefaultSubobjectByName(ComponentName));
92 if (ConstraintComponent)
95 GetDefaultSubobjectByName(ConstraintComponent->ComponentName1.ComponentName));
98 UE_LOG(LogCarla, Warning, TEXT(
"Door name: %s"), *(DoorComponent->GetName()));
99 FTransform ComponentWorldTransform = DoorComponent->GetComponentTransform();
100 FTransform RelativeTransform = ComponentWorldTransform * ActorInverseTransform;
104 ConstraintComponent->TermComponentConstraint();
108 UE_LOG(LogCarla, Error, TEXT(
"Missing component for constraint: %s"), *(ConstraintComponent->GetName()));
116 TArray<UPhysicsConstraintComponent*> Constraints;
117 GetComponents(Constraints);
118 for (UPhysicsConstraintComponent* Constraint : Constraints)
123 GetDefaultSubobjectByName(Constraint->ComponentName1.ComponentName));
125 GetDefaultSubobjectByName(Constraint->ComponentName2.ComponentName));
126 if (CollisionDisabledComponent1)
130 if (CollisionDisabledComponent2)
137 float FrictionScale = 3.5f;
139 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
140 GetVehicleMovementComponent());
141 check(Vehicle4W !=
nullptr);
146 TArray<float> OriginalFrictions;
147 OriginalFrictions.Init(FrictionScale, Vehicle4W->Wheels.Num());
152 TArray<AActor *> OverlapActors;
153 GetOverlappingActors(OverlapActors, AFrictionTrigger::StaticClass());
154 for (
const auto &
Actor : OverlapActors)
159 FrictionScale = FrictionTrigger->
Friction;
164 TArray<FWheelSetup> NewWheelSetups = Vehicle4W->WheelSetups;
166 for (
const auto &WheelSetup : NewWheelSetups)
168 UVehicleWheel *Wheel = WheelSetup.WheelClass.GetDefaultObject();
169 check(Wheel !=
nullptr);
172 Vehicle4W->WheelSetups = NewWheelSetups;
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();
187 BoundingBox.
Origin -= Translation;
188 BoundingBox.
Origin = Rotation.UnrotateVector(BoundingBox.
Origin);
189 BoundingBox.
Origin /= Scale;
193 Transform.SetTranslation(BoundingBox.
Origin);
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;
258 Control.
Steer = Value;
265 Control.
Brake = Value;
285 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
286 GetVehicleMovement());
287 check(Vehicle4W !=
nullptr);
289 TArray<float> WheelsFrictionScale;
290 for (
auto &Wheel : Vehicle4W->Wheels)
292 WheelsFrictionScale.Add(Wheel->TireConfig->GetFrictionScale());
294 return WheelsFrictionScale;
299 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
300 GetVehicleMovement());
301 check(Vehicle4W !=
nullptr);
302 check(Vehicle4W->Wheels.Num() == WheelsFrictionScale.Num());
304 for (int32 i = 0; i < Vehicle4W->Wheels.Num(); ++i)
306 Vehicle4W->Wheels[i]->TireConfig->SetFrictionScale(WheelsFrictionScale[i]);
312 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
313 GetVehicleMovement());
314 check(Vehicle4W !=
nullptr);
319 PhysicsControl.
TorqueCurve = Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData;
320 PhysicsControl.
MaxRPM = Vehicle4W->EngineSetup.MaxRPM;
321 PhysicsControl.
MOI = Vehicle4W->EngineSetup.MOI;
324 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchEngaged;
326 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchDisengaged;
329 PhysicsControl.
bUseGearAutoBox = Vehicle4W->TransmissionSetup.bUseGearAutoBox;
330 PhysicsControl.
GearSwitchTime = Vehicle4W->TransmissionSetup.GearSwitchTime;
331 PhysicsControl.
ClutchStrength = Vehicle4W->TransmissionSetup.ClutchStrength;
332 PhysicsControl.
FinalRatio = Vehicle4W->TransmissionSetup.FinalRatio;
334 TArray<FGearPhysicsControl> ForwardGears;
336 for (
const auto &Gear : Vehicle4W->TransmissionSetup.ForwardGears)
340 GearPhysicsControl.
Ratio = Gear.Ratio;
341 GearPhysicsControl.
UpRatio = Gear.UpRatio;
342 GearPhysicsControl.
DownRatio = Gear.DownRatio;
344 ForwardGears.Add(GearPhysicsControl);
350 PhysicsControl.
Mass = Vehicle4W->Mass;
355 UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
356 check(UpdatedPrimitive !=
nullptr);
358 PhysicsControl.
CenterOfMass = UpdatedPrimitive->BodyInstance.COMNudge;
361 PhysicsControl.
SteeringCurve = Vehicle4W->SteeringCurve.EditorCurveData;
364 TArray<FWheelPhysicsControl> Wheels;
366 for (int32 i = 0; i < Vehicle4W->WheelSetups.Num(); ++i)
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);
377 PxVehicleTireData PTireData = Vehicle4W->PVehicle->mWheelsSimData.getTireData(i);
380 PhysicsWheel.
LongStiffValue = PTireData.mLongitudinalStiffnessPerUnitGravity;
382 PhysicsWheel.
TireFriction = Vehicle4W->Wheels[i]->TireConfig->GetFrictionScale();
383 PhysicsWheel.
Position = Vehicle4W->Wheels[i]->Location;
385 Wheels.Add(PhysicsWheel);
388 PhysicsControl.
Wheels = Wheels;
407 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
408 GetVehicleMovement());
409 check(Vehicle4W !=
nullptr);
412 Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData = PhysicsControl.
TorqueCurve;
413 Vehicle4W->EngineSetup.MaxRPM = PhysicsControl.
MaxRPM;
415 Vehicle4W->EngineSetup.MOI = PhysicsControl.
MOI;
418 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchEngaged =
420 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchDisengaged =
424 Vehicle4W->TransmissionSetup.bUseGearAutoBox = PhysicsControl.
bUseGearAutoBox;
425 Vehicle4W->TransmissionSetup.GearSwitchTime = PhysicsControl.
GearSwitchTime;
426 Vehicle4W->TransmissionSetup.ClutchStrength = PhysicsControl.
ClutchStrength;
427 Vehicle4W->TransmissionSetup.FinalRatio = PhysicsControl.
FinalRatio;
429 TArray<FVehicleGearData> ForwardGears;
433 FVehicleGearData GearData;
435 GearData.Ratio = Gear.Ratio;
436 GearData.UpRatio = Gear.UpRatio;
437 GearData.DownRatio = Gear.DownRatio;
439 ForwardGears.Add(GearData);
442 Vehicle4W->TransmissionSetup.ForwardGears = ForwardGears;
445 Vehicle4W->Mass = PhysicsControl.
Mass;
449 UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
450 check(UpdatedPrimitive !=
nullptr);
452 UpdatedPrimitive->BodyInstance.COMNudge = PhysicsControl.
CenterOfMass;
455 Vehicle4W->SteeringCurve.EditorCurveData = PhysicsControl.
SteeringCurve;
458 const int PhysicsWheelsNum = PhysicsControl.
Wheels.Num();
459 if (PhysicsWheelsNum != 4)
461 UE_LOG(LogCarla, Error, TEXT(
"Number of WheelPhysicsControl is not 4."));
468 TArray<FWheelSetup> NewWheelSetups = Vehicle4W->WheelSetups;
470 for (int32 i = 0; i < PhysicsWheelsNum; ++i)
472 UVehicleWheel *Wheel = NewWheelSetups[i].WheelClass.GetDefaultObject();
473 check(Wheel !=
nullptr);
476 Wheel->TireConfig = DuplicateObject<UTireConfig>(Wheel->TireConfig,
nullptr);
479 Wheel->TireConfig->SetFrictionScale(PhysicsControl.
Wheels[i].TireFriction);
482 Vehicle4W->WheelSetups = NewWheelSetups;
485 GetWorld()->GetPhysicsScene()->GetPxScene()->lockWrite();
486 Vehicle4W->RecreatePhysicsState();
487 GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
489 for (int32 i = 0; i < PhysicsWheelsNum; ++i)
491 PxVehicleWheelData PWheelData = Vehicle4W->PVehicle->mWheelsSimData.getWheelData(i);
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);
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);
510 if (Recorder && Recorder->IsEnabled())
512 Recorder->AddPhysicsControl(*
this);
528 if (GetWorld()->GetFirstPlayerController())
530 ACarlaHUD* hud = Cast<ACarlaHUD>(GetWorld()->GetFirstPlayerController()->GetHUD());
538 if (hud->
DebugVehicle == GetVehicleMovementComponent()) {
540 GetVehicleMovementComponent()->StopTelemetry();
546 UE_LOG(LogCarla, Warning, TEXT(
"ACarlaWheeledVehicle::ShowDebugTelemetry:: Cannot find HUD for debug info"));
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);
578 UE_LOG(LogTemp, Warning, TEXT(
"Cannot set wheel steer direction. Physics are enabled."))
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)
592 return VehicleAnim->GetWheeledVehicleMovementComponent()->Wheels[(uint8)WheelLocation]->GetSteerAngle();
596 return VehicleAnim->GetWheelRotAngle((uint8)WheelLocation);
601 if(!GetCarlaMovementComponent<UDefaultMovementComponent>())
606 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
607 GetVehicleMovement());
608 check(Vehicle4W !=
nullptr);
613 SetActorEnableCollision(
true);
614 auto RootComponent = Cast<UPrimitiveComponent>(GetRootComponent());
615 RootComponent->SetSimulatePhysics(enabled);
616 RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
618 UVehicleAnimInstance *VehicleAnim = Cast<UVehicleAnimInstance>(GetMesh()->GetAnimInstance());
619 check(VehicleAnim !=
nullptr)
621 GetWorld()->GetPhysicsScene()->GetPxScene()->lockWrite();
624 Vehicle4W->RecreatePhysicsState();
625 VehicleAnim->ResetWheelCustomRotations();
629 Vehicle4W->DestroyPhysicsState();
632 GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
664 UE_LOG(LogTemp, Warning, TEXT(
"This door is not configured for this car."));
681 UE_LOG(LogTemp, Warning, TEXT(
"This door is not configured for this car."));
700 DoorComponent->DetachFromComponent(
701 FDetachmentTransformRules(EDetachmentRule::KeepWorld,
false));
702 FTransform DoorInitialTransform =
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;
710 if (Constraint->ConstraintInstance.AngularRotationOffset.Yaw < 0.0f)
712 AngleLimit = -AngleLimit;
714 Constraint->SetAngularOrientationTarget(FRotator(0, AngleLimit, 0));
717 Constraint->InitComponentConstraint();
719 UPhysicsConstraintComponent** CollisionDisable =
721 if (CollisionDisable)
723 (*CollisionDisable)->InitComponentConstraint();
731 FTransform DoorInitialTransform =
733 DoorComponent->SetSimulatePhysics(
false);
734 DoorComponent->SetCollisionProfileName(TEXT(
"NoCollision"));
735 DoorComponent->SetWorldTransform(DoorInitialTransform);
736 DoorComponent->AttachToComponent(
737 GetMesh(), FAttachmentTransformRules(EAttachmentRule::KeepWorld,
true));
void ApplyVehicleControl(const FVehicleControl &Control, EVehicleInputPriority Priority)
virtual void ProcessControl(FVehicleControl &Control)
void SetReverse(bool Value)
void SetThrottleInput(float Value)
UBaseCarlaMovementComponent * BaseMovementComponent
void RestoreVehiclePhysicsControl()
bool IsTwoWheeledVehicle()
FTransform GetVehicleBoundingBoxTransform() const
Transform of the vehicle'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
virtual FVector GetVelocity() const
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'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)
static FBoundingBox GetVehicleBoundingBox(const ACarlaWheeledVehicle *Vehicle, uint8 InTagQueried=0xFF)
float DampingRateZeroThrottleClutchEngaged
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 FlushVehicleControl()
void SetHandbrakeInput(bool Value)
float DampingRateZeroThrottleClutchDisengaged
void DeactivateVelocityControl()
float GetWheelSteerAngle(EVehicleWheelLocation WheelLocation)
float DampingRateFullThrottle
FVehicleControl LastAppliedControl
virtual int32 GetVehicleCurrentGear() const
void RefreshLightState(const FVehicleLightState &VehicleLightState)
TMap< UPrimitiveComponent *, UPhysicsConstraintComponent * > CollisionDisableConstraints
void SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W, const FVehiclePhysicsControl &PhysicsControl)
UBoxComponent * VehicleBounds
TArray< FGearPhysicsControl > ForwardGears
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)
FVector Extent
Radii extent of the bounding box.
UWheeledVehicleMovementComponent * DebugVehicle
geom::Transform Transform
bool UseSweepWheelCollision
void SetCarlaMovementComponent(UBaseCarlaMovementComponent *MoementComponent)
void AdjustVehicleBounds()
FVehicleLightState GetVehicleLightState() const
FTransform GetVehicleTransform() const
Transform of the vehicle.
TArray< FWheelPhysicsControl > Wheels
static ACarlaRecorder * GetRecorder(const UObject *WorldContextObject)