Network physics component - how to do for complex systems with constraints?

I’m working on physics-based multiplayer game and I need to have an actor like tower crane - 3 physical body with 2 constraints.

With a classic options when server does all the physics the controls on client side feels bad - delay and jitters. I’m trying now to do that using network physics component.

I tested it on a simple actor based on this example:

And this seems to work okay. However when I’m trying to apply that to a more complex system, I see that the result is producing, but it looks exactly the same as server authoritieve option, so I guess it’s not applied correclty.

This is a short example of my code, not everything but to give an idea of what I’m doing:

USTRUCT()
struct FCraneInputs
{
GENERATED_BODY()

FCraneInputs()
	: SteeringInput(0.f)	
	, HookMovement(FVector::ZeroVector)
{
}

// Steering output to physics system. Range -1...1
UPROPERTY()
float SteeringInput;

// Desired direction
UPROPERTY()
FVector HookMovement;

};

void UCraneMovementComponent::AsyncPhysicsTickComponent(float DeltaTime, float SimTime)
{
Super::AsyncPhysicsTickComponent(DeltaTime, SimTime);




if (!TowerBodyInstance || !UpdatedConstraint)
{
	return;
}

// Apply torque to rotate the tower
if (const auto Handle = TowerBodyInstance->ActorHandle)
{
	HandleTower = Handle->GetPhysicsThreadAPI();

	if (HandleTower)
	{

		HandleTower->AddTorque(FVector(0, 0, CraneInputs.SteeringInput * 10000000000));
	}

// Change the constraint (not sure it's safe to do this way in physics thread)
if (UpdatedConstraint)
{
	UpdatedConstraint->SetLinearPositionTarget(CraneInputs.HookMovement); 

}

yups. but as you can see there’s a reason why that’s the way that’s most common.

i’m can’t answer your question directly, specially since it’s quite broad, but i would try to improve server authoritative physics instead.

if you are going to send transforms from clients to server i would personally not send it every frame.

anyway, i wish you the best.

Hi, I’m currently diving into the networked physics topic because of my project demands and I really hate to be sucked in by another rabbit hole but from what I can see, it opens awesome opportunities, like a higher playercount on a server since you are bound to use network replication in combination with iris, however I’m definetly also no pro in this.

My guess is that you shouldn’t update the constraint in the async task. Use it to do physics calculations only like the force and impulse and torque stuff and use the according structs only to send data in or out of the game thread. Is your simulation mode set to resimulation? Did you set double float replication for location and rotation?

Maybe this will help you, too (it’s more for a pawn instead of a vehicle but there shoud be enough similarities):

And dont forget to annoy our all new friend with that:

https://dev.epicgames.com/community/assistant/unreal-engine

But please share your experience with us, I have the feeling that a lot of people are going to need it

Hi there, did you manage to actually get the result from this tutorial? Because I didn’t, for me it the client was constantly being pulled back to initial position.

I would be happy if you could share your experience about this tutorial if you managed to make it work :slight_smile:

I’m actually still struggling with the approach from the article I’ve attached in initial post, trying to make it work well with just 1 body, my result is the following:

  1. it doesn’t go out of sync

  2. movement of the client’s pawn is more-less smooth;

  3. but with large network delay it actually feels super delayed as well, almost like in a classic world with server authoritative approach.

I’ve set double float replication, also I was experimenting with resim and predictive mode. Resim reacts faster but artifacts when you collide with something are crazy as hell.

Will be happy to have a chat if you have more experience with that! It really looks like not a lot of people are working with these :slight_smile:

Hi Dude, I’m happy to see that you replied to stay in touch.

I’m still implementing the stuff. My project was pretty far before I realized a few days ago that I have to use networked physics, and my project was blueprint only. Thus it will take a little bit of time before I get done with the implementation in c++, but I will reduce myself to do just the networked physics in c++ for now.

Which is good, isn’t it? Did I get something wrong here? It’s supposed to not go out of sync afaik. I have implemented the networked physics stuff in my code but I didn’t transfer my physics stuff into the async task, yet. I can’t make any sync tests before this is done.

From my recent experiments, I figured out that I have to set some limitation for more smoothness.

Dont forget to keep “Async Physics Tick enabled” DISABLED in the Class Defaults of your pawn/vehicle.

grafik

And in my case, I have a pretty high amount of dampening. Maybe this will help you, too to get your vehicle more smooth? You will have to raise acceleration and steering forces by a lot to get things balanced when dampening is high.

However this works terrible since I have enabled async physics in my project settings. This might have rendered completely useless but I can’t tell for now.

Oh by the way, Max Angular Velocity helped me to get stuff more stable on behaviour, even with async phys enabled. It might really help you to set max velocities. My max angular velocity is 150, and I have very fast flying objects with around 500 km/h which I try to keep in sync.

However I won’t be able to work on my stuff before monday, so I will have to let you wait until then before I can tell you more.

Maybe this has something to do with the replication mode of your physics. From my understanding, resim is good for autonomus proxies and predictive is good for simulated proxies. My first attempt was to check which is my local role and decide by that which replication mode to use. But this is wrong, because the tutorial said:

The multiplayer replication mode we will use for the pawn is Physics Resimulation. Note that this replication mode doesn’t work well together with other modes like Predictive Interpolation yet, so all physics replicated objects should use the same replication mode (in this case Resimulation).

But you should still check for the local role in the constructor or in begin play and set replication mode by that. The tutorial shows the best practice for now.

Sorry, for now I can just help you more in theory and less in code.

I will be back in a few days to share my progress in code. Please keep your head up until then. I’m pretty sure that I will get this to work, too. And then we can see how to get constrains to work with this! I might need to know that, too

Hi there, some update from my side, I’ve managed to run this tutorial for one pawn and it works quite well.

However, regarding constraints it’s still huge pain and currently I’m not quite sure it’s possible to be honest

This is Unreal, so you got to get Real…

Physics are horrible. Even reverting to PhysX isn’t going to help much. You’ll just have issues.

All you can do is a different simulation on each client and some more or less “creative" adjusting based on an authoritative client that performs the simulation as well (this could be a client or the server. Depends on your setup). Oh and by creative generally an finterpto or similar so you can’t notice the shifting.

Otherwise, forget the physics.

Do the math. The math is always right. Run the same math on all clients. All clients will always be correct to one another (with minimal floating point driven errors).

You could think of the math as the server doing its authoritative thing and handing down values, except it won’t.

The only client side variable is Game Time, and servers are generally good at keeping that synced up (yea you could still cheat, but minimal impact and easily detected by almost any anti cheat).

So with that, the equations you come up with being solved for Time, will produce the same result on anything running the math in milliseconds without having to depend on network transmission.

That’s generally how you “make multiplayer games" unless you happen to work for Activision, Ubisoft, or EA, in which case you’ll just do the worse possible thing you can conceive and maybe even just have the player that gets a hit result determine the outcome authoritatively :wink:

As far as what this gets you…

Let’s just say that it wouldn’t be the first multiplayer ocean to allow hitting targets moving with waves on different clients. It’s nothing new really. It’s EA and co that can’t be bothered to do things right :wink:

Well, the idea of calculating the same math on each client is valid, of course, but for anything complex you can’t just write the equations down manually, so that’s why I’m trying to work with NetworkPhysicsComponent, that is basically allows to run the same physics simulations async in chaos for both client and server and then do server correction.

with single bodies now it works perfect, even with bad network emulation is doesn’t look annoying. But for the system of bodies, yeah, maybe the tech is not ready yet. I will try to reach out the author of the component and ask him what’s his expectations about that :slight_smile:

but for anything complex you can’t just write the equations down manually

An 8 or 16 cluster gerstner wave is likely 30 times more complicated than whatever math you need.

It’s done by creating a blueprint callable class/function in c++ and having it return whatever math result it needs to return (again based on time).

If your complaint is “well, I’d have no idea what math to put down”, then that is understandable.

You either need to trial and error some stuff, or to be capable of thinking like a mathematician… Maybe though, you can ask either a mathematics forum or AI to come up with some math… just don’t go fully trusting either source and rely on what you experience in pracrice during the “simulation” rather than whatever they “claim"… I mean chatGPT wasn’t even able to calculate the flow of a venturi funnel when given the equation, so…

I’m well familiar with modeling of the physical systems, it’s my full time job outside the gamedev hobby :slight_smile: I’m just saying that in my case even the base system of equations will include too many states (3 bodies with 2 constraints), but more importantly the true amount of equations is unknown because what I need is the prediction of physics when this system collides with something in the world, it could be any amount of additional rigid bodies.

So that’s why I’m trying to work with that from using physics simulations on UE, since both client and server have more-less equal worlds and both can perform chaos simulations, it should work in theory. At least for actors with single physical body it works awesome! even with bad connection I instantly can see the result of collisions on client (and it will be slightly corrected then by server)

For anyone to find this, here is how I finally did that:

this tutorial is a base: Networked Physics - Pawn Tutorial | Community tutorial
(but check the latest comments)

I’ve created 2 separate actors with network components, one is with actual control (apply torque) and the second one is without anything

Then I created a APhysicsConstraintActor, constraint the actors, and pass the reference of it into that actor with control.

In actor with control I’m not just apply torque, I also get handle to constraint and control it there as well.

And that’s it, it works smooth and good with average network connection. Hopefully that will be enough for my case :slight_smile:

2 Likes