简体   繁体   English

航位推算-客户端/服务器游戏-如何处理按键

[英]Dead Reckoning - Client/Server game - How to handle keystrokes

I've read a few articles about dead reckoning, but it's still a bit confusing to me. 我读了一些有关航位推算的文章,但是这仍然让我感到困惑。 I'm pretty sure I understand the concept of point-and-click movement, but how would I update keystroke movement between the client and server, when the end-point of the movement is unknown? 我敢肯定,我了解点击动作的概念,但是当移动的终点未知时,如何更新客户端和服务器之间的按键动作?

How would you estimate where the player is going (server-side) if there is no end-point? 如果没有端点,您如何估计播放器的运行方向(服务器端)?

Let's start with dead reckoning. 让我们从推算开始。 Try to think about it on a straight line where there is no left or right movement. 尝试在没有左右移动的直线上考虑它。

You are at point A and want to get to point B . 您位于A点,想到达B点。 You know the distance to point B so you can compute how much acceleration and velocity it will take to get there. 您知道到点B的距离,因此可以计算到达该点所需的加速度速度 Dead Reckoning works the other way. 航位推测法则相反。 You are at point A and will move at a set speed to get near point B . 您位于A点,将以设定的速度移动到B点附近。

In the video game world they use this method all the time. 在视频游戏世界中,他们一直在使用这种方法。 So don't think about it as you're moving to point B you're just moving towards point B in increments because in like a FPS your point B will constantly moving. 因此,在移至点B时,请不要考虑它,而只是逐步向点B移,因为像FPS一样,点B会不断移动。 When in fact its really only moving in increments in the direction of point B . 实际上,它实际上仅在B点的方向上递增。

Once you get moving forward then you can start worrying about left/right and up/down which will follow the same principle just in different directions. 一旦前进,您就可以开始担心左/右和上/下,它们将沿同一方向朝不同的方向前进。

As for implementing this you have 2 options. 至于实现这一点,您有2个选择。

One way, you could make this calculation on the client side then send the new position to the server. 一种方法是,您可以在客户端进行此计算,然后将新职位发送到服务器。 Then update what everyone else sees on screen. 然后更新其他人在屏幕上看到的内容。

The other way which I think is better you can make all these calculations on the server side and just receive an update where you ended up. 我认为另一种更好的方法是,您可以在服务器端进行所有这些计算,并且只收到最终的更新。 X-Box live makes one of the consoles the host so that machine is running all the software and the external users are just firing events. X-Box live使控制台之一成为主机,因此计算机正在运行所有软件,而外部用户只是在触发事件。 This is why you'll hear people complain about having an unfair host advantage. 这就是为什么您会听到人们抱怨拥有不公平的东道主优势的原因。

Now let's look at some code. 现在让我们看一些代码。 This script comes from the Unity Sdk standard install package. 该脚本来自Unity Sdk标准安装包。

/// This script moves the character controller forward 
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to Move or SimpleMove per frame.  

var speed : float = 6.0; 
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
}

So in this example we have speed which is set to a constant. 因此,在此示例中,我们将速度设置为常数。 So when moving in a direction we will travel in that Vector at a constant rate with no care about where point B is located. 因此,在朝某个方向移动时,我们将以恒定速率在该向量中移动,而无需关心B点的位置。

Jump speed is how fast will we move in the y axis in the positive direction and gravity in the negative direction stopping at point 0. 跳跃速度是我们将在y轴上沿正方向移动多快,而重力在负方向上停止于0点的速度。

So, How can you use this? 那么,如何使用呢? You could build a server script that executes a move action and have a client side controller that passes the direction information to the server. 您可以构建一个服务器脚本,该脚本执行移动动作并具有将方向信息传递到服务器的客户端控制器。

So lets say on the client side action is key press down, you send a call to the server to move you in the direction selection and the server will keep you moving you in that direction until action key press up or a change in direction from another input. 因此,可以说在客户端操作是按下按键,您向服务器发送呼叫以将您朝着方向选择移动,服务器将使您继续朝该方向移动,直到按下操作键或从另一个方向改变方向为止输入。

I hope this helped. 希望对您有所帮助。

Let's see... I understand you know how to code, but not what, am I right? 让我们看看...我知道您知道如何编码,但是不知道,对吗?

You can feed the keystrokes directly to the server and let it do all the work. 您可以将击键直接输入服务器,并让它完成所有工作。 The server has no need to know where you are heading, it has your coordinates and direction and that's all it will need, unless you have server-handled AI. 服务器无需知道您要去往何方,它就拥有坐标和方向,仅此而已,除非您拥有服务器处理的AI。 In the later case, you can do something similar to raycasting in Unity, start checking what is straight ahead and see if it is anything of interest, if so you know the potential destination. 在后一种情况下,您可以执行与Unity中的光线投射类似的操作,开始检查正前方的内容,并查看是否有兴趣,如果可以,则知道潜在的目的地。

It is safe to constantly send back to the client all his data, so it is always up to date. 不断向客户发送所有数据是安全的,因此始终是最新的。 If you believe this will strain your connection you can do that every 50ms or as often as you believe is safe, and for smooth function estimate everything on the client side too. 如果您认为这会使您的连接紧张,则可以每隔50毫秒或以您认为安全的频率进行连接,并且为使功能平稳,还可以估算客户端的所有内容。 Make sure the server does all the collision detection and all the mechanics not related to the UI, or else the game will be prone to client-side malfunction or malicious tampering. 确保服务器执行所有碰撞检测以及与UI无关的所有机制,否则游戏将易于出现客户端故障或恶意篡改。

If you do have to look at where the player might be going, you can use a few approaches, you could either have a number of virtual cubes in the world which keep track of what is inside them, which will simplify finding what's ahead in terms of resources, or you could check everything there is, which is rather heavy on the machine, but this adds some other complexities too. 如果您必须查看玩家的前进方向,则可以使用几种方法,您可以在世界上拥有多个虚拟立方体来跟踪其中的内部空间,从而简化寻找未来发展方向的方法。资源,或者您可以检查所有内容,这在计算机上相当繁重,但这也增加了其他一些复杂性。 In the first case do not forget to stop looking once you hit an obstacle. 在第一种情况下,一旦遇到障碍,别忘了停止寻找。

You can also calculate the angle between the player's direction and other items of importance to check what else might be on his mind. 您还可以计算玩家的方向和其他重要项目之间的夹角,以检查他的想法。

The question about UDP has already been answered, plus I'm sure Wikipedia has a helpful book on UDP and TCP , I have nothing to add. 关于UDP的问题已经得到解答,而且我确信Wikipedia上有一本关于UDP和TCP的有用书 ,我没有什么要补充的。

The explanation of dead reckoning was also quite satisfactory, I hope I added other angles to the concept. 航位推测法的解释也很令人满意,我希望我为这个概念增加其他角度。

I hope I helped, if you need any clarifications or any other help feel free to contact me, if I was less than unhelpful then feel free to downvote, I study computer engineering and am creating my second game for PC so maybe I have ran into some problem already and can share the knowledge. 我希望我能提供帮助,如果您需要任何澄清或其他帮助,请随时与我联系;如果我没有帮助,然后请我拒绝投票,我将学习计算机工程,并为PC创建第二款游戏,所以也许我遇到了已经存在一些问题,可以共享知识。

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

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