""battle pong almost complete ..... need help"

Started by
7 comments, last by SpaceDude 19 years, 7 months ago
Hi, (I'm beginner ). I'm making PONG that can be played in internet.Till know I have made it to be played in LAN. I want to make it such that this game should be played by two players in the internet. So I've some doubt here, Till now , I'm using game state concept(whenever the state of the ball or the paddle changes I send the new position to the client). But different machine have different frame rate, so, in some machine the ball is moving fast and in some its move slow.Due to which the update in the client side is not occuring simultaneously. Actually,when the ball hits the wall or paddle its state changes and then I'm sending the new information to the client. After getting correct information its move of its own (by performing some calculation). Can the ball movement ,paddle movement(by the player) shown almost together in both server and client side? so plz help it's almost complete.
Advertisement
Two things:

1) Separate out your logic (moving the ball, collision checking etc.) from your display code, and run the logic at a fixed frequency (say every 20ms)

2) Insert a constant delay between an input event being received (the user presses the 'up' key) and the action being taken (the paddle moves upwards). This delay will help hide network latency issues. (Obviously you still send the event straight away, but the idea is that by the time the delay is up, both computers will know about the event and there will be no need to pause gameplay while the two programs resynchronize).
Here's how I'd do network pong:

0) Use UDP.
1) There are three moving entities: paddle A, paddle B and ball.
2) One host is the "server" and receives data on port X.
3) Both hosts are "clients" and send data to port X.
4) Clients send packets saying "I want *my* paddle to be at position Y" every 50 milliseconds.
5) Server receives these packets, applies them to the game logic, and send out updates where the paddle and ball all are, as well as the velocity of each, every 50 milliseconds.
6) Each client, when receiving the updates, puts the enemy paddle and the ball in the positions/velocities that it says in the packet, but keep the player paddle at the location it thinks it should be in.
7) Each client runs the game logic, in addition to the server. This means that the client can run at 100 Hz, and get a smooth frame rate, even when there's only 20 packets per second.

The size of "here's my paddle position" is probably about 16 bytes (client ID plus timestamp plus position plus velocity) plus UDP headers.
The size of the broadcast packet is probably about 36 bytes (timestamp + 2 paddle positions w/velocities + ball x/y with velocity) plus UDP headers.
You can play this on a 28.8 kbps modem!

Note that if one player gets out of sync, he may hit the ball on his machine, but the server thinks he's elsewhere, so the ball will get put back behind his paddle. That's life in a networked world.

There are things you can do to improve this, such as keeping a log of where you were in the near past, measuring round-trip times, and forward extrapolating to the current server time for display; also, applying correction to the player paddle "in the past" and keeping a delta from the last known position, rather than an absolute position. Updates could also be "soft" so that discrepancies are healed over 250 milliseconds or so instead of immediately.

If someone has 200 ms latency ("ping") it will still suck, though ;-)
enum Bool { True, False, FileNotFound };
Thanxs a lot I'll try it out.
Hi,
How can I reduce data tranfer.
My data transfer rate is 500 bytes per seconds.
U said its around 36 bytes.
How will I reduce it?

What actually I'm doing is as follows:-

In server side:-
I'm sending the object(ball,paddle) when the state information.

In client side I'm receving it by using switch statement.

If possible Just show me the way I'll do it PLZ.







He said 36 bytes per packet, not per second. 500 bytes/second is pretty reasonable for an online action game - sounds like you're doing it right. Post a demo! :)
My first game to test DirectPlay. Ugly code, but it works.

http://pages.infinit.net/cbenoi1/NetPong.zip

-cb
500 bytes per second is fine.

To reduce your data rate, send less data, or send the data less often.
enum Bool { True, False, FileNotFound };
Quote:Original post by fractoid
2) Insert a constant delay between an input event being received (the user presses the 'up' key) and the action being taken (the paddle moves upwards). This delay will help hide network latency issues. (Obviously you still send the event straight away, but the idea is that by the time the delay is up, both computers will know about the event and there will be no need to pause gameplay while the two programs resynchronize).


That'll just make it worse... not only will you have a delay from the network, but you'll have a delay from your input device to your PC... Two wrongs don't make a right.

This topic is closed to new replies.

Advertisement