Search code examples
c++cocos2d-xgame-physicsmultiplayerenet

C++ Multiplayer game using ENet/Cocos2dx


I'm new to networking and multiplayer games in general so i tried to get into this field as challange and to discover more about it. So i decided to make my ready game that is built in cocos2dx multiplayer. I created server-side using enet and basicly what im doing is that im sending the velocity and position. What i noticied that the sync isnt really perfect i searched in google some says that we should do physics logic in server side so my question is: How can i perfectly sync the movments between the clients ? If by doing physics stuff in server side how can i do this any example ? Im using the (integrated in cocos2dx) Box2D physics engine

Thanks for reading sorry for spelling mistakes


Solution

  • If you play a few instances of a commercial AAA networked game side by side, you'll notice that they're not perfectly synced either.

    There's all kinds of lag in networking, and you need to hide that.
    Since you can't compensate for the lag (you can't transmit information faster than the speed of light), you need to create a believable simulation of synchronization.
    You possibly also don't want to transmit a complete physics update on each frame, but even if you do, packet loss and delays are inescapable facts of networking.

    One method that's often used is "dead reckoning", which means that you extrapolate the movement of entities based on their last known trusted position and velocity (and perhaps acceleration).
    When you get an update with the actual entity state, you update the state of your entities.
    This adds another layer of complexity: you don't want things to teleport, so you also need to interpolate between your extrapolated state and the updated state.

    In order to avoid cheaters, you would also make the server be the authority on what the world looks like, and send your input data - key presses, mouse movements, ... - to the server instead of a physics state, and do all calculations on the server as well as on the clients.
    (Not just the server because you need to approximate the world while the server is determining "the truth".)

    If you now think "but that means that the state of the world is different for different players", you're right.
    In reality this doesn't matter - since you very rarely have two players watching the same thing side by side, you only need the world to appear plausible to everyone.
    What you need to do in practice to make the world plausible depends on your game (a turn-based strategy game isn't as sensitive to time glitches as a fast-paced FPS), as does what shortcuts you can take without destroying the illusion.