I have a question which I cannot find an answer to.
I’m experimenting with building a multiplayer game with Node.JS and Socket.IO. I have built a chat room as my first experiment, so I have broadcasts working etc. Now I’m at the point where I want to get something working with Canvas.
The problem I’m having is getting my head around multiple and independent players. I know that each player will send their x,y cords to the server and the server will broadcast those, but how does the client know how many players to display, I’m guessing they have to be stored in an array somewhere.
My implementation will be very naive and simplified, no lag compensation, extrapolation and such, but it should point out a general conception of “multiplayering” with node.
I think the simplest approach is to have an associative array containing players(entities) on both client and server. Then from client side you send commands like
{action: "move", target:[32, 100]}and process this command with server logic (where the real game is running). To each socketon connectionyou should assign a player object or id so you can access it like:Then each let’s say 100ms you could send snapshots to all connected players:
And then at client side receive data and interpolate from old to new values.
This is a sufficient algorithm for a semi-arcade game with 20 objects at maximum. Decreasing snapshot’s interval makes it almost suitable for strategy game with more objects. Your main enemy is bandwidth usage which you can decrease minimizing packet’s size. For instance read about BiSON, LZW and don’t send data which haven’t changed since last snapshot.
My reputation doesn’t allow me to post all the links, so I have attached them here:http://pastebin.com/Kh3wvF1D
General introduction to multiplayer conceptions by Glenn Fiedler:
Some multiplayer techniques from Quake:
This will give u a clue about interpolation and extrapolation(prediction)
Valve’s article about latency compensation and general optimisations:
Multiplayer techniques in Age of Empires:
You can also read my article about optimizing bandwidth usage
+1 for Ivo’s Wetzel Mapple.js it’s a big pile of knowledge.