Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Best solution for a pong game  (Read 1337 times)
0 Members and 1 Guest are viewing this topic.
Offline Breadstick

Senior Newbie




while(true) me.doAction(Act ions.PWN_ALL);


« Posted 2004-09-21 06:47:50 »

Hey fellow java people.  I have a multiplayer pong game, and it's network layer completely written and working.  However, I'm new to networking games, and I'm not sure what the best method is to keep the games in sync in a real-time game such as pong.

When connected to a friend in the same state as me, I'm able to relay about 40 packets of data per second, each containing a serialized String of length 4 (11 bytes of data).

When the ball bounces off of a paddle, the angle at which it will travel in the opposite direction is dependant on the speed at which the paddle was moving when the ball hit it.

Does anybody have any thoughts as to what the best way to keep the two players' games in sync would be?  I'm not sure what sort of data to send to the other player, and when to send it.  Also, even if using 1.5, or gagetimer, will timing be inacurate if the computer clocks on each computer are not perfectly in sync?

Thanks,
Breadstick
Offline crystalsquid

Junior Duke




... Boing ...


« Reply #1 - Posted 2004-09-21 12:18:25 »

If the ball movement is perfectly predictable (no random numbers used), then all you really need to transmit is the position & velocity of the opponents bat at the point of contact. presuming the bats only move in 1D, the time of the colliosion must be at a set point - given the balls current velocity and direction, there will be a specific time when it reaches the x coordinate f the front of the bat. the trick will be to have both players with the feeling the game is 'responsive'.

One way to do this is have each players client being responsible for the ball movement from the point the other player hits it until they hit it back. That way they get to play 'perfectly' until it is the other players turn, at which point you send the start position & velocity of the ball to the other player and let them handle it until it returns. The player without the ball sends his paddle positions to the other, but this is merely cosmetic to let the other player see roughly what is going on.

Sychnonising a network clock is non-trivial. if you can avoid it, do. Otherwise, google for more details. Essentially you have to measure travel times between the clients and work out which messages had a 'good' round trip (very little delay) and use these to set the clock, ignoring any timer messages that were slower than average.

Hope this helps,

Dom
Offline Breadstick

Senior Newbie




while(true) me.doAction(Act ions.PWN_ALL);


« Reply #2 - Posted 2004-09-21 18:22:58 »

Thanks for the tips.  One idea I had was that when one player had to update the other player, they would send the current system time to the other player, along with the speed and velocity of the ball in that players game.  When the other player received this data, that player would calculate where the current possition of the ball should be based on the ball info the other player sent, and how many 'ticks' have passed since the time that was received.

I wasn't sure if I could handle this accurately though.  Is it safe to assume the the two players' system clocks are synced?  If not, what can I use as a reference point for time? I was thinking about using some sort of key frame system, but wasn't so sure if that would be the best solution here.

Hope that wasn't too confusing...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline crystalsquid

Junior Duke




... Boing ...


« Reply #3 - Posted 2004-09-21 19:40:40 »

As th ball has a fixed equation describing its motion, you should be able to sync without worrying too much about time. Anyway, thats an extra complication for now Smiley

To sync a clock, you basically decide which client is going to run the 'master clock' This 'resets' the clock by storing the current time. Thenthe game time is 'currentTimeInMillis - startTime'.

The other client must ask for a time update, sending a request number with the message, and recording when it sent the message 'msgTime' and what number it used.

The master clock respons immediately to the message with the current game time (currentTime - startTime).

The other client receives this, and matches it to the stored sent request (using the request number I mentioned earlier), also noting the time it received the message 'recvTime'. Note all client times can be done with the currentTimeMillis as we are going to be taking differences.

The approximate 'game time' for this client is then:

   approxGameTime = receivedGameTime + ((recvTime - msgTime)>>1);

This is adding on half the round trip time (RTT).
However, that is not the end of it. You have to account for variations in the transmission.

To do this:

For the first timer message received just set gameClock and store RTT as 'bestRTT'

For each subsequent clock update:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
RTT = recvTime - msgTime;
if( RTT < bestRTT )
{   // Best result so far, average this with current clock
     gameTime = (approxGameTime + gameTime)>>1;
     bestRTT = RTT;
}
else if( RTT < bestRTT + (bestRTT>>2) )
{  // The RTT was pretty good, but not great
    // Take a 1/4 weighted average
     gameTime = (3*gameTime + approxGameTime)>>2;
}
clockOffset = gameTime - System.currentTimeMillis();


then the client has an offset he can use to get the game time:

1  
2  
3  
4  
long GetGameTime()
{
    return (clockoffset - System.currentTimeMillis());
}


This is a simple way of getting an OK shared clock.
To do it properly, you should measure the standard deviation of the RTTs, and use a weighted average based on this (instead of the 25% of best RTT I use above). This will give a better update behaviour over a noisy network than the scheme here, but this should work ok for a local area network or a good internet connection.

Apologies in advance for typos, syntax errors, & getting my +/- signs the wrong way around Smiley

Hope this helps,

- Dom
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (20 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (30 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!