Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XNA中FPS式相机目标计算_C#_Windows Phone 7_Math_Xna_3dcamera - Fatal编程技术网

C# XNA中FPS式相机目标计算

C# XNA中FPS式相机目标计算,c#,windows-phone-7,math,xna,3dcamera,C#,Windows Phone 7,Math,Xna,3dcamera,我需要的是根据相机位置、Y旋转和Z旋转来计算我的lookat 3d向量的位置,我假设任何大于0的数字都足以让相机观看 这是我用来控制相机、计算视图和投影矩阵等的静态类 涉及位置和旋转的类成员: public static Vector3 Position { get; set; } //Publicly available for use outside the class public static Vector3 Rotation { get; set; } private static V

我需要的是根据相机位置、Y旋转和Z旋转来计算我的lookat 3d向量的位置,我假设任何大于0的数字都足以让相机观看

这是我用来控制相机、计算视图和投影矩阵等的静态类

涉及位置和旋转的类成员:

public static Vector3 Position { get; set; } //Publicly available for use outside the class
public static Vector3 Rotation { get; set; }
private static Vector3 camPos = new Vector3(0.0f, 200.0f, 300.0f); //However we work with these for manipulation of values
private static Vector3 camTarget = new Vector3(0, 0, -1200.0f);

private static float rotY = 0, rotZ = 0; //I believe that these are the correct axis following the right hand catasian coordinate system
我的相机的更新功能:

public static void Update()        
{
    //Controls here

    //Update Position here
    Position = camPos;            

    //Update camTarget based on Position, rotY and rotZ <- Need help with this bit

    //After calculating camTarget update Rotation
    Rotation = camTarget;

    UpdateMatrices();
}

请大家分享一下这个秘密好吗?

不要使用
矢量3.向前
,从旋转矩阵中拉出
。向前
。向上
矢量:

    public void UpdateMatrices()
    {
        /// assumes Rotation is a Vector3 containing rotation around each axis expressed in radians
        Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z);
        /// View = Matrix.CreateLookAt(Position, Rotation, Vector3.Forward);

        View = Matrix.CreateLookAt(Position, Position + rotationMatrix.Forward, rotationMatrix.Up);
        Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, nearClip, farClip);

        /// World matrix needs to match your translation and rotation used above
        /// XNA expects matrices to be concatenated in SRT (scale, rotation, then translate) order:
        World = rotationMatrix * Matrix.CreateTranslation(Position);

        CamBoundingFrustrum = new BoundingFrustum(View * Projection);
    }
请注意,您必须将相机的平移(通过正在存储的向量,或从相机的世界矩阵
.translation
属性)添加到
旋转矩阵.Forward
以获得真实目标

矩阵
.Forward
.Backward
.Left
.Right
.Up
.Down
属性与旋转矩阵变换的等效
矢量3
属性相同。所以,
RotationMatrix.Forward
指向你所看到的方向,
.Up
与这个方向正交,等等

如果只使用
Vector3.Forward
,就不会得到变换后的向量,可能会发生奇怪的事情

其他说明:

  • 不要让一切都是静态的。在
    游戏期间创建摄影机类的实例。初始化()
    并将其添加为属性<代码>静态应谨慎使用-它会导致各种有趣的线程问题,让你发疯
  • 我还建议切换到四元数而不是偏航/滚动/俯仰(如果您还没有),因为它避免了万向锁,但这是另一个主题

  • 这里有一点与您的问题无关:您的
    CamBoundingFrustrum
    是一个类,而不是一个结构。这意味着每帧创建一个“新”帧会给垃圾收集器带来大量垃圾。处理因相机移动/旋转而在每一帧中对平截头体所做更改的预期方法是简单地更新(每一帧)边界平截头体对象的
    矩阵
    属性,该属性在最初创建相机时仅创建一次。
        public void UpdateMatrices()
        {
            /// assumes Rotation is a Vector3 containing rotation around each axis expressed in radians
            Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z);
            /// View = Matrix.CreateLookAt(Position, Rotation, Vector3.Forward);
    
            View = Matrix.CreateLookAt(Position, Position + rotationMatrix.Forward, rotationMatrix.Up);
            Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, nearClip, farClip);
    
            /// World matrix needs to match your translation and rotation used above
            /// XNA expects matrices to be concatenated in SRT (scale, rotation, then translate) order:
            World = rotationMatrix * Matrix.CreateTranslation(Position);
    
            CamBoundingFrustrum = new BoundingFrustum(View * Projection);
        }