简体   繁体   English

如何在网络乒乓球比赛中同步球?

[英]How to synchronize the ball in a network pong game?

I'm developing a multiplayer network pong game, my first game ever. 我正在开发多人网络乒乓球游戏,这是我有史以来的第一款游戏。 The current state is, I've running the physic engine with the same configurations on the server and the clients. 当前状态是,我已经在服务器和客户端上以相同配置运行物理引擎。 The own paddle movement is predicted and get just confirmed by the authoritative server. 预测自己的桨运动,并由权威服务器确认。 Is a difference detected between them, I correct the position at the client by interpolation. 如果检测到它们之间存在差异,我会通过插值来校正在客户端的位置。 The opponent paddle is also interpolated 200ms to 100ms in the past, because the server is broadcasting snapshots every 100ms to each client. 过去,对手的桨叶也插补了200ms到100ms,因为服务器每100ms向每个客户端广播一次快照。

So far it works very well, but now I have to simulate the ball and have a problem to understanding the procedure. 到目前为止,它工作得很好,但是现在我必须模拟球,并且在理解过程上遇到问题。

I've read Valve's (and many other) articles about fast-paced multiplayer several times and understood their approach. 我已经多次阅读Valve的(以及许多其他)有关快节奏多人游戏的文章,并了解了他们的方法。 Maybe I can compare my ball with their bullets, but their advantage is, the bullets are not visible. 也许我可以将我的球与他们的子弹进行比较,但是他们的优势是,子弹不可见。 When I have to display the ball, and see my paddle in the present, the opponent in the past and the server is somewhere between it, how can I synchronize the ball over all instances and ensure, that the it got ever hit by the paddle even if the paddle is fast moving? 当我必须展示球并查看当前的球拍,过去的对手和服务器之间时,如何在所有情况下同步球并确保球被球拍击中即使桨快速移动? Currently my ball's position is simply set by a server update, so it can happen, that the ball bounces back, even if the paddle is some pixel away (because of a delayed server position). 目前,我的球的位置只是通过服务器更新来设置,因此即使桨距某个像素点(由于延迟的服务器位置),球还是会反弹回来。

Until now I've got no synced clock over all instances. 到目前为止,我还没有在所有实例上同步时钟。 I'm sending a client step index with each update to the server. 我正在将每次更新的客户端步骤索引发送到服务器。 If the server did his job, he sends the snapshot with the last step index of each client back to the clients. 如果服务器完成了工作,则他会将快照以及每个客户端的最后一步索引发送回客户端。 Now I'm looking for the stored position at the returned step index and compare them. 现在,我在返回的步骤索引中查找存储的位置并进行比较。 Do I need a common clock to sync the ball? 我需要一个公共时钟来同步球吗?

EDIT: 编辑:
I've tried to sync a common clock for the server and all clients with a timestamp. 我尝试用时间戳同步服务器和所有客户端的通用时钟。 But I think it's better to use an own stepping instead of a timestamp (so I don't need to calculate with the ping and so on - and the timestamp will never be exact). 但是我认为最好使用自己的步进而不是时间戳(因此,我不需要使用ping等进行计算-时间戳永远不会是精确的)。 The physics are running 60 times per second and now I use this for keeping them synchronized. 物理每秒运行60次,现在我使用它使它们保持同步。 Is that a good way? 那是个好方法吗?

When the ball gets calculated by each client, the angle after bouncing can differ because of the different position of the paddles (the opponent is 200ms in the past). 当每个客户计算球时,由于桨的位置不同(对手过去是200毫秒),弹跳后的角度可能会有所不同。 When the server is sending his ball position, velocity and angle (because he knows the position of each paddle and is authoritative), the ball could be in a very different position because of the different angles after bouncing (because the clients receive the server data after 100ms). 当服务器发送球的位置,速度和角度时(因为他知道每个球拍的位置并且是权威的),由于弹跳后的角度不同,球可能会处于非常不同的位置(因为客户端接收到服务器数据) 100ms后)。 How is it possible to interpolate such a huge difference? 如何插值如此巨大的差异?

As you are developing your first game, I think you should try the simplest but brute-force method first. 在开发第一个游戏时,我认为您应该首先尝试最简单但蛮力的方法。 Then you will experience the first exciting result, then you will get courage and try the better methods. 然后,您将体验到第一个令人兴奋的结果,然后您将获得勇气并尝试更好的方法。

Like Source Engine method, process the game play in one side and send every object state to the other for each 1/30 second. 与源引擎方法一样,在一侧处理游戏,然后每1/30秒将每个对象状态发送到另一侧。 This is a brute method but it works in LAN environment. 这是一种蛮力的方法,但是它可以在LAN环境中工作。

Now you will find problems that occur WAN environment where latency is more than 1/30 second. 现在,您将发现延迟超过1/30秒的WAN环境中出现的问题。

I am not sure it works actually, but I think that: 我不确定它是否确实有效,但是我认为:

  1. Assume that the movement of ball does not change by only player's hit. 假设球的运动不会仅因球员的命中而改变。
  2. Send ball position P, velocity and player A position only when player hits the ball to B. 仅当球员将球击至B时,才将球的位置P,速度和球员A的位置发送出去。
  3. At B, receive it but process it as if time L is already passed (L=latency between A and B * 2) However, rendered ball should be keep its previous movement until it reaches the ball position P. 在B处接收它,但就好像已经过去了时间L一样进行处理(L = A和B之间的等待时间* 2)。但是,渲染的球应保持其先前的运动,直到到达球位置P。

Values which never are affected can be masqueraded, even if it is time value. 永远不会受影响的值可以被伪装,即使它是时间值。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM