I do a small step-by-step (25 seconds per move) network game on Phaser.js (P2 physics), according to the air hockey principle.

The essence of the work is as follows. When one player walks (runs his chip on the principle of angry birds, that is, it delays and indicates the direction), then the sprite ID, velocity.x and velocity.y are transmitted to the server. And from the server, respectively, this data goes to the second player and the chip with the same ID is assigned the data of the applied acceleration. Everything basically works as it should. Both players have all the chips on the field in the same way. But, from time to time (at least 1 time per game) there is a failure and the chips of one player move around the field differently from the chips of another.

All sprites have body.setCircle (radius). The border of the field is set manually (rectangle on the white line).

Can anyone come across a similar problem and tell you how to solve it. Maybe in addition to speed, you need to transfer some parameters to the server?

screen

  • @Kromster, turn-based game, 25 seconds per turn - DemoS
  • You are probably right, apparently some kind of “micro” movement remains, and it is different for two players. In any case, the angular velocity is exactly. Then can you tell me how to completely stop the sprite in this engine when passing a turn? - DemoS
  • Yes, I did it now, just thinking maybe there is something out of the box for that. But there appeared a new problem. If you switch to another tab, the animation stops, and when you return, it continues. If during the time that the tab was closed, the opponent made a move, then when you return to the game tab, the move is made when the animation of the previous move is not finished yet and hence the new out of sync. We need to somehow defeat the stop animation with an inactive tab, since the game is tied up on time - DemoS
  • good)) How to convert your comment in response, so I accepted? But it has not been here quite a while, everything has changed - DemoS

1 answer 1

Thanks @Kromster for the tip on "residual motion." That was the problem.

How do you synchronize time? Do the players go strictly by turns (as in chess) or at the same time? - Kromster

Add debug output to the log, compare the logs of the players, find the difference, find out where it came from, correct it. - Kromster

And during the course of the player all the chips are static or there is still some residual movement? - Kromster

You are probably right, apparently some kind of “micro” movement remains, and it is different for two players. In any case, the angular velocity is exactly. Then can you tell me how to completely stop the sprite in this engine when passing a turn? - DemoS

I am not familiar with the engine, but you can probably reset all speeds and check all the positions at the end of the turn. - Kromster 3 hours ago

The following code helped:

this._players.setAll('body.velocity.x', 0); this._players.setAll('body.velocity.y', 0); this._ball.body.velocity.x = 0; this._ball.body.velocity.y = 0; this._ball.body.angularVelocity = 0;