【问题标题】:How to move the camera behind a model with the same angle?如何以相同的角度将相机移动到模型后面?
【发布时间】:2012-12-07 20:56:36
【问题描述】:

我遇到在 3D 世界中将相机移动到物体后面时遇到困难。我会创建两种视图模式。

1:用于 fps(第一人称)。 第二:人物背后的外景(第二人称)。

我在网上搜索了一些示例,但它在我的项目中不起作用。

这是我的代码,用于在按下 F2 时更改视图

 //Camera
        double X1 = this.camera.PositionX;
        double X2 = this.player.Position.X;
        double Z1 = this.camera.PositionZ;
        double Z2 = this.player.Position.Z;


        //Verify that the user must not let the press F2
        if (!this.camera.IsF2TurnedInBoucle)
        {
            // If the view mode is the second person
            if (this.camera.ViewCamera_type == CameraSimples.ChangeView.SecondPerson)
            {
                this.camera.ViewCamera_type = CameraSimples.ChangeView.firstPerson;

                //Calcul position - ?? Here my problem
                double direction = Math.Atan2(X2 - X1, Z2 - Z1) * 180.0 / 3.14159265;
                //Calcul angle - ?? Here my problem

                this.camera.position = ..
                this.camera.rotation = ..

                this.camera.MouseRadian_LeftrightRot = (float)direction;
            }
            //IF mode view is first person
            else
            {
                    //....

【问题讨论】:

    标签: c# 3d xna camera


    【解决方案1】:

    这是 Xna 中一个非常基本的第三人称相机(您所说的第二人称)。它假设您存储了玩家的世界矩阵并且可以访问它:

    Vector3 _3rdPersonCamPosition = playerWorldMatrix.Translation + (playerWorldMatrix.Backward * trailingDistance) + (playerWorldMatrix.Up * heightOffset);// add a right or left offset if desired too
    Vector3 _3rdPersonCamTarget = playerWorldMatrix.Translation;//you can offset this similarly too if desired
    view = Matrix.CreateLookAt(_3rdPersonCamPosition, _3rdPersonCamTarget , Vector3.Up);
    

    如果您的 FPS 摄像头工作正常,并且假设它与玩家的位置和方向基本相同,您可以用它的视图矩阵代替上面的 playerWorldMatrix,如下所示:

    Matrix FPSCamWorld = Matrix.Invert(yourWorkingFPSviewMatrixHere);
    

    现在无论我在哪里写playerWorldMatrix,你都可以改用FPSCamWorld

    【讨论】:

      【解决方案2】:

      如果我是你,我会带上你现在工作的 FPS 摄像机(我假设它可以正确移动,有一个位置矩阵等?),然后向它添加另一个平移变换,以将它“移动”回播放器。

      换一种说法:

      如果 FPS 视图的“平移/视图矩阵”类似于:

      (抱歉,好久没玩XNA了,所以不记得正确的类名了)

      var camTranslateMatrix = [matrix representing player position];
      var camDirectionMatrix = [matrix representing player direction, etc];
      var camViewMatrix = camTranslateMatrix * camDirectionMatrix;
      

      然后你可以像这样改变它:

      var camTranslateMatrix = [matrix representing player position];
      var camDirectionMatrix = [matrix representing player direction, etc];
      
      // If not in 3rd person, this will be identity == no effect
      var camThirdPersonMatrix =
             IsInThirdPersonMode ?
                   new TranslateMatrix(back a bit and up a bit) :
                   IdentityMatrix();
      
      var camViewMatrix = 
            camTranslateMatrix * 
            camDirectionMatrix * 
            camThirdPersonMatrix;
      

      有意义吗?这样一来,每次在两个视图之间切换而无需大量讨厌的数学运算就很简单了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 1970-01-01
        • 2022-07-26
        • 1970-01-01
        • 2020-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多