Jump to content
  • Advertisement
AliAman

Java Smooth multiplayer player movement (UDP network)

Recommended Posts

7 hours ago, arnero said:

for the moment let's jsut pretend that there is no package loss. I've pinged tons of servers in the past years and either I reach them and there is 0 package loss or the firewalls blocks me. We live in 2019, there is pratically no package loss.

Unless you live in an overpopulated city with poor internet, an overabundance of wifi interference from neighbours, and are using a mobile hotspot as internet connection.

Yes, you can have a beefy 50mbps internet connection today with 350ms ping and 1-5% packet loss.

Also if you're using WiFi over an AP that's slightly too far from the internet router. LAN connections will go through the router regardless if they're on the same AP, and there will be packet loss (a lot of it).

9 hours ago, AliAman said:

I am just setting the player's position to the one received by the packet but due to packet loss and packet delay, the movement is jittery and freakin laggy. How should I implement the interpolation. Can you please show the code even if it is sixth grade because I am in grade 11 now I think I might need a review ;)

Okay.

Player A sends position (5, 10) at 100050ms.

Player A sends position (5, 20) at 100100ms.

Then.

Player B receives the first position. It sets the time of player A to 99950ms (so we keep an animation clock for each remote player), 100ms earlier (buffering delay), and positions him at (5, 10).

Player B has a next frame 50ms later, it moves the animation time of player A forward to 100000ms, position stays the same.

Player B receives the second position.

Player B has a next frame 50ms later, it moves the animation time of player A forward to 100050ms, position stays the same, since it's now at the first position.

Player B has a next frame 25ms later, it moves the animation time of player A forward to 100075ms. This time is inbetween the two received positions, so the position is interpolated to (5, 15).

And so on.

The key here is, start with replicating exactly the positions and timing from the remote player, as if they're an animation timeline, with some initial delay to allow for buffering. Only once you get that working, think about extrapolating and advanced smoothing.

Quick and dirty interpolation pseudocode:

double timestamp0, timestamp1;
vector2 pos0, pos1;
double timestampNow;
double diffTotal = timestamp1 - timestamp0; // time between timestamps
if (diffTotal <= 0) return pos1; // sanity
double diffNow = timestmap1 - timestampNow; // should be between 0 and diffTotal if interpolating
double factor1 = diffNow / diffTotal; // between 0 and 1
double factor0 = 1.0 - factor1; // between 0 and 1
return (pos0 * factor0) + (pos1 * factor1); // result vector

Review the following maths:

  • Trigonometry (area, sin, cos, tan, the rest is not important). Right angled triangles are practically the same math as vector math, so you can skip ahead to vector math from there as well (vector math is 'easier').
  • Vector math (adding vectors, multiplying vectors with a factor, the length of a vector (sqrt(x²+y²), it's easy.), dot product.)
  • Matrix math (matrix * vector, matrix * matrix.) This is useful for rotations and stuff.
  • Interpolation (linear interpolation, bezier interpolation.)
  • Extrapolation
  • Approximation

And always remember: sin, cos, tan and sqrt are evil. :)

Edited by Kaetemi

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!