1
\$\begingroup\$

There is a multiplayer game and server sends to all clients a message when a shot happens, containing bullet position, bullet velocity and timestamp. By having timestamp, we can calculate the lag, let's say it's 100ms.

My game uses Box2D. How can I predict the real and current bullet position by having such data? I guess velocity might also change a little bit too.

\$\endgroup\$
1
\$\begingroup\$

current_position = starting_position + ((lag_delay + time_elapsed ) * velocity)

Where "time elapsed" is how long we've known about the bullet existence. Ostensibly this doesn't need to be stored, as when the bullet is created client-side, it is 0, and afterwards we update its position every frame update.

The lag delay is merely just an extra bit of deltaTime applied when the bullet is first spawned.

\$\endgroup\$
  • \$\begingroup\$ I guess this only works for linear, straight line bullets. What about ones that are affected by gravity? \$\endgroup\$ – Gintas_ Dec 30 '15 at 15:42
  • \$\begingroup\$ Calculate the gravity for the lag_time as well. Seriously, all the lag_time is is a once-off "do this much movement as the bullet spawns" calculation. \$\endgroup\$ – Draco18s Dec 30 '15 at 15:43
  • \$\begingroup\$ can you provide an example please? \$\endgroup\$ – Gintas_ Dec 30 '15 at 15:45
  • \$\begingroup\$ pastebin.com/vuCsZc3B \$\endgroup\$ – Draco18s Dec 30 '15 at 16:04

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.