4

i've searched and read many articles and looked at the GKTank apple sample (can't see any specifics about my problem, seems to process events as they occur) but i can't see a sample or tutorial that seems to answer the following

in a multiplayer game over bluetooth/internet (maybe) how do you synchronise player interactions so that the correct game outcome occurs taking into account latency etc ...

for example:

A button is displayed to each player A & B on two separate devices connected via bluetooth

Player A presses the button (Player A is hosting the game and so latency is not an issue) Player B presses the button just before Player A but his connection to Player A's device has at least a 200ms network latency

problem: the game needs to know Player B pressed first even though Player A's touch request would reach the game code first - i.e. no advantage to the hosting player.

i know the touch event has the timestamp and so i could ensure that the actual time of the press was sent from Player B to the game code... but not sure if thats the right approach and if it is where to go from there..

i expect the answer lies in some game time cycle where touches aren't processed immediately but within a game cycle...

any help on this or pointers to tutorial or specific source code that handles this would be appreciated.

adam

2 Answers2

2

You might want to ask this question on https://gamedev.stackexchange.com/ , as it's a general question about multiplayer lag or latency, and not particularly specific to the iPhone.

You might be able to use timestamps to order the action messages. I think iPhones are synchronized to a date time server maintained by AT&T.

Community
  • 1
  • 1
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
1

Well I have no actual experience with creating multiplayer games. But as is the case with most game developement questions, I suppose there is no wrong or right, so some logic thinking should do the trick.

Here are some thoughts you might want to consider:

  • Even if no latency is occuring on Player A's device, you'll have to introduce some to compensate for player B (and vice versa, since from Player B's point of view Player A is late too)
  • Thus, you'll need to introduce some kind of a "command stack" to buffer input from both players and execute the commands once both Players had the opportunity to contribute their inputs for a given point in gametime
  • You may want to introduce ping commands to measure the actual latency from time to time
  • Only one Device (the host) should be measuring the latency and announce it to the client
  • Based on the measured latency, calculate a time offset (relative to the hosts time) or delay for the commands in the stack (use the latency to convert Player B's timestamps to "local" time)
  • Keep the delay dynamic if possible, to compensate for varying latency (keep measuring latency throughout the session
  • If the actual latency peaks beyond the calculated one, commands from Player B may be put onto the stack late - make sure they will be executed anyway (lag may be experienced at this point)
  • You'll actually need two "layers" - the input layer, which exchanges, buffers and synchronizes the actual input, and a game layer, which receives the delayed commands from the input layer

So far, my 2 cents ;-)

Toastor
  • 8,980
  • 4
  • 50
  • 82
  • thanks for the comments - pretty much along the lines i was thinking although i hoped someone else might have already done the donkey work logic as it must be present in all multiplayer games across devices. i guess i'll have to work through it myself. – adam pinder Jan 13 '11 at 10:11
  • Thanks to the www, you'll find code for almost every purpose, but especially with games it won't be easy to use it most of the time - at least that has been my experience so far, because game code is delicate. Available code may not fit "as is" and will force you to do a lot of rewriting, sometimes up to the point where you would have been better off writing your own logic from scratch. The alternative would be using an engine... That being said it is still possible that you'll find just what you need somewhere, but unfortunately I have no hints for you as to where to look for it... – Toastor Jan 13 '11 at 10:34