Modifying vehicle properties at runtime

Is it possible to change a vehicles properties at runtime? Any changes I make to VehicleMovement (UWheeledVehicleMovementComponent) do not take effect until the vehicle has respawned.

I need to be able to change the vehicles physics on the fly but I cannot figure out a way to do so.

Simple example:

VehicleMovement->COMOffset = FVector(0.0f, 0.0f, 0.0f);
VehicleMovement->MaxEngineRPM = 10000;
VehicleMovement->Wheels[0]->LongStiffValue = 1000;

I’m assuming that the underlying PhysX simulation data is not being updated, is there any way to “refresh” it without destroying the vehicle?

I also interested on that. So looking forward to hear a answer

Hi JackNorris,

To change vehicle properties you can try to create class derived from UWheeledVehicleMovementComponent4W, then create some function and set up options you want (Mass, MaxEngineRPM, etc) and call SetupVehicle(). To find example how to create your own VehicleMovementComponent check VehicleGame example (UVehicleMovementComponentBoosted4w.h/cpp).

void UVehicleMovementComponentBoosted4w::SetUpMyOptions()
{
	COMOffset = FVector(0.0f, 0.0f, 0.0f);
	MaxEngineRPM = 10000;
	Wheels[0]->LongStiffValue = 1000;
	SetupVehicle();
}

Cheers,

Hi ,

Thanks for the suggestions, but I don’t think this solution can be valid for OP’s situation/example. For example, Wheels is immediately overwritten by the WheelSetups in SetupVehicle(). Modifications made to Wheels[0] will not affect the simulation.

Further, the recreation of the physics asset causes a loss of information - the vehicle velocity is reset, for example. This doesn’t really fulfill the criteria of ‘on the fly’.

I haven’t had any luck with calling SetupVehicle(), as I appear to lose the ability to control the vehicle afterward (user inputs not updated). I had more luck calling RecreatePhysicsState(), as I could drive the vehicle afterward, but the reset on velocity still applied.

Any other ideas on what to try?

Did anyone find a solution to this? What is the correct way to apply changes to vehicle physics settings (engine, wheels) at runtime?

VehicleGame.Build.cs

		PrivateDependencyModuleNames.AddRange(
			new string[] {
				"Slate",
				"SlateCore",
				"PhysX",
			}
		);




class UVehicleMovementComponentBoosted4w

VehicleMovementComponentBoosted4w.h

	/** Wheel Lat Stiff */
	UFUNCTION(BlueprintCallable, Category = "Wheel")
	void SetWheelLatStiff(int32 WheelIndex, float LatStiffValue, float LatStiffMaxLoad, float LongStiffValue, class UTireType* TireType);

	/** Wheel Friction*/
	UFUNCTION(BlueprintCallable, Category = "Wheel")
	void SetWheelFrictionScale(int32 WheelIndex, class UTireType* TireType);

	
VehicleMovementComponentBoosted4w.cpp
	
#if WITH_PHYSX
#include "PxVehicleWheels.h"
#endif //WITH_PHYSX


void UVehicleMovementComponentBoosted4w::SetWheelLatStiff(int32 WheelIndex, float LatStiffValue, float LatStiffMaxLoad, float LongStiffValue, class UTireType* TireType)
{

	if (WheelIndex >= 0 && LatStiffValue > 0.01 && LatStiffMaxLoad > 0.01 && LongStiffValue > 0.01 && Wheels.Num() > WheelIndex)
	{
		Wheels[WheelIndex]->LatStiffValue = LatStiffValue;
		Wheels[WheelIndex]->LatStiffMaxLoad = LatStiffMaxLoad;
		Wheels[WheelIndex]->LongStiffValue = LongStiffValue;
		Wheels[WheelIndex]->TireType = TireType;

		// init tire data
		// physx::PxVehicleTireData PTireData;
		physx::PxVehicleTireData PTireData;
		PTireData.mType = Wheels[WheelIndex]->TireType ? Wheels[WheelIndex]->TireType->GetTireTypeID() : GEngine->DefaultTireType->GetTireTypeID();
		PTireData.mLatStiffX = Wheels[WheelIndex]->LatStiffMaxLoad;
		PTireData.mLatStiffY = Wheels[WheelIndex]->LatStiffValue;
		PTireData.mLongitudinalStiffnessPerUnitGravity = Wheels[WheelIndex]->LongStiffValue;
		
		PVehicle->mWheelsSimData.setTireData(WheelIndex, PTireData);
	}

	
}

void UVehicleMovementComponentBoosted4w::SetWheelFrictionScale(int32 WheelIndex, class UTireType* TireType)
{
	if (WheelIndex >= 0 && Wheels.Num() > WheelIndex)
	{
		Wheels[WheelIndex]->TireType = TireType;
		// init tire data
		// physx::PxVehicleTireData PTireData;
		physx::PxVehicleTireData PTireData;
		PTireData.mType = Wheels[WheelIndex]->TireType ? Wheels[WheelIndex]->TireType->GetTireTypeID() : GEngine->DefaultTireType->GetTireTypeID();
		PTireData.mLatStiffX = Wheels[WheelIndex]->LatStiffMaxLoad;
		PTireData.mLatStiffY = Wheels[WheelIndex]->LatStiffValue;
		PTireData.mLongitudinalStiffnessPerUnitGravity = Wheels[WheelIndex]->LongStiffValue;

		PVehicle->mWheelsSimData.setTireData(WheelIndex, PTireData);
	}
}

in 4.15:
VehicleGame.Build.cs

         PrivateDependencyModuleNames.AddRange(
             new string[] {
                 "Slate","SlateCore",
                 "PhysX",
                 "PhysXVehicles", "PhysXVehicleLib",
             }
         );

How would you go about doing this in 4.18.3?