Physics Sub-Stepping

Yes, for our project we have added that feature and it should be present in engine versions 4.6 and up.

In the tick of component which you want to access sub-steps from the game, do this:



void URailroadWheelComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// Add custom physics forces each tick
	GetBodyInstance()->AddCustomPhysics(OnCalculateCustomPhysics);
}


This will call the delegate ‘OnCalculateCustomPhysics’ during next tick, for each substep (16 times for 16 substeps). Define delegate as:



FCalculateCustomPhysics OnCalculateCustomPhysics;
OnCalculateCustomPhysics.BindUObject(this, &UTrainPhysicsSimulator::CustomPhysics);


The delegate body is:



void UTrainPhysicsSimulator::CustomPhysics(float DeltaTime, FBodyInstance* BodyInstance)
{
	URailroadWheelComponent* RailroadWheelComponent = Cast<URailroadWheelComponent>(BodyInstance->OwnerComponent.Get());
	if (!RailroadWheelComponent) return;

	// Calculate custom forces
	//.....

	// You will have to apply forces directly to PhysX body if you want to apply forces! If you want to read body coordinates, read them from PhysX bodies! This is important for sub-steps as transformations aren't updated until end of physics tick
	PxRigidBody* PRigidBody = BodyInstance->GetPxRigidBody();
	PxTransform PTransform = PRigidBody->getGlobalPose();
	PxVec3 PVelocity = PRigidBody->getLinearVelocity();
	PxVec3 PAngVelocity = PRigidBody->getAngularVelocity();

	//......

	PRigidBody->addForce(PForce, PxForceMode::eFORCE, true);
	PRigidBody->addTorque(PTorque, PxForceMode::eFORCE, true);
}


You can contact me at #unrealengine (nickname ), this isn’t the correct account from which I should be replying.

1 Like