Disable PhysXVehicleManager when physics are off

This commit is contained in:
Daniel Santos-Olivan 2021-01-13 13:01:38 +01:00 committed by Axel1092
parent f9ca7b5577
commit 3e3b7fcb20
3 changed files with 44 additions and 20 deletions

View File

@ -915,10 +915,15 @@ void FCarlaServer::FPimpl::BindActions()
RESPOND_ERROR("unable to set actor simulate physics: actor not found");
}
auto Character = Cast<ACharacter>(ActorView.GetActor());
// The physics in the walkers works in a different way so to disable them,
auto* Character = Cast<ACharacter>(ActorView.GetActor());
auto* CarlaVehicle = Cast<ACarlaWheeledVehicle>(ActorView.GetActor());
// The physics in the vehicles works in a different way so to disable them.
if (CarlaVehicle != nullptr){
CarlaVehicle->SetSimulatePhysics(bEnabled);
}
// The physics in the walkers also works in a different way so to disable them,
// we need to do it in the UCharacterMovementComponent.
if (Character != nullptr)
else if (Character != nullptr)
{
auto CharacterMovement = Cast<UCharacterMovementComponent>(Character->GetCharacterMovement());
@ -938,24 +943,10 @@ void FCarlaServer::FPimpl::BindActions()
{
RESPOND_ERROR("unable to set actor simulate physics: not supported by actor");
}
auto Vehicle = Cast<ACarlaWheeledVehicle>(ActorView.GetActor());
if(Vehicle)
{
Vehicle->SetActorEnableCollision(true);
#ifdef WITH_CARSIM
if(!Vehicle->IsCarSimEnabled())
#endif
{
RootComponent->SetSimulatePhysics(bEnabled);
RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
}
}
else
{
RootComponent->SetSimulatePhysics(bEnabled);
RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
}
}
return R<void>::Success();
};

View File

@ -635,6 +635,34 @@ void ACarlaWheeledVehicle::EnableCarSim(FString SimfilePath)
#endif
}
void ACarlaWheeledVehicle::SetSimulatePhysics(bool enabled) {
UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
GetVehicleMovement());
check(Vehicle4W != nullptr);
if(bPhysicsEnabled == enabled)
return;
SetActorEnableCollision(true);
#ifdef WITH_CARSIM
if(!IsCarSimEnabled())
#endif
{
auto RootComponent = Cast<UPrimitiveComponent>(GetRootComponent());
RootComponent->SetSimulatePhysics(enabled);
RootComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
GetWorld()->GetPhysicsScene()->GetPxScene()->lockWrite();
if(enabled)
Vehicle4W->RecreatePhysicsState();
else
Vehicle4W->DestroyPhysicsState();
GetWorld()->GetPhysicsScene()->GetPxScene()->unlockWrite();
}
bPhysicsEnabled = enabled;
}
void ACarlaWheeledVehicle::UseCarSimRoad(bool bEnabled)
{
#ifdef WITH_CARSIM

View File

@ -124,6 +124,8 @@ public:
void ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl);
void SetSimulatePhysics(bool enabled);
void SetWheelCollision(UWheeledVehicleMovementComponent4W *Vehicle4W, const FVehiclePhysicsControl &PhysicsControl);
void SetVehicleLightState(const FVehicleLightState &LightState);
@ -295,6 +297,9 @@ private:
UPROPERTY(Category="CARLA Wheeled Vehicle", EditAnywhere)
float CarSimOriginOffset = 150.f;
UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere)
bool bPhysicsEnabled = true;
// Small workarround to allow optional CarSim plugin usage
UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UMovementComponent * ExternalMovementComponent;