I'm building an MMO using Node.js, and there are monsters roaming around. I can make them move around on the server using vector variables acceleration, velocity and position.
acceleration = steeringForce / mass;
velocity += acceleration * dTime;
position += velocity * dTime;
Right now I just send the positions over, and tell the players these are the "target positions" of the monsters, and let the monsters move towards the target positions on the client with a speed dependant on the distance of the target position. It works but looks rather strange.
How do I synchronise these properly with the players without looking funny to them, taking into account the server lag?
The problem is that I don't know how to make use of the correct acceleration/velocity values here; right now they just move directly in a straight line to the target position instead of accelerating/braking there properly. How can I implement such behaviour?
Answer
There are several things you could do to reduce the visible effects of lagging. Just some ideas:
Send the current acceleration, velocity and position values to the client, and let the client calculate the new velocities and positions until it receives a new data package.
Avoid using frequently changing acceleration.
You should consider defining states of the characters. For example: When a monster attacks, it has 2 acceleration, 0 starting velocity (vector pointing towards the player), and this state lasts for 2 seconds. You can send the identifier of the monster's current state, and the client will know the speed and acceleration values, and will know that it's gonna last exactly for 2 seconds. This way the client can draw the movement smoothly. If something happens in those 2 seconds, eg. the monster dies, then the client receives the new state ("Dying") and can show the animation of that state. Also, define default state changes. For example, a monster will have the "Dead" state as default after the "Dying" state. If "Dying" lasts for 3 seconds, and the client doesn't receive any updates for 3 seconds, your monster isn't gonna get stuck in the "Dying" state, but will die properly on the client side as well.
You could implement some kind of lag compensation on the client side. For example, a small delay to the actions of the player, or effects of spells etc. If the player pushes the jump button, he won't jump immediately, but only after 40 milliseconds or so. The packet gets sent to the server with a timestamp, and forwarded to another player if necessary. The server and the other player's client will know that the actual jumping should be displayed only at Timestamp + 40 milliseconds. You can make this work with most actions without making the delay frustratingly high.
Further reading:
Low traffic client synchronization with server in MMO
How do I keep an MMO synchronized?
No comments:
Post a Comment