8
\$\begingroup\$

I'm creating a simple racing game (spaceships, no gravity) using p2.js, phaser and node.js.

What I have done: Client receives world state from server:

  • extrapolate other players based on latest velocity/position from server
  • check if client-side prediction was correct - if no apply position from server and process inputs that server was not aware of.
  • fixed physics step

Server:

  • receives inputs from clients and apply
  • fixed physics step
  • sends world state to each client

Now I'm struggling with collisions between players. Colliding player is jumping all time during collision. I think it's because client-side prediction is not calculating similar results to the server.

Server doesn't know all inputs from player (lag).

Player doesn't have the same position of colliding player as the server (lag).

Combining this two makes the client to resolve collision different than the server and when world state arrives player has to make a big correction.

\$\endgroup\$
  • \$\begingroup\$ Would it be possible to increase the timestep on the server so that there is at least less of a correction? Have you tried making the players use the same data that the server has (the last sent position as well as predictive position of the other players)? \$\endgroup\$ – user3797758 May 7 '16 at 14:07
  • \$\begingroup\$ Suggestion: Have you tried to make only the server calculates the collision? The client will only be used to render and send/receive data (and react to whatever data it received, of course). \$\endgroup\$ – Greffin28 May 7 '16 at 16:30
  • \$\begingroup\$ Possible duplicate of Network latency compensation problem for laggy players with collision \$\endgroup\$ – congusbongus Jun 4 '16 at 13:32
1
\$\begingroup\$

Do physics only on server. Here is an example workflow:

  1. Client presses "steer left".
  2. Client sends "steer left" request to server
  3. Server receives the data.
  4. Server steers instance of client on server to left.
  5. Server detects there is a collision between players.
  6. Server makes corrections based on collision.
  7. Server sends position data to client.
  8. Client receives the position data.
  9. Client adjusts it's position.
\$\endgroup\$
0
\$\begingroup\$

If you want the player interaction to feel instantaneous then there are a few choices. If not then just do physics on server side.

Option 1: Client side detects collision and collision data is sent to server and/or directly to other client. Certain multiplayer shooters do this kind of collision detection because otherwise the shooting mechanics would not feel accurate.

Option 2: Server side does physics, but makes many different predictions. You can just use more instances of each player and predict them to do slightly different things. Make sure they don't self-collide and send the data to the clients. Each time the player motion matches an prediction you use the data that has already been sent to the client.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.