Sign in to follow this  

how to synchronize the game??

This topic is 4804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, im working on networking my pong clone. im only doing this in preperation for networking my 2d action / RPG. anyway, this is what i got so far: -both the client and server runs the game logic (moves the ball, checks for collisions, etc...) -every 10 MS, the client tells the server where his paddle is, and the server sends this to the other client -every 50 MS, the server sends the client where the ball is, and its velocity this works fine if theres no lag. if the person has 200 ping or so, the game is un-playable. does anyone have any tips for synchronizing the game, and make it run smoothly at that? i cant seem to find any articles really about this, and ive checked out the article section here... one thing i'd like to note.. im using rakknet, which automatically synchronizes time on all machines. this means i can time-stamp a packet, and on the recieving end just do getTime() - timestamp, and it tells me how long ago this packet was created. but i dont know how to use this exactly for my own purposes... im using time-based movement... should i add this time difference into the delta time between frames when moving everything? or do i use this time to calculate where the ball should be exactly? im not sure, and any small examples would be greatly appreciated. thanks for any help.

Share this post


Link to post
Share on other sites
The fact that Raknet syncronizes the time for you makes things very easy. In your situation I would, upon receiving a position update packet, use the timestamp to calculate where the object should be relative to the position inside (assuming the current velocity is maintained) the packet and move it there, setting the velocity etc. Presumably that should not be too off from where the object already is on the client.
Note: in games where multiple planes of movement exists a common scheme known as “dead-reckoning” can be used to predict an objects position along a natural curved path based upon its recent changes in position/velocity.
There is no need to send the ball’s position ever 50ms. Unless your pong clone is somewhat warped, the ball will follow a direct and 100% predictable path until it hits a paddle. Therefore, it is only necessary to send a ball position / velocity update upon impact with one of the two paddles. You mentioned that the game logic runs on both the client and server – from an impact with a paddle you can be guaranteed that the ball will not change position/velocity in a way that conflicts with the game logic on the client until it hits the opposite end.

Hope this makes sense,
Jackson Allan

Share this post


Link to post
Share on other sites
I agree. Pong is very easy to network, because the latency between interactions is usually greater than transmission latency. The server just needs to tell the others if the ball was hit or missed (and in what direction); anything above that is just for looks :-)

Still, sending the ball every 50 milliseconds is not that bad, because if you miss one packet, you will just get the next packet and life will be good. If you send only authoritative state transitions, then they have to be sent using reliable means, which may cause jitter because of re-transmission (and ordering, should the game be more complex than pong :-)

Share this post


Link to post
Share on other sites
hey guys...thanks for the replies. i think i got it working pretty smoothly..i tested it out with a friend who had a little more then 200 ping (hes on a modem), and it ran pretty decently for him and good for me... the way i do it is send out the player's paddle position if it has changed and 10 ms have passed since i last sent one. also, the client doesnt do any logic on the ball anymore - he just renders the ball, thats it. every 50 MS the server broadcast's the ball's position. the server moves and does collision detection on the ball. when the server sends the ball's position, he timestamps the packet and on the receiving end i do time - timestamp, then put the ball in his place and add that amount that it would have moved in that time (vel * time).... anyway, it seems to work pretty smoothly. i know its probably bad for me to not do the logic on the client, right? but it seems to move smoother if the client doesnt move the ball or detect collision...

i think it might be time to start working on networking my 2d action / RPG... im going to switch to using the mouse for movement, so that then i only have to send out a packet each time i click to move (heres my pos, heres where i clicked). i was thinking for bullets and such, i would do a method similar to how i do the ball in pong. make a BULLET_FIRED packet, and send it when a client fired a gun... have it include starting position and a timestamp. when another client gets it, he knows how long ago the bullet was really fired... so when he creates the bullet on his machine, he can "scale" the velocity by however much is needed factoring in the time. i put "scale" in quotes because im not sure thats the proper term for it, im really bad with math. in fact, how would i do this calculation to calculate velocity of a bullet when your given start pos, and time since it was fired? basically the idea is to make up for lag, a bullet will travel faster on someone's client if he has a slow connection, but it should appear smooth to him, and it should hit the target on his machine as it does on the other clients. is any of this a good idea or what? thanks a lot for any help.

Share this post


Link to post
Share on other sites
No logic on the client? Sending the balls position every 50 milliseconds is not the best – but if it works for you it may as well remain.
Quote:

but it seems to move smoother if the client doesnt move the ball or detect collision...

Not sure why this would occur – did you notice this while running two instances of the client and one server on a single machine? In my experience this can lead to jerky gameplay problems as the simultaneous game loops fight for CPU time (one instances tended to dominate while the others ran slowly). I improved, but by no-means solved, this issue by inserting ‘Sleep( 0 )’ at the end of my game loop, thereby allowing other threads CPU resources to complete what was needed.

Quote:

i was thinking for bullets and such, i would do a method similar to how i do the ball in pong


Be sure not to have the server update the bullets on the client machines though – all you need is one packet describing the bullet’s initial position/speed and the client can handle the rest. I do, however, think that you’ve already figured that one out.

Quote:

so when he creates the bullet on his machine, he can "scale" the velocity by however much is needed factoring in the time.


Whoa – that seems like an odd way to go about it. Generally you want to synchronise the game on two machines, not have projectiles shooting around the place at different velocities etc. This will just lead to a lot of misleading hits at long distances and unpredictable behaviour, not to mention the alternating speed effects noticeable to the player. Creating a bullet at original_position + velocity * ( time – timestamp ) on the client side is how one would usually go about it – just make sure there are no obstacles between the position broadcasted in the packet and the projected position. This works well enough – I play Subspace with a ping of 320-400 and the bullets appearing slightly out from enemy spacecraft is no real problem. The fact is that changing the velocity of the bullet to make up for lost time will simply add to the problem. :P

Anyhow, my apologies for my delayed response,
Jackson Allan

Edit: Did you decided to give Enet a miss?

Share this post


Link to post
Share on other sites
hey jack, thanks a lot for your reply. ok, maybe that was a bad idea that bullets would go faster.... but, i figured this would guarentee that the bullet would hit the target at the same time on all machines, and i figured a bullet moving faster would look better then a bullet starting out in a position further away from the gun... but, your probably right. thanks again.

yeah, i did stop using eNet, im using Rakknet now instead. its higher level and has some nice features. Rak'kar is constatly updating the API and helping his users out in the forums, which is one of the main reasons i switched. eNet had a mailing list which seemed dead.

Share this post


Link to post
Share on other sites

This topic is 4804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this