105 FTransform ActorInverseTransform = GetActorTransform().Inverse();
111 UPhysicsConstraintComponent* ConstraintComponent =
112 Cast<UPhysicsConstraintComponent>(GetDefaultSubobjectByName(ComponentName));
113 if (ConstraintComponent)
116 GetDefaultSubobjectByName(ConstraintComponent->ComponentName1.ComponentName));
119 UE_LOG(LogCarla, Warning, TEXT(
"Door name: %s"), *(DoorComponent->GetName()));
120 FTransform ComponentWorldTransform = DoorComponent->GetComponentTransform();
121 FTransform RelativeTransform = ComponentWorldTransform * ActorInverseTransform;
125 ConstraintComponent->TermComponentConstraint();
129 UE_LOG(LogCarla, Error, TEXT(
"Missing component for constraint: %s"), *(ConstraintComponent->GetName()));
137 TArray<UPhysicsConstraintComponent*> Constraints;
138 GetComponents(Constraints);
139 for (UPhysicsConstraintComponent* Constraint : Constraints)
144 GetDefaultSubobjectByName(Constraint->ComponentName1.ComponentName));
146 GetDefaultSubobjectByName(Constraint->ComponentName2.ComponentName));
147 if (CollisionDisabledComponent1)
151 if (CollisionDisabledComponent2)
158 float FrictionScale = 3.5f;
160 UWheeledVehicleMovementComponent* MovementComponent = GetVehicleMovementComponent();
162 if (MovementComponent)
164 check(MovementComponent !=
nullptr);
169 TArray<float> OriginalFrictions;
170 OriginalFrictions.Init(FrictionScale, MovementComponent->Wheels.Num());
175 TArray<AActor *> OverlapActors;
176 GetOverlappingActors(OverlapActors, AFrictionTrigger::StaticClass());
177 for (
const auto &Actor : OverlapActors)
182 FrictionScale = FrictionTrigger->
Friction;
187 TArray<FWheelSetup> NewWheelSetups = MovementComponent->WheelSetups;
188 for (
const auto &WheelSetup : NewWheelSetups)
190 UVehicleWheel *Wheel = WheelSetup.WheelClass.GetDefaultObject();
191 check(Wheel !=
nullptr);
194 MovementComponent->WheelSetups = NewWheelSetups;
406 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
407 GetVehicleMovement());
408 check(Vehicle4W !=
nullptr);
411 PhysicsControl.TorqueCurve = Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData;
414 PhysicsControl.DampingRateFullThrottle = Vehicle4W->EngineSetup.DampingRateFullThrottle;
416 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchEngaged;
418 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchDisengaged;
421 PhysicsControl.bUseGearAutoBox = Vehicle4W->TransmissionSetup.bUseGearAutoBox;
422 PhysicsControl.GearSwitchTime = Vehicle4W->TransmissionSetup.GearSwitchTime;
423 PhysicsControl.ClutchStrength = Vehicle4W->TransmissionSetup.ClutchStrength;
424 PhysicsControl.FinalRatio = Vehicle4W->TransmissionSetup.FinalRatio;
426 TArray<FGearPhysicsControl> ForwardGears;
428 for (
const auto &Gear : Vehicle4W->TransmissionSetup.ForwardGears)
432 GearPhysicsControl.
Ratio = Gear.Ratio;
433 GearPhysicsControl.
UpRatio = Gear.UpRatio;
434 GearPhysicsControl.
DownRatio = Gear.DownRatio;
436 ForwardGears.Add(GearPhysicsControl);
447 UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
448 check(UpdatedPrimitive !=
nullptr);
450 PhysicsControl.CenterOfMass = UpdatedPrimitive->BodyInstance.COMNudge;
453 PhysicsControl.SteeringCurve = Vehicle4W->SteeringCurve.EditorCurveData;
456 TArray<FWheelPhysicsControl> Wheels;
458 for (int32 i = 0; i < Vehicle4W->WheelSetups.Num(); ++i)
463 PxVehicleWheelData PWheelData = Vehicle4W->PVehicle->mWheelsSimData.getWheelData(i);
465 PhysicsWheel.
DampingRate = Cm2ToM2(PWheelData.mDampingRate);
466 PhysicsWheel.
MaxSteerAngle = FMath::RadiansToDegrees(PWheelData.mMaxSteer);
467 PhysicsWheel.
Radius = PWheelData.mRadius;
468 PhysicsWheel.
MaxBrakeTorque = Cm2ToM2(PWheelData.mMaxBrakeTorque);
471 PxVehicleTireData PTireData = Vehicle4W->PVehicle->mWheelsSimData.getTireData(i);
475 PhysicsWheel.
LongStiffValue = PTireData.mLongitudinalStiffnessPerUnitGravity;
482 PhysicsWheel.
TireFriction = Vehicle4W->Wheels[i]->TireConfig->GetFrictionScale();
483 PhysicsWheel.
Position = Vehicle4W->Wheels[i]->Location;
485 Wheels.Add(PhysicsWheel);
491 UWheeledVehicleMovementComponentNW *VehicleNW = Cast<UWheeledVehicleMovementComponentNW>(
492 GetVehicleMovement());
494 check(VehicleNW !=
nullptr);
497 PhysicsControl.TorqueCurve = VehicleNW->EngineSetup.TorqueCurve.EditorCurveData;
500 PhysicsControl.DampingRateFullThrottle = VehicleNW->EngineSetup.DampingRateFullThrottle;
502 VehicleNW->EngineSetup.DampingRateZeroThrottleClutchEngaged;
504 VehicleNW->EngineSetup.DampingRateZeroThrottleClutchDisengaged;
507 PhysicsControl.bUseGearAutoBox = VehicleNW->TransmissionSetup.bUseGearAutoBox;
508 PhysicsControl.GearSwitchTime = VehicleNW->TransmissionSetup.GearSwitchTime;
509 PhysicsControl.ClutchStrength = VehicleNW->TransmissionSetup.ClutchStrength;
510 PhysicsControl.FinalRatio = VehicleNW->TransmissionSetup.FinalRatio;
512 TArray<FGearPhysicsControl> ForwardGears;
514 for (
const auto &Gear : VehicleNW->TransmissionSetup.ForwardGears)
518 GearPhysicsControl.
Ratio = Gear.Ratio;
519 GearPhysicsControl.
UpRatio = Gear.UpRatio;
520 GearPhysicsControl.
DownRatio = Gear.DownRatio;
522 ForwardGears.Add(GearPhysicsControl);
533 UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(VehicleNW->UpdatedComponent);
534 check(UpdatedPrimitive !=
nullptr);
536 PhysicsControl.CenterOfMass = UpdatedPrimitive->BodyInstance.COMNudge;
539 PhysicsControl.SteeringCurve = VehicleNW->SteeringCurve.EditorCurveData;
542 TArray<FWheelPhysicsControl> Wheels;
544 for (int32 i = 0; i < VehicleNW->WheelSetups.Num(); ++i)
549 PxVehicleWheelData PWheelData = VehicleNW->PVehicle->mWheelsSimData.getWheelData(i);
550 PhysicsWheel.
DampingRate = Cm2ToM2(PWheelData.mDampingRate);
551 PhysicsWheel.
MaxSteerAngle = FMath::RadiansToDegrees(PWheelData.mMaxSteer);
552 PhysicsWheel.
Radius = PWheelData.mRadius;
553 PhysicsWheel.
MaxBrakeTorque = Cm2ToM2(PWheelData.mMaxBrakeTorque);
556 PxVehicleTireData PTireData = VehicleNW->PVehicle->mWheelsSimData.getTireData(i);
559 PhysicsWheel.
LongStiffValue = PTireData.mLongitudinalStiffnessPerUnitGravity;
566 PhysicsWheel.
TireFriction = VehicleNW->Wheels[i]->TireConfig->GetFrictionScale();
567 PhysicsWheel.
Position = VehicleNW->Wheels[i]->Location;
569 Wheels.Add(PhysicsWheel);
592 UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
593 GetVehicleMovement());
594 check(Vehicle4W !=
nullptr);
599 Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData =
PhysicsControl.TorqueCurve;
604 Vehicle4W->EngineSetup.DampingRateFullThrottle =
PhysicsControl.DampingRateFullThrottle;
605 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchEngaged =
607 Vehicle4W->EngineSetup.DampingRateZeroThrottleClutchDisengaged =
611 Vehicle4W->TransmissionSetup.bUseGearAutoBox =
PhysicsControl.bUseGearAutoBox;
612 Vehicle4W->TransmissionSetup.GearSwitchTime =
PhysicsControl.GearSwitchTime;
613 Vehicle4W->TransmissionSetup.ClutchStrength =
PhysicsControl.ClutchStrength;
614 Vehicle4W->TransmissionSetup.FinalRatio =
PhysicsControl.FinalRatio;
616 TArray<FVehicleGearData> ForwardGears;
620 FVehicleGearData GearData;
622 GearData.Ratio = Gear.Ratio;
623 GearData.UpRatio = Gear.UpRatio;
624 GearData.DownRatio = Gear.DownRatio;
626 ForwardGears.Add(GearData);
629 Vehicle4W->TransmissionSetup.ForwardGears = ForwardGears;
636 UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
637 check(UpdatedPrimitive !=
nullptr);
639 UpdatedPrimitive->BodyInstance.COMNudge =
PhysicsControl.CenterOfMass;
642 Vehicle4W->SteeringCurve.EditorCurveData =
PhysicsControl.SteeringCurve;
646 if (PhysicsWheelsNum != 4)
648 UE_LOG(LogCarla, Error, TEXT(
"Number of WheelPhysicsControl is not 4."));
655 TArray<FWheelSetup> NewWheelSetups = Vehicle4W->WheelSetups;
657 for (int32 i = 0; i < PhysicsWheelsNum; ++i)
659 UVehicleWheel *Wheel = NewWheelSetups[i].WheelClass.GetDefaultObject();
660 check(Wheel !=
nullptr);
663 Wheel->TireConfig = DuplicateObject<UTireConfig>(Wheel->TireConfig,
nullptr);
666 Wheel->TireConfig->SetFrictionScale(
PhysicsControl.Wheels[i].TireFriction);
669 Vehicle4W->WheelSetups = NewWheelSetups;
672 GetWorld()->GetPhysicsScene()->GetPxScene()->lockWrite();
673 Vehicle4W->RecreatePhysicsState();
674 GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
676 for (int32 i = 0; i < PhysicsWheelsNum; ++i)
678 PxVehicleWheelData PWheelData = Vehicle4W->PVehicle->mWheelsSimData.getWheelData(i);
681 PWheelData.mMaxSteer = FMath::DegreesToRadians(
PhysicsControl.Wheels[i].MaxSteerAngle);
682 PWheelData.mDampingRate = M2ToCm2(
PhysicsControl.Wheels[i].DampingRate);
683 PWheelData.mMaxBrakeTorque = M2ToCm2(
PhysicsControl.Wheels[i].MaxBrakeTorque);
684 PWheelData.mMaxHandBrakeTorque = M2ToCm2(
PhysicsControl.Wheels[i].MaxHandBrakeTorque);
685 Vehicle4W->PVehicle->mWheelsSimData.setWheelData(i, PWheelData);
687 PxVehicleTireData PTireData = Vehicle4W->PVehicle->mWheelsSimData.getTireData(i);
690 PTireData.mLongitudinalStiffnessPerUnitGravity =
PhysicsControl.Wheels[i].LongStiffValue;
691 Vehicle4W->PVehicle->mWheelsSimData.setTireData(i, PTireData);
696 UWheeledVehicleMovementComponentNW *VehicleNW = Cast<UWheeledVehicleMovementComponentNW>(
697 GetVehicleMovement());
699 check(VehicleNW !=
nullptr);
702 VehicleNW->EngineSetup.TorqueCurve.EditorCurveData =
PhysicsControl.TorqueCurve;
707 VehicleNW->EngineSetup.DampingRateFullThrottle =
PhysicsControl.DampingRateFullThrottle;
708 VehicleNW->EngineSetup.DampingRateZeroThrottleClutchEngaged =
710 VehicleNW->EngineSetup.DampingRateZeroThrottleClutchDisengaged =
714 VehicleNW->TransmissionSetup.bUseGearAutoBox =
PhysicsControl.bUseGearAutoBox;
715 VehicleNW->TransmissionSetup.GearSwitchTime =
PhysicsControl.GearSwitchTime;
716 VehicleNW->TransmissionSetup.ClutchStrength =
PhysicsControl.ClutchStrength;
717 VehicleNW->TransmissionSetup.FinalRatio =
PhysicsControl.FinalRatio;
719 TArray<FVehicleNWGearData> ForwardGears;
725 GearData.
Ratio = Gear.Ratio;
726 GearData.
UpRatio = Gear.UpRatio;
729 ForwardGears.Add(GearData);
732 VehicleNW->TransmissionSetup.ForwardGears = ForwardGears;
739 UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(VehicleNW->UpdatedComponent);
740 check(UpdatedPrimitive !=
nullptr);
742 UpdatedPrimitive->BodyInstance.COMNudge =
PhysicsControl.CenterOfMass;
745 VehicleNW->SteeringCurve.EditorCurveData =
PhysicsControl.SteeringCurve;
753 TArray<FWheelSetup> NewWheelSetups = VehicleNW->WheelSetups;
755 for (int32 i = 0; i < PhysicsWheelsNum; ++i)
757 UVehicleWheel *Wheel = NewWheelSetups[i].WheelClass.GetDefaultObject();
758 check(Wheel !=
nullptr);
761 Wheel->TireConfig = DuplicateObject<UTireConfig>(Wheel->TireConfig,
nullptr);
764 Wheel->TireConfig->SetFrictionScale(
PhysicsControl.Wheels[i].TireFriction);
767 VehicleNW->WheelSetups = NewWheelSetups;
770 GetWorld()->GetPhysicsScene()->GetPxScene()->lockWrite();
771 VehicleNW->RecreatePhysicsState();
772 GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
774 for (int32 i = 0; i < PhysicsWheelsNum; ++i)
776 PxVehicleWheelData PWheelData = VehicleNW->PVehicle->mWheelsSimData.getWheelData(i);
779 PWheelData.mMaxSteer = FMath::DegreesToRadians(
PhysicsControl.Wheels[i].MaxSteerAngle);
780 PWheelData.mDampingRate = M2ToCm2(
PhysicsControl.Wheels[i].DampingRate);
781 PWheelData.mMaxBrakeTorque = M2ToCm2(
PhysicsControl.Wheels[i].MaxBrakeTorque);
782 PWheelData.mMaxHandBrakeTorque = M2ToCm2(
PhysicsControl.Wheels[i].MaxHandBrakeTorque);
783 VehicleNW->PVehicle->mWheelsSimData.setWheelData(i, PWheelData);
785 PxVehicleTireData PTireData = VehicleNW->PVehicle->mWheelsSimData.getTireData(i);
788 PTireData.mLongitudinalStiffnessPerUnitGravity =
PhysicsControl.Wheels[i].LongStiffValue;
789 VehicleNW->PVehicle->mWheelsSimData.setTireData(i, PTireData);
797 if (Recorder && Recorder->IsEnabled())
799 Recorder->AddPhysicsControl(*
this);
821 auto *MovementComponent = GetVehicleMovement();
828 TelemetryData.
EngineRPM = MovementComponent->GetEngineRotationSpeed();
830 TelemetryData.
Drag = MovementComponent->DebugDragMagnitude / 100.0f;
833 FPhysXVehicleManager* MyVehicleManager = FPhysXVehicleManager::GetVehicleManagerFromScene(GetWorld()->GetPhysicsScene());
835 SCOPED_SCENE_READ_LOCK(MyVehicleManager->GetScene());
836 PxWheelQueryResult* WheelsStates = MyVehicleManager->GetWheelsStates_AssumesLocked(MovementComponent);
839 TArray<FWheelTelemetryData> Wheels;
840 for (uint32 w = 0; w < MovementComponent->PVehicle->mWheelsSimData.getNbWheels(); ++w)
844 WheelTelemetryData.
TireFriction = WheelsStates[w].tireFriction;
845 WheelTelemetryData.
LatSlip = FMath::RadiansToDegrees(WheelsStates[w].lateralSlip);
846 WheelTelemetryData.
LongSlip = WheelsStates[w].longitudinalSlip;
847 WheelTelemetryData.
Omega = MovementComponent->PVehicle->mWheelsDynData.getWheelRotationSpeed(w);
849 UVehicleWheel* Wheel = MovementComponent->Wheels[w];
850 WheelTelemetryData.
TireLoad = Wheel->DebugTireLoad / 100.0f;
852 WheelTelemetryData.
Torque = Wheel->DebugWheelTorque / (100.0f * 100.0f);
853 WheelTelemetryData.
LongForce = Wheel->DebugLongForce / 100.f;
854 WheelTelemetryData.
LatForce = Wheel->DebugLatForce / 100.f;
858 Wheels.Add(WheelTelemetryData);
861 TelemetryData.
Wheels = Wheels;
863 return TelemetryData;
1061 DoorComponent->DetachFromComponent(
1062 FDetachmentTransformRules(EDetachmentRule::KeepWorld,
false));
1063 FTransform DoorInitialTransform =
1065 DoorComponent->SetWorldTransform(DoorInitialTransform);
1066 DoorComponent->SetSimulatePhysics(
true);
1067 DoorComponent->SetCollisionProfileName(TEXT(
"BlockAll"));
1068 float AngleLimit = Constraint->ConstraintInstance.GetAngularSwing1Limit();
1069 FRotator AngularRotationOffset = Constraint->ConstraintInstance.AngularRotationOffset;
1071 if (Constraint->ConstraintInstance.AngularRotationOffset.Yaw < 0.0f)
1073 AngleLimit = -AngleLimit;
1075 Constraint->SetAngularOrientationTarget(FRotator(0, AngleLimit, 0));
1078 Constraint->InitComponentConstraint();
1080 UPhysicsConstraintComponent** CollisionDisable =
1082 if (CollisionDisable)
1084 (*CollisionDisable)->InitComponentConstraint();