Networking physics simulation

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Networking physics simulation

Postby JernejL » Sun Mar 04, 2018 9:03 am

I am wondering what approaches have you taken to network/online synchronization of your game that uses newton, for various reasons i cannot use tick sync, so i am interested in traditional approaches.

I synchronize body matrix for now and result is only good for tests, there is no smooth movement, i tried syncing velocity aswell, but had some really unusual and glitchy results, id probably need to convert this to use addforce calls?

How do you synchronize newton physical bodies thru internet?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby Julio Jerez » Mon Mar 05, 2018 6:19 pm

I have not consider networking at all.
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Networking physics simulation

Postby JernejL » Tue Mar 06, 2018 1:22 am

I found a bug in how i was syncing velocity, it looks better now.
It seems to work alright, but missed udp sync frames or uneven timing looks a bit messy, i'll experiment a bit and try to make a video of how this approach looks like (no interpolation or extrapolation)
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JernejL » Fri Mar 09, 2018 2:28 pm

This is not bad for velocity and matrix sync, but i think the body to body collisions are still long way to feel good:

Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby Julio Jerez » Fri Mar 09, 2018 4:43 pm

darn that's really cool.
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Networking physics simulation

Postby JernejL » Sat Mar 10, 2018 2:08 pm

Yeah and this was with just position + velocity sync, i have to figure out how to handle the collisions properly.. currently somehow cars go thru each other sometimes on collision which is really bad..

I think i will just turn off collisions on clients between cars, and when a collision happens on server, i'll have server take over for a short time and override what's on clients, depending on collision severity.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby zak » Sun Mar 11, 2018 7:16 am

When you set pos and vel, you have to call NewtonInvalidateCache() to have proper collisions, while i don't know if NewtonResetBroadphase() is to call. Julio?
zak
 
Posts: 58
Joined: Mon Dec 06, 2004 9:30 am

Re: Networking physics simulation

Postby JernejL » Sun Mar 11, 2018 8:52 am

zak wrote:When you set pos and vel, you have to call NewtonInvalidateCache() to have proper collisions, while i don't know if NewtonResetBroadphase() is to call. Julio?


That is only if you use tick sync, i dont use tick sync input, but sync bodies and their movements.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby Julio Jerez » Sun Mar 11, 2018 9:19 am

in newton 3.14 this is a lot more critical than is was in 3.13 an lower.
in previuos versions each collsion pair was tested for obb to see if they required contact calculation.

this was problematic for CCD and for irregular shape object it is an expensive operation if you want to be accurate, else it end up calling for many positive false which is bad.
At best the test was of the same cost of more expensive than doing the contacts calculation.

now what it does is that when the broadphase detect two bodies are potential collider, it generates the contact joint unconditionally. this generates more joints of course but this is not expensive if they are handled with a good data structure.

the broadphase can detect new pairs but it can't remove pairs, so two new phases new to newton were added. other engines do not need this because they generate pair or validate pair per frames.

in newton 3.13 and lower the method was validating pairs but I found that method too simplistic and also more expensive.

what newton does now is that for each generated pair, it call calculate contacts, and the contact calcuation is set that is always report the closest distance between the two bodies.
on initialization the pair distance is set to zero, to inforce a call to contact calculation the first time the contact joint is created.

then after that for each pair it does a conservatiove integration of the relative velocity to see if they will intersect, is this trick that makes it work for cvs seamlessly. if they do, a call to collision is made to update the distance and possibly calculate contact points. This way pairs are only checked when the distance fall below the threshold considered too close for collisions, if the distance grows, then the pair is deleted.

The moral is that is you change position or velociy, the collision will malfunction because the broadphase does not know about this and will use an older distance.

invalidate cache delete all contacts joints, so it should take care of that. no sure if I verify that is actually doing so. maybe what it needs is an incremental invalidate cache like it does now when a pair is validated.

The cool thing about this new method is that it can do trivial rejection even if a body is inside of a building, something that is impossible when using obb test.

I will check out if invalidate cache is doing the proper thing, but most likely is not since like I said it work by reseting the world.
Invalidate cache is more for initializing a world for deterministic results.
If you do not need determinism, maybe a functions like NewtonSetMotionState is more appropriate because that will not delete the contact, instead it will just recalculate the joint distance.
The engine already does similar things, for example there is Setmatrix not sleep, which is used in the vehicle to extrapolate wheel tire collision without changing the broadphase.

Anyway try invalidate cache, and tell me if this is good enoghut, because moving the object most certainly will create a malfunction sooner or later.
Julio Jerez
Moderator
Moderator
 
Posts: 10954
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Networking physics simulation

Postby JoeJ » Sun Mar 11, 2018 10:29 am

Some ideas i'd try...

Use (decent) external forces to sync, but no direct velocity / position changes, (exception maybe for offscreen stuff.)

Process collisions on the client as usual, end sync only their non physical effect with server, e.g. changes in damage, health or score. Server could reject changes if it does not agree with them for some reason. E.g. driving over a human but failing to hurt him may be better than teleporting stuff around.


Anyways it's very interesting, so please keep us up to date... :D
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JernejL » Mon Mar 12, 2018 3:55 am

Ok, taking that into account, it all makes sense that if a body is not moved correctly via forces it will end up embedded in a car due to body teleporting to location and due to difference in synchronization on different clients.

Because this is synced via udp, i cannot just sync force and torque calls, because packets might be missing - so each synchronization step will have to individually calculate fore and torque to immediately move body to intended position in 1 update. So i suppose what i need to do is following:

- Calculate force and torque needed to move a body to proper place
- Apply that force and torque
- Override body velocity to correct one (so the body doesn't go crazy, and continues with its original velocity as was intended to be during synchronization step)

So, how is this doable? How do i calculate force and torque to move the body to intended position, this is a variant of inverse kinemarics maybe.. how would you suggest i approach this?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby JoeJ » Mon Mar 12, 2018 5:38 am

You can start with the methods we discussed here: http://newtondynamics.com/forum/viewtopic.php?f=9&t=9214

JernejL wrote:- Calculate force and torque needed to move a body to proper place
...
- Override body velocity to correct one


You could do both at once. Knowing target velocity and position, a naive approach would be to use half target velocity, and half of the velocity that would move position to target in some time like 0.2 seconds.
Advanced approach would be to set up equations of motion from existing state to the future, and from target state to the past using some max correction acceleration. Then intersect both trajectories so positions and velocities are equal and look at the resulting acceleartion telling you about forces. Math is a bit involved, so i hope (and believe) some naive approach is good enough.

Goal would be to allow a difference between client and server, but to have a bound it does not get too large, and also the correction forces should be pretty unnoticed.

Additionally you would need to predict those target values from a timestamp from the most recent package and eventually smooth it a bit.

Of course you could use something like kinematic joint instead of forces and torques as well.


(It's not that easy to control physics, so don't expect perfect results quickly. Starting with something like a picking mechanic (gravity gun) would be better because you feel yourself how the lag is, how things start to oscillate etc.)

EDIT:
We may overthink this! If you sync just position, velocity must be in sync as well, right?
(I often implemented ways to control position and velocity, but i always end up working with just position :wink: )

Also, all i say is about syncing things that are not very different.
If, however, somebody outside a house throws a stone at a wall corner, it may fly left on the server and right at the client. If the stone is an important game object and must be kept in sync, i have no idea how to do this nicely other than teleporting it while the player hopefully looks away.
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Networking physics simulation

Postby JernejL » Mon Mar 12, 2018 8:29 am

I use the "Immediately stop a body" approach to reach desired velocities and it works well, however i'm not sure how to calculate a needed velocity to move the body to a specific location, if you can explain a bit further how this all relates and how the force can be calculated to have body moved to exact spot, i can probably make a few prototypes easily.

I sometimes lack general physics knowledge, but i can apply math to a problem once i know what to do :)
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Networking physics simulation

Postby Shaderman » Mon Mar 12, 2018 8:38 am

What about the good old client/server model? You wouldn't have to mess with Newton itself which sounds like a painful approach.

- One of the hosts acts as server (or use a dedicated server if possible) and does all the Newton stuff
- Client input (mouse/keyboard) is sent to the server
- The server distributes just location and rotation to the client(s) which is used by the client(s) to move the objects
- To make objects move smooth, client side prediction/interpolation/extrapolation can be used.

AFAIK this is the most common way for network multiplayer games.

If you're not familar with such a client/server model, you might want to read sites like

http://drewblaisdell.com/writing/game-n ... with-pong/
https://gamedevelopment.tutsplus.com/tu ... edev-10074
https://gafferongames.com/post/what_eve ... etworking/
Shaderman
 
Posts: 66
Joined: Tue Mar 08, 2016 2:51 am

Re: Networking physics simulation

Postby JernejL » Mon Mar 12, 2018 8:53 am

I am making a client / server model, but to move bodies i am setting matrix - which causes bodies to tunnel thru other cars, to move them i need to do it via forces, this is what we are discussing how to do now.

I have to warn against complete server-side driving: There is little sense to have server-side car physics for each client driving due to packetloss and uneven packets, this ruins game experience a lot.

A great example of how bad this can be is Realtime Worlds's APB game. Example complaints = http://steamcommunity.com/app/113400/di ... 817147002/ ) I allow each player to do his own local driving, and just sync that back to server, and then distribute to other clients (with data sanitization checks to see if they deviate too far from what is possible)

The main problem remaining is just the issue of setting body matrixes directly (i need to do it via addforce).

Maybe i could just go crazy and make a "network" joint and apply simple linear rows? - would this be feasible?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1441
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest