Developing multiplayer games has different set of difficulties as compared to single player games which makes it even more challenging. While developing multiplayer games, you have to deal with number of factors that can affect the performance of your games and one such factor is: physics.
Any game will involve different types of actions like moving, jumping, shooting, etc. Actions like jump, shoot, etc. do not occur often but actions like movement change at every frame so exchanging this information at every frame is not feasible. Suppose your player is moving and if you send the position of the player at every frame; and game is running at 60fps, there will be enormous number of messages to be sent and received making the network slow.Also, these messages will be difficult to handle.
To handle such problems, you have to properly plan how physics will work in your game, and how you are going to synchronize the physics across all clients. The easiest way to handle this situation is to maintain few characteristics for your player like
(here Position, Velocity, Acceleration are vector quantities and Mass is a scalar quantity. Gravitation can be simulated as vector quantity by keeping X component as 0 and only using Y component to represent the downward gravitational force)
Gravitation force will be constant on all clients, hence you don’t even need to exchange this information which will decrease message load.
Initially, when the player starts to move, you need to send information regarding position, velocity, acceleration and mass. Start simulating the physics on other clients using this information. Send information only when there is a change in velocity (or acceleration) because if these remain constant, there will be no deviation (assuming all the ideal conditions) in the path followed by the player on all clients.
Updating Position and Velocity:
Position.X = Position.X + Velocity.X
Position.Y = Position.Y + Velocity.Y
Velocity.X = Velocity.X + Acceleration.X
Velocity.Y = Velocity.Y + Acceleration.Y – Gravity.Y
You can ignore Acceleration if it’s not needed in your case just to keep it minimal. As you can see if velocity is constant, position will increase in same ratio and will appear same in all clients. When there is a change in velocity, you only need to exchange information at that instant only.
Keeping these quantities as vector gives one more advantage i.e. if there is a change in direction then there will be a change in velocity, hence you can also send information regarding the direction through velocity.
Consider this simple example
1. At Instant t, player A starts to move from position p(x,y) with velocity v(x1,y1). You need to send this information to all clients, so the player A will appear at position p(x,y) and will start to move with velocity v(x1,y1) across all clients.
2. At instant t+dt, the player will appear at p’(x1’, y1’) across all clients so you don’t need to exchange any information between the time t and t+dt
3. At instant t+dt, the player changes its direction so the new velocity will be v(x1’,y1’), now you have to exchange this information with all clients, changing the direction of player A in all clients.
This way you can reduce considerably the number of messages to be exchanged
Hope this article helped you, please do share your feedback. If you have any questions or need any further assistance, please feel free to write us at firstname.lastname@example.org