Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for help debugging a problem with rigid body location during the physics simulation #679

Open
PhilMaguire opened this issue Mar 6, 2025 · 1 comment

Comments

@PhilMaguire
Copy link

Hi there,

We are using PhysX 3.4 as part of Unreal Engine 4.27.

We are a racing game so are making use of the vehicle simulation. In this particular instance I am not, however.

Our AI racers "teleport" a mesh around the route to give us better control of their speed for balancing the difficulty. The logic is as follows:

  • Before the physics simulation is started for frame N work out the target transform
  • Calculate the angular and linear velocities to reach the target transform during the physics simulation
  • Set these on the vehicle mesh
  • Post physics simulation for frame N set the global pose to ensure that the mesh ends up where we wanted it (in case of collisions etc)

I am seeing some very unexpected behaviour when I apply this logic to a mesh. I have captured this behaviour in PVD. I have uploaded my PVD capture here https://drive.google.com/file/d/1xW_vvPCEf2KLyog6xkq6ihRwGpSgN2fu/view?usp=sharing

If you take a look at the behaviour of BP_Vehicle_Offroad_3_C_0.VehicleMesh from frame 277 you will see that it moves backwards then goes forwards for a few frames then jumps backwards again. This is really not what I would expect given the inputs and I am at a loss to work out how this is happening.

If I take a look at a single frame of output from my code at approximately this point this is what I see:

Pre-Physics simulation

LogTemp: [2569] BP_Vehicle_Offroad_3_C_0 Current V(X=96798.45, Y=-153021.58, Z=975.27) Target V(X=96706.05, Y=-153027.98, Z=983.85)

LogTemp: [2569] BP_Vehicle_Offroad_3_C_0 TargetVel V(X=-4434.68, Y=-307.44, Z=411.70) Expected End Physics Loc V(X=96706.05, Y=-153027.98, Z=983.85) DeltaTime 0.020837

LogTemp: [2569] BP_Vehicle_Offroad_3_C_0 PrePhysics PhysX Transform V(X=96798.45, Y=-153021.58, Z=975.27) R(P=4.81, Y=-176.24, R=-0.21)

LogVehicleForce: Verbose: [2569] NONPLAYER VEHICLE FORCE - SET LINEAR VELOCITY - new: V(X=-4434.68, Y=-307.44, Z=411.70) current-new: V(X=19.83, Y=-0.27, Z=-3.35) added by BP_RacerAIControllerState_SteerToInitialSpline_C_0 to vehicle BP_Vehicle_Offroad_3_C_0 (comp VehicleMesh)

The first line is my current UE4 scene component world transform. Note that it is identical to the PhysX transform on the 3rd line. I have read this from the physx sim using

FTransform VehicleTransform = FTransform::Identity;
FPhysicsCommand::ExecuteRead(Vehicle->GetMesh()->GetBodyInstance()->ActorHandle, [&](const FPhysicsActorHandle& Actor)
{
	PxRigidActor* PActor = FPhysicsInterface::GetPxRigidActor_AssumesLocked(Actor);

	// calculate the test global pose of the rigid body
	VehicleTransform = P2UTransform(PActor->getGlobalPose());
});

You can see on the 4th line of output I am setting the velocity on the vehicle mesh to move me to my desired location during the physics simulation based on delta time of 0.020837. For the purposes of debugging I am then extrapolating what I expect to happen in my 2nd line of output with Expected End Physics Loc = Current + (TargetVel*DeltaTime). This matches my "Target" location from the first print.

During Physics Simulation

[568]LogTemp: FPhysSubstepTask::UpdateTime DeltaSeconds 0.020837 NumSubsteps 2 SubTime 0.010419
[568]LogTemp: FPhysSubstepTask::SubstepSimulationStart DeltaTime 0.010419 bLastSubstep 0
[568]LogCCDContactModify: Verbose: Actor BP_Vehicle_Sedan_Traffic_Taxi_C_0 decided to ignore this collision with Actor Roads_9
[568]LogCCDContactModify: Verbose: Actor BP_Vehicle_Sedan_Traffic_Taxi_C_0 decided to ignore this collision with Actor Roads_9
[568]LogTemp: FPhysSubstepTask::SubstepSimulationStart DeltaTime 0.010419 bLastSubstep 1
[568]LogCCDContactModify: Verbose: Actor BP_Vehicle_Sedan_Traffic_Taxi_C_0 decided to ignore this collision with Actor Roads_9
[568]LogCCDContactModify: Verbose: Actor BP_Vehicle_Sedan_Traffic_Taxi_C_0 decided to ignore this collision with Actor Roads_9

We have contact modification for all vehicles. I can tell from this that my offroad 3 has not had any contacts as I have no output about attempting to modify them.

End Physics Simulation

LogTemp: [2569] BP_Vehicle_Offroad_3_C_0 Transform V(X=97422.77, Y=-152984.89, Z=922.44) R(P=5.10, Y=-176.26, R=-0.21) Vel V(X=-4434.68, Y=-307.44, Z=411.70) (R(P=5.29, Y=-176.03))

Once the physics simulation has run my vehicle mesh has surprisingly moved in the wrong direction. Note the X coordinate has increased from my pre-simulation location, rather than decreased, which is what I would expect from a negative component to my x velocity. Printing out the velocity after the simulation you can see that it is unchanged from that which I set before the simulation started. Puzzling.

Post Physics Simulation

LogTemp: [2569] BP_Vehicle_Offroad_3_C_0 PostPhysicsTick teleporting vehicle to V(X=96706.05, Y=-153027.98, Z=983.85) R(P=5.50, Y=-176.04, R=-0.20)
LogTemp: [2569] BP_Vehicle_Offroad_3_C_0 Post Physics Transform V(X=96706.05, Y=-153027.98, Z=983.88) R(P=5.39, Y=-176.27, R=-0.21)

I SetWorldLocationAndRotation on my VehicleMesh. To make really sure this has put it where I wanted it I then getGlobalPose again and log that out as "Post Physics Transform"

Here is a more visual representation of what has happened.

Image

The yellow vehicle mesh is where the vehicle started the frame (in UE component and in physx sim). The red vehicle mesh is where I wanted it "TargetLocation". The blue vehicle mesh is where it ended up. Velocity is going from right to left in my picture.

The only explanation I can think of is that something is setting the global pose of my vehicle mesh between me checking it pre sim and the simulation moving it. How else could a negative x velocity result in the vehicle moving in positive x as a result of the simulation running?

I tried to catch this happening by implementing a very crude locking system. Immediately after I read the global pose pre-simulation to log it out I call my debug function LockGlobalPose. This simply records the handle to the physx rigid actor in a static array. If anything calls SetGlobalPose_AssumesLocked from UE on a handle that is in this array the code is set to fail a check. Before I teleport my vehicle in Post Physics simulation I call my debug function UnlockGlobalPose. This removes the handle from the static array. I have validated this works by not calling UnlockGlobalPose and I fail the check when I teleport the vehicle.

With these checks in place I don't hit them but still experience the going backwards behaviour. The check was on when I recorded my PVD capture. I have also break pointed every line of code that calls setGlobalPose external to the implementation in SetGlobalPose_AssumesLocked and none of these are triggered. It seems the vehicle mesh is not getting moved by SetGlobalPose_AssumesLocked, except by me in Post Physics Tick.

Please could you advise where I can go from here to identify the cause of this issue? Is there any other external way a mesh could have it's pose altered in the sim? Is something about what I am doing to move the mesh the cause of the issue? Any help greatly appreciated.

Thanks in advance,

Phil

@PhilMaguire
Copy link
Author

Hi there,

Looking over my pvd capture with fresh eyes I note that the centre of mass of my vehicle mesh is really really strange. On frame 277 it is something like 13km under my mesh!

This must be the cause of the issue. Any ideas what might cause a centre of mass to end up in such a strange location?

Thanks again,

Phil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant