Synchronizing Multiplayer Startup - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Synchronizing Multiplayer Startup (/thread-8902.html)
Synchronizing Multiplayer Startup - LegendofLink - Apr 27, 2011 02:26 PM
I'm currently developing a fighting game for the iPhone in my spare time, and I have most of the kinks in the basic engine worked out. Before I proceed with developing the mechanics of the game though, I would like to have a basic networking model functional. The main problem I'm facing in synchronizing the game state between two devices is the fact that the match does not start at the same time on either device.
Currently I am calculating latency between the devices and delaying the start of the match on the "host" device in an attempt to match when the other device receives the start message. Unfortunately, the latency rather wildly fluctuates between 10ms and 90ms, meaning that there is no guarantee that the games will start in sync even after compensating for latency. Does anyone here have an idea how I can accomplish this? Any help at all would be appreciated.
RE: Synchronizing Multiplayer Startup - Oddity007 - Apr 27, 2011 03:46 PM
If there's no server, then I imagine you could have both ends send a fixed time at which the two would be ready to start at to each other, then wait for the time that is the latest.
RE: Synchronizing Multiplayer Startup - LegendofLink - Apr 27, 2011 04:08 PM
(Apr 27, 2011 03:46 PM)Oddity007 Wrote: If there's no server, then I imagine you could have both ends send a fixed time at which the two would be ready to start at to each other, then wait for the time that is the latest.
I thought about that soon after posting my initial question, and tried it out. Unfortunately, I've found that there is no way to guarantee that the two devices' internal clocks will be in sync with each other.
I may have messed up in my math somewhere, so here's some code to look at:
The "host" does this:
If the devices' clocks were synchronized, this should work, but it didn't. If I made any mistakes here, please tell me.
RE: Synchronizing Multiplayer Startup - SethWillits - Apr 27, 2011 05:55 PM
I would go with your first method, and then you'll likely need to speed up or slow down one or both games (just a little bit each frame) to get them as close to in-sync as you can.
RE: Synchronizing Multiplayer Startup - OneSadCookie - Apr 27, 2011 06:03 PM
It's not clear to me that starting at the same wall-clock time helps you, but if it does:
A: this is "then"
A->B "tell me your clock"
B->A "this is my clock"
A: this is "now"
A: the clock value I just received from B is what his was at 0.5 * (now - then) ago.
(A can repeat several times and perform statistical analysis for greater accuracy if desired. It breaks down if the communication is *really* asymmetric, that is A->B takes significantly more or less time than B->A).
A: therefore the delta of his clock to mine is...