处理 XNA + Lidgren 中的延迟 [英] Dealing with lag in XNA + lidgren

查看:78
本文介绍了处理 XNA + Lidgren 中的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 XNA 中尝试 lidgren 并且我有一些滞后"的问题.

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

我已经下载了他们的 XNA 样本 并注意到即使他们的样本也滞后.问题是,另一边的运动并不顺畅,我正在局域网(实际上是在同一台计算机上)而不是通过互联网进行尝试.

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.

是否有与 Lidgren 和 XNA 连接滞后导致运动不顺畅的相同问题?

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

推荐答案

您链接的示例直接将位置设置为从网络接收到的任何内容,这对于多人游戏来说是个坏主意!

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 (Math.Abs(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. 如果单元位置几乎在远程位置,它会跳到远程位置(但是,它会跳到很小的距离,看起来不会滞后).
  2. 如果差异太大而无法跳跃,则慢慢朝您应该处于的位置移动.

由于单位平稳地向应有的位置移动,因此看起来完全没有滞后!

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

插值常数控制本地和远程位置收敛的速度:

The interpolation constant controls how fast the local and remote positions will converge:

  • 0:忽略网络更新
  • 小:非常快速地卡入到位(可能看起来很迟钝)
  • 大:缓慢滑入到位,看起来很流畅,但可能感觉没有反应

您需要在这些选项之间选择一个折衷方案.

You need to choose a compromise somewhere in between these options.

在实现这种系统时,还有一些其他的事情需要考虑,例如,你经常想要一个上限来限制单元与它们的远程位置之间的距离,否则本地和远程状态可能会变得未卡住";在某些情况下.如果它们相距太远(除非极端滞后,否则永远不会发生这种情况)您可以停止游戏并告诉用户它太滞后,或者将单位直接跳到位置,这看起来会滞后但至少游戏会继续.

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.

附录:重读这个答案,我突然想到一个增强功能是跟踪时差.如果您(大致)知道系统中的滞后是多少,那么您就会知道,当您收到具有远程位置的数据包时,您大致知道该数据包距过去多远.如果您也发送远程速度,则可以预测对象现在的位置(假设速度恒定).在某些游戏中,这可能会使估计的本地状态和真实的远程状态之间的差异变小,而在其他游戏中(您有很多变化的速度),这可能会使事情变得更糟.

Addendum: Rereading this answer, it occurs to me that an enhancement would be to track time differences. If you know (roughly) what the lag is in the system, then you know that when you receive a packet with a remote position in you know roughly how far into the past that packet is from. If you send remote velocity too, you can predict where the object is now (assuming constant velocity). This may make the difference between estimated local state and true remote state smaller in some games, in other games (where you have lots of changing velocities) it might make things worse.

这篇关于处理 XNA + Lidgren 中的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆