Problem Player Movement Lag In Client.

245 views
Skip to first unread message

Gấu Trắng

unread,
May 27, 2016, 3:58:32 PM5/27/16
to lidgren-network-gen3
Hello,
I'm using XNA Lidgren for my project. But I'm having problems in the Player Movement. I always send them to the server in the Update Position Player Player, Position on Server to receive and pay for the other player to normal. But when the client receives Position shall be Lag. Through Debug, I discovered the problem lies in the "while ((incmsg = Client.ReadMessage ())! = Null)". While I think the reason it works faster gametime Quick Update and Draw Position will not keep the XNA New Position.

Here is my entire code in Client:

while ((incmsg = Client.ReadMessage()) != null)
            {
                switch (incmsg.MessageType)
                {
                    case NetIncomingMessageType.Data:
                        {
                            string headStringMessage = incmsg.ReadString();

                            switch (headStringMessage)
                            {
                                case "CharacterMove":
                                    {
                                        string name = incmsg.ReadString();
                                        int x = incmsg.ReadInt32();
                                        int y = incmsg.ReadInt32();
                                        foreach (OthersPlayer player in OthersPlayer.players)
                                        {
                                            if (player.name.Equals(name))
                                            {
                                                player.position = new Vector2(x, y);
                                            }
                                        }
                                    }
                                    break;

Vaughan Hilts

unread,
May 27, 2016, 7:18:25 PM5/27/16
to lidgren-ne...@googlegroups.com
Hi,

This won't work very well anyway. You're setting the position directly, you're likely going to want to actually smooth this out over an interpolation. Consider, at the very least, using a Lerp to the new location, with the time it takes being the hertz rate of the updates sent.

--
You received this message because you are subscribed to the Google Groups "lidgren-network-gen3" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lidgren-network-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gấu Trắng

unread,
May 28, 2016, 1:48:28 AM5/28/16
to lidgren-network-gen3
Thank you for your reply to my question.
I still do not understand your way of handling this problem. You can give me a few lines of code, or more detailed explanation is not?
Thank you.

Vào 06:18:25 UTC+7 Thứ Bảy, ngày 28 tháng 5 năm 2016, Vaughan Hilts đã viết:
To unsubscribe from this group and stop receiving emails from it, send an email to lidgren-network-gen3+unsub...@googlegroups.com.
Message has been deleted

Ramon Oliveira

unread,
Jun 29, 2016, 4:06:41 PM6/29/16
to lidgren-network-gen3
Firstly, this will help just a little bit, but try reading messages this way:

        NetIncomingMessage incmsg;
        if ((incmsg = client.ReadMessage()) != null)
              (...)

Secondly, Calling foreach everytime you receive a message is too expensive, specially if you put an if condition inside of it.

I would do it this way:
  • Create an array of player objects, like Player[] players = new Player[100]
  • When you send your position to the server, send your ID, which represents your position on the array, such as 2 (a byte).
      NetOutGoingMessage outmsg = client.CreateMessage(); outmsg.Write(myId); outmsg.Write(myPosX), outmsg.Write(myPosY)...
  • When you receive an update, update the position this way:
       players[im.ReadByte()].position = new Vector2(im.ReadFloat(),im.ReadFloat());

So the final code is something like this:

        NetIncomingMessage incmsg;
        if ((incmsg = client.ReadMessage()) != null)
        {
            switch(incmsg.ReadByte()) //use byte, it's faster to read, store and send
            {
                case 0: //byte 0 represents player update
                    OthersPlayer.players[incmsg.ReadByte()] = new Vector2(incmsg.ReadFloat(), incmsg.ReadFloat());
                    break;
            }
        }


Also, how often are you sending position updates to the client? The recommended rate is about 30 miliseconds, which is equal to roughly 30 times per second.

Using interpolation between the old position and the position just received will reduce, as mentioned above, will reduce the lag sensation, however you shouldn't have any lag if you're running it on localhost. And if you interpolate the position on client side only, make sure that the position on the server is as close as possible to the position on client.


ric...@lbcit.ca

unread,
Jul 13, 2016, 11:54:22 PM7/13/16
to lidgren-network-gen3
You should never trust the client to tell you their own ID (or anything else important, for that matter). It would be trivially easy to send packets with a spoofed ID and make everyone on the server go crazy.

The server should always be the authority.
Reply all
Reply to author
Forward
0 new messages