# Implementing multiplayer aspect of a simple HTML5 game

I've been thinking about this a lot. I am trying to implement a simple multiplayer game but I have difficulty figuring out how to "share the scene". For example, there are non-player entities like trucks and ideally I am thinking I will just send all the connected clients the bus location (x,y) and the rotation angle ,however, I am running into difficulty because the bus rotates each frame so I am wondering how I may achieve that on server-side?

It seems like a really tough nut to crack and I went over countless examples online and I think I just can't conceptually clarify how something like that can be accomplished.

Any ideas will be very appreciated.

EDIT: Somewhat related simplified question is this. How to simulate animation by sending (x,y, angle) from the server to the clients? Is it with setTimeout() on the server-side, what about each client having different frame-rate on their end?

EDIT2: Just to give one more example which I think would clarify. Consider a rotating triangle that all players should see. How do you accomplish this, do you adjust the angle in the server part of the code and then just emit to all players, because then the server side of code needs an update() for that object. Damn I hope I am not poorly expressing myself.

## 2 Answers

I remember struggling with the same thing actually and here it is. I figured it out not long time ago. I will use the rotating triangle as an example. The server is the one that needs to figure out that the triangle needs to start rotating. The triangle should have two things:

• Rotation
• Rotation velocity

Now, let's say that the every client has it's own triangle. There are two triangles on the screen, yours and your opponents.

When you press the button to start rotating, you need to tell the server that you want to start rotating. When the server finds out, the server needs to change the rotation velocity and continue updating the angle of the triangle. Also, it needs to inform the clients about the triangles rotation. 10 times a second(or it can be less, it depends), the server needs to send the rotation angle to all the clients.

That way the rotation on the clients will update 10 times a second. Now, the only thing that is left is interpolation. The server, instead of just sending the angle, it should send the velocity too. Then the client can use the velocity and simulate it for itself and make it update at the frame it's running on. When the next packet arrives, it just sets the new position and the new velocity that the server said and keeps interpolating.

That is basically it. There are a few links that helped me understand this:

https://gafferongames.com/post/what_every_programmer_needs_to_know_about_game_networking/

https://www.cakesolutions.net/teamblogs/how-does-multiplayer-game-sync-their-state-part-1

Damn I hope I am not poorly expressing myself.

Sometimes I suck at it too and hope I didn't poorly express myself either haha. That's why I provided those links.

Good luck.

Two things for sure,

(1) You'll NEVER get satisfactory results by pumping out a sequence of precisely timed commands. You have to program the behavior to operate mostly autonomously at the client's end

(2) Clients will not agree on the relationship between events they see and their reactions. In other words, client A sees a spaceship appear and shoots it, client B sees client A was able to shoot before the ship even appeared.