Realtime X Y positioning with physics engine

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Soko
Posts: 3
Joined: 18 Mar 2009, 03:31

Realtime X Y positioning with physics engine

Postby Soko » 18 Mar 2009, 03:44

Hello!
I'm having a bit of a trouble figuring out the following: Let's say we are making a soccer game with 8 players and with a quite robust collision/physics engine. So the x y of the players are constantly updating and the physics are calculated on the client side, so my problem is if there is the slightest latency in the x y updating, the physics will be calculated differently on each client when they hit the ball, and then the ball will be on different places. The solution would be if the physics would be calculated on server side but as the physics engine is written in as3 and it is quite robust that's not really possible.
Am i overlooking something and the solution is so easy that i just can't see it?
Thank you!
User avatar
axcho
Posts: 7
Joined: 16 Mar 2009, 15:48
Contact:

Postby axcho » 18 Mar 2009, 16:18

Send a timestamp along with the x and y.

Does your physics engine use variable timesteps or does time always increase the same amount every frame? If the latter, you can keep a variable on the client side that is the number of frames that have elapsed so far. When you send an update, include the current frame number.

When you receive an update, add the x, y, and time data to a list. Before your physics engine goes to the next frame, look through the list to see if there is an update for the next frame, and if so, apply the x and y update and remove that element from the list.

Depending on how your physics engine and update works, you might also want to prevent the engine going to the next frame until you've received updates from all other players about the previous frame.

Let me know if you have any questions.
Soko
Posts: 3
Joined: 18 Mar 2009, 03:31

Postby Soko » 18 Mar 2009, 23:44

Hi axcho!
Thank you for your reply. I am really curious how it will work in a real word situation. The problem is that if even just one player is laggy then it will cause latency in the other's game as well. I'm running some tests now and see if whether this can work.
Thank you!
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 19 Mar 2009, 07:57

Calculating the physics on the server side can be tricky and also quite heavy, when you have hundreds or thousands of games running.
I have seen a few attempts of server side physics and none of them was really usable. The problem is that the animations need to be smooth and fast on the client and it's unlikely that the network lag will help you in this field :(

Multiplayer real-time phsyics games are not an easy task, it's the art of the approximation and of error hiding, so to speak.
The most difficult part is compensating the network lag by using prediction algorithms etc... Entire books are written on this subject.

All I can say is take some time to research about these topics, and try to build a few simple prototypes that address each problem of your specific game mechanics.

Good luck
Lapo
--
gotoAndPlay()
...addicted to flash games
cobaswosa
Posts: 2
Joined: 17 Mar 2009, 14:33

Postby cobaswosa » 19 Mar 2009, 12:51

Does anyone know the names of any such books?

Thanks
User avatar
axcho
Posts: 7
Joined: 16 Mar 2009, 15:48
Contact:

Postby axcho » 19 Mar 2009, 15:22

cobaswosa wrote:Does anyone know the names of any such books
I do not, sorry.

However, a quick search has popped up the following potentially useful links:
GameDev.net -- Multiplayer and Networking
Architecture and Techniques for an MMORTS
Real-time multiplayer flash game: source code

Soko wrote:Hi axcho!
Thank you for your reply. I am really curious how it will work in a real word situation. The problem is that if even just one player is laggy then it will cause latency in the other's game as well. I'm running some tests now and see if whether this can work.
Thank you!
Hey, you're welcome. :) Could you describe exactly what physical objects are in your game, and what is being computed on each player's client, and what data is sent? (x and y position? or delta? of what?)

If both clients are computing the exact same thing, you'd want to be able to send updates that can be used to correct a simulation that has diverged, so you can run each client's engine without having to wait for an update every frame. To clarify what I mean, if you just send the change in x and y of a ball, getting an update after the simulation has diverged won't bring the clients back in sync. But if you send both the position and velocity of a ball, you could re-synchronize the two clients.

The problem is the situation where both clients are out of touch for a while, and then both players manage to hit the ball into their opponent's goal, according to their own simulations. Both of them think they've won. Of course, you can't bring both of those situations into agreement in any straightforward way.

In that case you could try making that part of the game itself, so if you detect that such a situation has occurred, you could make the game announce "Time Warp!" and have both clients roll back to the time in the simulation when they last were synchronized, and replay from there. :P Maybe you have a better idea.
Soko
Posts: 3
Joined: 18 Mar 2009, 03:31

Postby Soko » 20 Mar 2009, 00:48

Lapo thanks for your input however you have discouraged me a bit! :) Btw SmartFoxServer is amazing, really easy to use and offers a tons of great feature. Congratulations!

axcho
There are only two kinds of objects on the field, the players and the ball. As the players position will be provided by the other players, the only object that must be calculated is the ball. I was thinking that maybe the position of the ball could be corrected according to the first who joined the game but then that would be unfair because he would have no latency at all. Or another idea is that i send the position of the ball too and on server side i would get the average of all player's ball and send that back to everyone. I can't think through if it worked, i hope it won't open a blackhole in the space that pulls in my server. :wink:
You have given me a good idea again, about sending the linear and angular velocity too with the x y, that way i could precalculate some of the happenings.
Thanks again!
User avatar
axcho
Posts: 7
Joined: 16 Mar 2009, 15:48
Contact:

Postby axcho » 22 Mar 2009, 19:16

Yes, try those, I'm curious to hear how well they work. :)

And sending velocity is good. It's especially important to have that info so you can precalculate in order to hide lag.
aMUSiC
Posts: 48
Joined: 15 Dec 2007, 17:15

Postby aMUSiC » 26 Mar 2009, 11:00

I'm onto a very similar project and I've been thinking about coping with the network lag. The problem basically is that the clients can never be synchronized, and if they try to be jumpiness in the game will occur.

For instance, I thought of maybe keeping tabs of each connected client's latency on the server, and sending each state change message along with a number representing the total latency. The receiving client would then get x/y/velocity, discover what the ball's placement and velocity will be after X milliseconds and start rendering from that point. This would cause the ball to jump though..

Anyhow. .still looking into it

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 67 guests