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.