Server/Client Application using Box2D

Hello,
I'm developping a kart game on flash, with box2D as physic engine. And for now the game works like this:
-Each client hosts a physic engine that compute the collision and so
-Every 30 ms (about) each client send their positions to the servers which send it to everybody
-When receiving the positions packet, the client update the kart's positions

My problems are:
-The collision is not centered on one machine, implying error and jerky reactions sometimes (when two kart collide)
-The kart from others do not move fluently, but in a discret way. Due to the fact that the client just update the position and do not compute the driving or so.

So I was thinking, in order to fix some problems, that I could host the physic engine directly on my server (which is implemented in java btw). And my game would look like this:
-Each player send the key pressed/released to the server
-The server update the physic world engine with it
-The server send to everybody the new positions

Problems expected from that:
-Server overflowed by computation and request, causing lag.

So what do you think? Should I change my system or just rearranged it? And with what?

Thank you for your help
There have been a couple discussions on networking the engine, you can search for those.

I've done the following things and it works pretty well:

* create bodies on a certain set frame number (when everybody starts together, have each game start counting frames). So game A can say to everyone "create this guy on frame 50" (which is hopefully sometime in the future) and all the games will be able to create the body at the same time.
* have a constant timestep.
* Have two types of sync messages, one that's linear velocity, and another that syncs the position, linear velocity, and angular velocity. You can use these however you want, but I would run the first sync messages somewhat often, and then second kind every once in a while. The second ones can make things look jittery. Also, definitely look for lag or other discrepancies between one game engine and the cloud to know when to send more messages.

For the sync messages, you should only send out messages for each computer's "own" objects, as you don't want two computers caught constantly correcting eachother.

In general, if you can keep the same timestep and spawn the objects on the same frames, you should have everything pretty well synced. The messages are mainly for inevitable network lag or slowdown of a machine.
Hello, and first thank you for helping me.

For your answer, I don't understand the point to create every single body at the same frame for every client?
About the timestep, I'm using a constant one.

And about the sync message with linear/angular velocity, I really doubt of its efficiency. Because let's picture it:
-Client A send it's velocity: 10m/s at time "t0" and send velocity:0m/s at "t1"
-Client B catch the first package with 10m/s and update the kart A and do not catch the other one, so the kart A still have a velocity at "t1" and end in a different place. I am wrong?
And when do you trigger the emission of this message (linear/angular)? Each time the velocity change? And when do you decide to send more messages (when there is lag or inversely when there is no lag)?

Quote:
For the sync messages, you should only send out messages for each computer's "own" objects, as you don't want two computers caught constantly correcting eachother.


I don't really understand what you are meaning here, can you explain to me a little bit with an example or so?


And finally, why don't you store the physic engine on the server?

Thank you very much
Silo wrote:
why don't you store the physic engine on the server?

This would cause a whole lot of strain on the server, especially because it constantly has to send out position data so each of the client computers can have a somewhat smooth experience. My suggestion would be not to do this, and just have physics run on all client games. But this requires the engines to by synced.

Silo wrote:
For your answer, I don't understand the point to create every single body at the same frame for every client?

Not all on the same frame. Like this:
Computer A wants to create a ball on all game instances. It also knows the max network lag of the system is, say, 50ms. Lets say the current frame is 4000, so it sends out a message to all the other computers to create a ball (that is "owned" by computer A) at frame 4005 (30fps, .05 seconds lag, means about 1.5 frame delay). Computer A will also create its ball on frame 4005. Thus all engines will add Computer A's ball at the same time.

That's a pretty key concept to keep games synced, and it doesn't only have to apply to creating physics shapes. Say, now in the context of your game, you tell your car to move left. Well, that message to move left will take some lag to get to the other machines. So we do the same thing, tell everybody (along with yourself) to move your player left at frame 4005.

As for the sync messages:

First of all, remember how that ball/player was "yours"? For all the important game objects, you should be keep track of who owns what.
The sync messages do seem to mess things up, but even if you perfectly sync all physics operations between machines, there is bound to be small discrepancies from receiving messages late, or machines slowing down, etc. That's where you need some way to tell another machine where all "your" objects are.

There are many different strategies for this, and I just had outlined a simple one. Here's another, more complex way, that tries to account for the network lag:

Have "check" message, where you send computers each of your object's position, velocity. When you receive a check message you compare the position and velocity difference.
* If the check's velocity is in a way different direction than yours, then there was probably a collision, so don't do anything and wait for the next check message.
*If the check's velocity and position can somewhat accurately predict the current position of the object (you know the frame # that the message was sent, so you should be able to interpolate where the object will be), then don't change anything.
*If the predicted position from the message is far off from your position, and velocities match pretty well, then sync to the predicted position.

But this method can have problems if the message's frame # is way below the engine's current (lots of network lag), and you can get inaccurate interpolation, and have jumpy objects.

Basically, the sync messages are where you can get creative, and are probably the hardest and most tweakable part of the system.

Silo wrote:
And about the sync message with linear/angular velocity, I really doubt of its efficiency. Because let's picture it:
-Client A send it's velocity: 10m/s at time "t0" and send velocity:0m/s at "t1"
-Client B catch the first package with 10m/s and update the kart A and do not catch the other one, so the kart A still have a velocity at "t1" and end in a different place. I am wrong?
And when do you trigger the emission of this message (linear/angular)? Each time the velocity change? And when do you decide to send more messages (when there is lag or inversely when there is no lag)?


The method I had outlined before was simple, but wasn't the best. It didn't account for the lag of the network.
The emission of the messages depends on the strategy that you use. For the strategy above, I would send out "check" messages staggeringly throughout "my" objects. I'd play with the quantity of messages per second, but probably aim for send out a check message for each object every 2 seconds or less.

I hope that helped, what do you think?
Also, instead of using frames to specify when to use messages, using the time (since the game started) would eliminate problems of slow computers with reduced framerate
Thank you again, you definitively help me out with this problem.

I really like this solution:
Quote:
Not all on the same frame. Like this:
Computer A wants to create a ball on all game instances. It also knows the max network lag of the system is, say, 50ms. Lets say the current frame is 4000, so it sends out a message to all the other computers to create a ball (that is "owned" by computer A) at frame 4005 (30fps, .05 seconds lag, means about 1.5 frame delay). Computer A will also create its ball on frame 4005. Thus all engines will add Computer A's ball at the same time.

But you are speaking like it's not a really good idea, can you tell me what are the issues of this implementation? Because I really think about using it for my game. If everybody make action at the same frame, how can it be wrong or different in every clients?

The second solution seems to me, too much work to implement for not so good result.

And in your case, you're using the first idea right? And does it work well? How often do you send the linear/angular velocity and the positions packet?

Really thank you for your time and help, I really appreciate it.
Silo wrote:
I really like this solution:
<snip>
But you are speaking like it's not a really good idea, can you tell me what are the issues of this implementation? Because I really think about using it for my game. If everybody make action at the same frame, how can it be wrong or different in every clients?


I'm sorry I was being very ambiguous, when I said "before" I meant the previous post. That whole section is a good idea. I think you should use both methods -- physics calls to the same frame/time, and the sync messages.

Quote:
The second solution seems to me, too much work to implement for not so good result.

And in your case, you're using the first idea right? And does it work well? How often do you send the linear/angular velocity and the positions packet?


When I used this, I was able to get decent matchup between systems by using the velocity packet every 2 seconds and the position every 5 seconds. but it was a little jumpy sometimes, because of the network lag with the position packets. This was before I scrapped my network layer for my engine, which I'm now rebuilding. When the sync messages are done (again), I'll probably go with something closer to the second strategy.

So again, you would want to both
*synchronize your physics calls (same frame/time)
*have some sort of sync message
Ok thank you for all these informations. I will implement them and come here tell you what's going on.

But how can you send the velocity each 2 sec, positions each 5 sec and still have a non jerky game? Do you also send the key pressed/released? Because how can the kart knows what to do between each velocity or position packet?


Thank you again
Silo wrote:
But how can you send the velocity each 2 sec, positions each 5 sec and still have a non jerky game? Do you also send the key pressed/released? Because how can the kart knows what to do between each velocity or position packet?


keep in mind those were for the sync messages. The cart (on all computers) should also be receiving your move/steer message like this:

toucansam wrote:
Computer A wants to create a ball on all game instances. It also knows the max network lag of the system is, say, 50ms. Lets say the current frame is 4000, so it sends out a message to all the other computers to create a ball (that is "owned" by computer A) at frame 4005 (30fps, .05 seconds lag, means about 1.5 frame delay). Computer A will also create its ball on frame 4005. Thus all engines will add Computer A's ball at the same time.

That's a pretty key concept to keep games synced, and it doesn't only have to apply to creating physics shapes. Say, now in the context of your game, you tell your car to move left. Well, that message to move left will take some lag to get to the other machines. So we do the same thing, tell everybody (along with yourself) to move your player left at frame 4005.


That describes frame-(or time)-specific messages. Keep in mind that these messages and sync messages are two different things.

Original thread: https://box2d.org/forum/viewtopic.php?t=4009