Physics substepping and drifting of transform from mesh over time after applying a force or torque.

Can anyone help me out with a Unreal physics problem. Happens in 4.8 or 4.10.3, not tested other versions.

I’m substepping the physics, applying a torque directly to the rigidbody in the substep function. It starts to rotate, all looks normal, but when I debug draw the transform position and axis from the physics rigidbody (not the unreal mesh), it is drifting further away from the actual unreal mesh It’s not a simple one frame lag, but a huge drift in both position and orientation over time until they are completely out of sync.

The setup is simple, set substeeping on and call custom function from tick, apply torque on PxRigidbody in substep. Now back in tick, get the PxRigidbody again and draw the transform and axis. Watch the debug lines drift when simulation, falling under gravity. Seems to be fine if no torque is applied.

Some simple code…



void AHoverTest::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	m_pBoxMeshComponent->GetBodyInstance()->AddCustomPhysics(OnCalculateCustomPhysics);

	FlushPersistentDebugLines(this->GetWorld());

	PxRigidBody* PRigidBody = m_pBoxMeshComponent->GetBodyInstance()->GetPxRigidBody();
	PxTransform PTransform = PRigidBody->getGlobalPose();

	PxVec3 o = PTransform.p;
	PxVec3 up = PTransform.q.getBasisVector2();
	PxVec3 to = o + (up * 1000.0f);
	FVector4 hkFrom;
	FVector4 hkTo;

	hkTo.Set(to.x, to.y, to.z, 1.0f);
	hkFrom.Set(o.x, o.y, o.z, 1.0f);


	DrawDebugLine(
	this->GetWorld(),
	hkFrom,
	hkTo,
	FColor(255, 0, 0),
	true, -1, 0,
	2
	);
	
}


void AHoverTest::CustomPhysics(float DeltaTime, FBodyInstance* BodyInstance)
{

	check(this->GetWorld());

	FPhysScene *fScene = this->GetWorld()->GetPhysicsScene();

	// Scene Lock for Multi-Threading
	PxScene* pSyncScene = fScene->GetPhysXScene(PST_Sync);

	SCOPED_SCENE_WRITE_LOCK(pSyncScene);

	PxRigidBody* PRigidBody = BodyInstance->GetPxRigidBody();

	PRigidBody->addTorque(PxVec3(0.0f, 10000000.0f, 0.0f));
}


This is what it looks like when it’s out of sync…should be a line from position in the up direction of the cube.

Won’t your code draw debug line first and only then call physics update? Because you code is inside the Tick() function itself and Super::Tick() won’t be updating physics from what I understood.
The other thing is all DrawDebug functions are lagging even if you draw them without sub-stepping. It’s enough for the object to be moving at 50km/h for debug lines to be way back behind.

I’m expecting a frame lag, but not this much. This was a simple setup with a cube, but I’m trying to debug my complex physics system but it’s rather difficult if there is this much deviation. Hence trying something simple that is rotating and falling through the air. It shouldn’t be travelling that fast. At the beginning of the simulation the drift is less noticeable.

What is the best order todo things then ? Is there a function that get calls after all the physics have been done and before the rendering or after everthing has been done ? Unity has a post update function where you can be assured that all the physics and the rendering have been done. Does Unreal have the same ?

I’m not sure about post physics function inside of the same actor. Normally, your actor have to be set to Pre_Physics TickGroup, as to my understanding its an requirement for CustomPhysics delegate to execute properly. I assume you have this enforced.
What I do for debugging sub-stepping physics is write down intermediate debug vectors into an array and then render all of them during regular tick(). You will still have a bit of lag. What I haven’t tried is to pass this array to another actor which is set to Post_Physics TickGroup and render it from there.

You could also use array component for visualization if you have issues with debug lines. Basically you could try to combine this with BoredEngineers idea and just make another actor that contains only arrow component, and then change a variable on it that updates it’s transform on next Tick.

Ok, as a neebie to unreal, but with experience of PhysX, Bullet and Havok I’m baffled how Unreal is currently working with sub stepping or the relationship with physics and the rendered view.

What I want to achieve: Enable substepping to run my complex physics and then update some meshes/actors based upon the physics results. Sounds simple, but tearing my hair out in unreal because of the lag

So pausing in the editor, the displayed mesh seems a frame in advance, ie printing the position and checking the inspector, the two values do not match. Inspector or rendered view is a frame in advance. How do I access the rendered transform ?

Thank you all for your help. With this and re-reading the documentation I resolved my problrem. A quick recap, I was debug drawing the physics position and it was always at least 2 frames behind. This was because I was drawing in the tick function of the actor and it’s tick group is set to TG_PrePhysics . Therefore, it is a frame behind and then debug draw is a further frame behind.

To fix, I created a component and set the tick group to TG_PostPhysics and it’s in here that I do the debug drawing or if you want to set anthing to a certain location.

Nice! Thank you for sharing results. As I said, I didn’t knew if that would work but glad to hear that this is how debugging should be properly done.