Synchronizing a simulation for multiplayer...

Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

Synchronizing a simulation for multiplayer...

Post by Zeal »

I found a good article here...

http://www.gaffer.org/game-physics/networked-physics

One general question though, how do you syncronize TWO simulations over a network. I was always under the impression that if you manually set the position of a rigid body, the entire universe would EXPLODE!

So a simple question - if a body on the client falls out of sync with the server, how can the client 'force' it back to its 'correct' position?

Thanks
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Synchronizing a simulation for multiplayer...

Post by chunky »

http://developer.valvesoftware.com/wiki ... mpensation

Also a good read.

Personally what I'm doing in my game is just warping my physics objects from where my client simulation thinks they are, a certain percentage towards the position that they're predicted to be based on the position and velocity when the server sent them, compensating for lag.

This won't work, since I stripped out a bunch of angle-related stuff and other unnecessary stuff for the example, but I hope it makes it clear:

Code: Select all

bool TWSpaceShip::setPosVel(float xpos, float ypos, float xvel, float yvel, int delta) {
// Delta is the time in milliseconds, compared to now, that the packet was sent. It's usually negative
	if(NULL == mShipBody) return false;

	btVector3 newpos(xpos, ypos, 0);
	btQuaternion newrot(btVector3(0,0,1), theta);
	btTransform newtrans(newrot, newpos);
	btVector3 newvel(xvel,yvel,0);

	btTransform currtrans = mShipBody->getWorldTransform();
	btVector3 currpos = currtrans.getOrigin();

	btVector3 predictpos = newpos - newvel * ((float)delta/1000.0);
	predictpos = predictpos * SPACESHIP_CORRECTION_LINEAR + currpos * (1.0 - SPACESHIP_CORRECTION_LINEAR);

	btQuaternion predictrot(btVector3(0,0,1), predicttheta);
	btTransform predicttrans(predictrot, predictpos);

	mShipBody->setWorldTransform(predicttrans);
	mShipBody->setLinearVelocity(newvel);

	return true;
}
SPACESHIP_CORRECTION_LINEAR, if set to 1.0, always warps the ship instantly to the considered correct position. at 0.0, it never warps it to a correct position, and 0.5 warps it halfway. Personally, I have it set to about 0.75 and that "feels right" for my game.

Gary (-;
Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

Re: Synchronizing a simulation for multiplayer...

Post by Zeal »

Wait is it safe to do that? Just manually set the transform brute force style? What happens if this new position overlaps with ANOTHER body in your simulation? Wont that cause both objects to go flying?
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Synchronizing a simulation for multiplayer...

Post by chunky »

Wait is it safe to do that? Just manually set the transform brute force style? What happens if this new position overlaps with ANOTHER body in your simulation? Wont that cause both objects to go flying?
On a client, temporarily: possibly. On the server: no, since I'm not doing that moving on the server.

Gary (-;
Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

Re: Synchronizing a simulation for multiplayer...

Post by Zeal »

On a client, temporarily: possibly.
So youre saying the worst thing that could happen is two objects overlap and go flying (at least on the client, the server remains the same)? But I imagine if you interpolate slowly this is unlikely eh? Hrmmm..

Am I the onlyone who has been told NOT to manually move bodies like this though? I was always told if you want to move a body, you MUST do so by applying forces, ect...
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Synchronizing a simulation for multiplayer...

Post by chunky »

Am I the onlyone who has been told NOT to manually move bodies like this though? I was always told if you want to move a body, you MUST do so by applying forces, ect...
No, that's common practice with physics engines and considered "right". Sometimes that simply isn't feasible
So youre saying the worst thing that could happen is two objects overlap and go flying (at least on the client, the server remains the same)? But I imagine if you interpolate slowly this is unlikely eh? Hrmmm..
Potentially. I figure I'm gonna cross that bridge if I get to it. It hasn't been a problem so far, through practical experimentation. If it becomes a problem, then I'll look into it...

Gary (-;
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

Re: Synchronizing a simulation for multiplayer...

Post by sparkprime »

I'm not sure exactly how it works, but directly moving objects could at least cause problems with contacts and hinges and so on, right?

It works for kinematic objects though. One thing I don't understand is how to do rigidbodies that are mostly simulated but also slightly kinematic and controlled by the system. Maybe not applying any impulses (even gravity and collision impulses) and setting the velocity explicitly each internal simulation step would work.

What I really need is a top-level guide to this stuff, Preferably some maths to make things clear.
Zeal
Posts: 47
Joined: Thu Oct 18, 2007 6:49 am

Re: Synchronizing a simulation for multiplayer...

Post by Zeal »

What I really need is a top-level guide to this stuff, Preferably some maths to make things clear.
I would also love to see a high level guide that explains this stuff, MINUS the math (for all us who are not CRAZY MATH NUTS :) ).
Wavesonics
Posts: 71
Joined: Thu May 22, 2008 8:03 pm

Re: Synchronizing a simulation for multiplayer...

Post by Wavesonics »

Just an idea, but lots of games use use very simple prediction code when they weren't getting updates from the server.

You could *only* run the simulation on the server, and do very simple client side prediction in-between updates.
jorjee
Posts: 7
Joined: Fri Mar 27, 2009 12:14 am

Re: Synchronizing a simulation for multiplayer...

Post by jorjee »

I'm not sure exactly how it works, but directly moving objects could at least cause problems with contacts and hinges and so on, right?
From what I know of bullet, the only safe way to directly move objects around is by using kinematic objects. If not, the dynamics of the world (collision response for example) becomes unpredictable.
One thing I don't understand is how to do rigidbodies that are mostly simulated but also slightly kinematic and controlled by the system. Maybe not applying any impulses (even gravity and collision impulses) and setting the velocity explicitly each internal simulation step would work.
If you do not apply any impulses, aren't you in effect making them completely kinematic objects? They are not affected by the world dynamics anymore are they?
Don't know if this is feasible; how about making a compound object with the parent as a kinematic object and the children as dynamic objects? The kinematic parent will be completely in your control but the dynamic children will be affected by the kinematic parent but also by the world dynamics.