tags:

views:

217

answers:

1

I am experimenting with lidgren in XNA and I'm having some issues with the 'lag'.

I've downloaded their XNA sample and noticed that even their sample lags. The thing is, the movement is not smooth on the other side, and I'm trying this on a LAN (on the same computer actually) not over the internet.

Has any had the same issues as regards unsmooth movement due to a lagging connection with lidgren and XNA ?

+2  A: 

The sample you linked directly sets the position to whatever it receives from the network, this is a bad idea for a multiplayer game!

What you should do in a real game is interpolate between the local position and the remote position. So, your receive method would look a little like this:

void Receive(packet)
{
    unit.RemoteX = packet.Read_X_Position();
    unit.RemoteY = packet.Read_Y_Position();
}

This has no affect on the local position on the unit, instead in your update method (every frame), you move the local position towards the remote position:

void Interpolate(deltaTime)
{
    difference = unit.RemoteX- unit.LocalX
    if (difference < threshold)
        unit.LocalX = unit.RemoteX
    else
        unit.LocalX += difference * deltaTime * interpolation_constant
}

You then display the "local" position of the unit, this achieves lagless movement like so:

  1. If the unit position is almost at the remote position, it will jump to the remote position (however, it will jump such a tiny distance that it won't look laggy).
  2. If the difference is too big to jump, then move slowly towards the position you should be in. the interpolation constant controls how fast the local and remote positions will converge. With interpolation constant zero, the unit will ignore network updates, with constant 1 it will teleport straight to the network position (and look laggy) - so you need to choose a compromise somewhere in between

Since the unit moves smoothly towards where it should be, it looks like there is no lag at all!

There are some other things to consider when implementing this kind of system, for example you often want an upper limit on how far apart units can be from their remote position otherwise the local and remote state can become "unstuck" in some situations. If they are too far apart (which should never happen except in cases of extreme lag) you can either halt the game and tell the user it's too laggy, or jump the unit straight into position, which will look laggy but at least the game will continue.

Martin
+1: Thanks for your answer; it's already given me some insight
Andreas Grech
good to know :D
Martin
It would be nice if you accepted this answer ;)
Martin
Believe me, I was going to accept this answer :-) I can't thank you enough for your email collaboration as well.
Andreas Grech