I want to make a 2 player pong game that uses websockets and node.js server. socket.io is used on both client and server. So far, my only experience is creating a chat application.
This is my first attempt at a multiplayer game so I’m not so familiar with network gaming. Should the server keep track of:
- Every position the ball is at and how often or when?
- player movement, player move left or right, what if I press and hold for awhile, how do I handle this? Should I send like a
pressHoldStartPositionandpressHoldStopPosition? I guess this is easy if I only allow pressing but not holding down.
My thoughts:
- When the ball hits a player, the client calculates velocity, start and end position and the other client should perform the correct animation from that.
- No idea.
The clients calculates – nothing – unless you want it to predict next game step* (server anwser). The whole game runs on server which is one and only trustworthy source of data and calculations. In pong like games where there is less than 10 objects you can send data quite often (50ms interval will do). For a ball I’d send [x, y, velocity, or angle], for a paddles [x, y].
Then interpolate (animate in time – 50ms) between old (x,y on client) and new (x,y which you just got from server).
Don’t send milion copies of – player pressed up – rather send one packet each period of time (100ms?) containing informations like “player is pressing up” for 100ms, or “player set position” to (32, 100) then check server-side was this move possible and accept it or reject.
Maybe this thread will help with your adventure:
[*1] preditction is a lag compensation mechanism which you can implement later. In simplified way it means that server and client shares some of game logic code and when client has no current data from server it tries to simulate what is happening in the real game.