How can I implement deterministic physics

Hi,

I am working on a multiplayer game and some level actors has physics enabled and they call fall over.

I notice a lot of issues with replication as sometime the server will tell the object fell to a direction but the client fell to a slightly different direction, causing collision issues.

How can I implement deterministic physics so the server can tell the client the right direction. Any code, documentation, video, would be very helpful.

Thank you

Hey there Kayrox.

Depending on your needs there are several ways to do this.

The most technically efficient would be to use a Rigid Body animation.

Less efficient, should you need to have physics interactions is to simply add a force in the direction you want the object to fall, once it has met your desired conditions.

Thank you, but that wouldn’t be useful for me as it is an object in the scene that needs to be dynamic.

Good luck.

What they mean is:
No one uses physics in unreal to begin with (because chaos sucks). Pretending to simulate physics and obtain the same result from different scenarios is just outlandish, for pretty much any engine.

Best you can do is “not use physics”, which is the first answer you were given.

The next best option is “hope”. Basically, you hope that applying the same value to the sameobject at the exact same moment in game gime will produce similar results with as little margin of error as possible: good luck.

Your only option to have something that seems like physics would be to make it so that only the server does calculations and serves positions out to the clients.
Obviously, on simulated objects, even over a t1 line, this is not only impractical, but next to impossible to achieve with any smoothness due to network lag.

What AAA games do, is to allow the client to simulate (hoping it is somewhat accurate), then lerp or warp the object to the server given location.
When done correctly, the player won’t notice.
(Granted, the avarage player wont notice the room caving in under them, so you have a fairly low bar to reach ;p)

Deterministic physics using built-in Unreal Engine physics isn’t particularly feasible, even for single-player games.
Networked Deterministic physics using built-in Unreal Engine is essentially impossible.

Your options when using Unreal Engine physics are either “crank up the replication rate and accept that you’ll be using a bit of bandwidth,” or “accept that physics entities will snap/correct/jump around.” (This is really the same for Chaos in 5.x and for PhysX in 4.x, not that different.)

Your option when you really do need deterministic physics, is to write your own physics engine. Because you want it to be deterministic, you’ll probably want to build it based on fixed-precision math – use an int64, treat the low 16 bits or so as fraction, for example. You will want to develop an actor component that syncs the actor setup (collision volumes, initial position) into your physics engine on startup, and then syncs the simulation results back out after each simulation step.

Because time steps in Unreal aren’t fixed, you’ll also need to translate between the fixed time steps you’ll need in your physics engine, and the current variable time in Unreal used for rendering, typically using either some interpolation with virtual time lag, or some extrapolation. You’ll probably also want to run your physics simulation at pretty high resolution to avoid too much discrepancy here; 240 Hz could be reasonable. (Typically this means you simulate more than one physics step in each render step)

For certain gameplay elements, you’re going to have to make up your mind – do you do triggers and interaction volumes using Unreal physics still (so they’ll be slightly out of sync with your main physics simulation) or do you want to mirror all the Unreal events (overlaps and so on) into and out of your simulation?

People are saying it’s “impossible,” but that’s not true. If you’re good at developing physics engines, you can totally do this, and the structure that Unreal already has in place for setting up objects in editors can be quite helpful as scaffolding, so you can focus on all the things that you need to do differently. If you were to write a physics engine from scratch (which people do do,) you’d have to develop a bunch of things on your own that you can just re-use from Unreal.

It is, of course, at least a year-long effort, and probably longer if you want advanced features, so make sure you know that this is how you want to spend your time. And if you haven’t really dived into a physics engine before, expect to spend another year ahead of this just to read the few books that exist, and learn how they work. Maybe pick apart a couple of open source ones (Bullet, ODE, etc) to get a feel for what the design and interface paradigms are.

As someone else said above: Good luck!

3 Likes

Let’s not forget that you also then have to study up on net code and replication so that the simulations can be synced between all clients.

If you don’t, then your time spent developing custom physics withe the idea that it will work in multiplayer is going to hit a brick wall…

Also, lets remeber that Epic thought they could do better than Nvidia (or wanted to remove their licensing and what not), and they have yet to produce something that’s even remotely as functional as a 10+ years old clump of physics code… in almost 3 years.

They still have a performance differential of about 80% between physx and chaos.

With really no added benefit I can think of aside from really, really, expensive cost to have cloth-to-world interaction.

Can you do better than epic? Yes, definitely.
Can you do better than Nvidia? So far, epic wasn’t able to…

Ps: maybe AI can somehow be used/trained to calculate and provide the same result to all clients faster than the regular physics calculations can happen.
Simply because when you train it it then uses stored values to guess plausible outcomes…

1 Like

Your answer is good, but the main question here is: “Can you do better than Nvidia for your particular use case?” The answer depends entirely on what your use case is.

If the physics are reasonably simple, and the main goal is to make it deterministic across client/server and across CPU architectures, then it’s very possible to do better than Nvidia.

If the goal is “do exactly the same thing as PhysX does, but faster/better, on your own” then, well, no.

Btw, I remember when Nvidia bought NovodeX to make PhysX. That code is more like 20 years old by now :slight_smile:

2 Likes

Which makes improving on it highly probable, rather than Impossible.

Im sure Nvidia will whip out some AI shenanigans next, to which unreal will be forced to drop chaos and offer that as an alternative…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.