R
R
Roman Rakzin2016-01-31 12:26:38
Canvas
Roman Rakzin, 2016-01-31 12:26:38

2d engines in golang or how to calculate coordinates on the server?

There is a client, he has a ball with coordinates.
He wants to move to another area of ​​the canvas within a certain amount of time.
The calculation must be done by the server. That is, the server knows that client1 is now at the coordinates x=20;y=50, and the client hovered the mouse over the area x=120;y=150, that is, sent the server "I want to go there".
The server should, say, in 2 seconds move Client1 to that point, if there are no new instructions from the client, and report the coordinates of Client1 every, say, 100ms. That is, the client only draws what the server sent and says where I want. There is a similar thing in the game agar.io
How do you do such calculations in golang?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Shevelev, 2016-01-31
@mantyr

In fact, you have two tasks:
1. movement animation
2. interaction with other objects / players
Animation is done on js separately, for this the server does not need to tell where to move every 100ms (of course, if there was no route change).
But interaction with other objects and players is a complicated thing, in fact there is a formula for finding intersections of object trajectories and making decisions about changing the further trajectory or changing the states of objects. In a way, the client and server duplicate this task, they just do it differently.

T
twobomb, 2016-01-31
@twobomb

I did this, by the way, too, an analogue of agar.io, even the code remained. But I used Node.js server.
The easiest way to do this is that the client sends the angle and speed to the server and that's it. We hang the mousemove event on the canvas in it, we define the angle between the ball and the mouse. Next to the helmet server. BUT! It is important to make a restriction, for example, to send no more than 50ms, because the mouse can be moved quickly and too much will be sent to the server.
On the server, organize the movement of the ball like this
ball.x += speed * Math.cos(ball.angle);//Angle in radians
ball.y += speed * Math.sin(ball.angle);//Angle in radians
AND send the coordinates to the ball to the client and he will stupidly draw them.
Well, as I understand it, you have already made a game cycle on the server, like
fps = 30;//Number of renderings per second
function gameLoop(){
//Your actions, movements,
renderings setTimeout(gameLoop,1000/fps);
}
And it's even better that the speed is not sent by the client, but it is calculated on the server, otherwise there will be cheaters...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question