Synchronizing Multiplayer Startup

Nibbie
Posts: 2
Joined: 2011.04
Post: #1
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.
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #2
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.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2011.04
Post: #3
(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:
Code:
NSTimeInterval delay = 2;
NSDate* gameStart = [NSDate dateWithTimeIntervalSinceNow:delay];
message.startTime = [gameStart timeIntervalSince1970];
[self sendMessage:confirm ToSession:session];
[NSTimer scheduledTimerWithTimeInterval:delay repeats:NO usingBlock:^(NSTimer* t){
  //start game
}];
And the device on the receiving end does this:
Code:
NSTimeInterval delay = message.startTime - [[NSDate date] timeIntervalSince1970];
[NSTimer scheduledTimerWithTimeInterval:delay repeats:NO usingBlock:^(NSTimer* t){
  //start game  
}];

If the devices' clocks were synchronized, this should work, but it didn't. If I made any mistakes here, please tell me.
Quote this message in a reply
⌘-R in Chief
Posts: 1,248
Joined: 2002.05
Post: #4
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
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...
Quote this message in a reply
Post Reply