A
A
Alixx2022-03-10 00:39:54
Node.js
Alixx, 2022-03-10 00:39:54

How to make the movement of bots on node.js so that it is smoothly displayed on the client?

The html page has a canvas with a map and characters on it. The client communicates with the node.js server using sockets (ws).
Everything that happens on the map is controlled by the node.js server for synchronization between all clients. So far, all the work of the server has been to process the request from the client and send a response to the client.

Bots, on the other hand, should work on a different principle.
The server must move them (at different speeds) to new coordinates, and it must inform clients about this so that they have the same and smooth display of this movement of bots.

How to implement it?
Once every few milliseconds, run a cycle for all bots, in it, taking into account time and speed, assign a new coordinate to the bot and send it to all clients? And how then to make smoothness of this movement on the client? That on the client did not twitch?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2022-03-10
@Alexandroppolus

In theory, bots should not be too different from the characters of the players. The only difference is that the character moves by the server according to the ws message from the player, and the bot - according to the command from the bot itself, but the essence of the messages and commands is the same, for example, "start driving to the right". Well, the server should immediately send the player the changed coordinates of certain objects.
bots periodically, according to a timer, are given the opportunity to assess the situation and "think" what to do next, and send a command to the movement mechanism.
the copy of the world on the client is updated according to ws messages. Redraw - by requestAnimationFrame, where the current coordinates are taken. Usually this approach gives a smooth movement.
there is an annoying moment here: for example, the player presses the right arrow button, but until it reaches the server via ws, until the server moves the player and reports it back to the client, tens or even hundreds of milliseconds can pass, it will feel like brakes. Therefore, sometimes an "optimistic update" is made - the object starts moving on the client without waiting for a message from the server. Looks smoother, but out of sync is possible. So it’s not clear which is better, usually it’s a set of compromises.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question