I'm creating a small online multiplayer game where I have multiple thin clients and an authoritative server. Both the client and the server have a fixed game loop.
Now I have a game entity with the following properties:
- X position: float
- Y position: float
- Direction: float
In the client, the entity is in constant velocity changing its direction based on user input and sends an input event to the server.
[LEFT, PRESSED]
: Entity moves to the left[LEFT, RELEASED]
: Entity moves to the right
Every event is queued on the server, and simulated the same way as on the client (calculating the new properties based on trigonometric functions) and the event is send to other players in the room. The other players apply the same simulation to that particular entity (keeping track of what key is pressed for each entity).
Now after some time playing, the position and direction start to drift and stop being in sync. I suspect this has to do with floating-point arithmetic(?)
Is this implementation correct, rather than sending its position and direction every step?
How would I solve this problem? I think I need to make the server the single source of truth and send regular state snapshots to the the clients to correct possible mistakes/drifting? What would be a good interval to keep the clients up to date?
At the moment, I'm broadcasting an event to every player in the room. What would be a nice approach to only send events to players near the entity? Would a quadtree work here (the same I'm using for my collision detection)?