c# – 使用偏航和俯仰创建旋转矩阵并模拟FPS摄像机

c# – 使用偏航和俯仰创建旋转矩阵并模拟FPS摄像机,第1张

概述首先,我必须说我想在这里做的只是关于点,而不是对象或其他任何东西.我在这里使用OpenGL更多的是用于计算. 所以这是我的问题.我想在我的2D环境中显示一些3d点(例如winform或图像); 为此,我编写了这个函数:(它在C#中并使用OpenTK(OpenGL包装器)但我相信如果您了解OpenGL,无论您对C#有何了解,都可以理解它) private Vector3 GetProjectedLo 首先,我必须说我想在这里做的只是关于点,而不是对象或其他任何东西.我在这里使用OpenGL更多的是用于计算.

所以这是我的问题.我想在我的2D环境中显示一些3d点(例如winform或图像);
为此,我编写了这个函数:(它在C#中并使用OpenTK(OpenGL包装器)但我相信如果您了解OpenGL,无论您对C#有何了解,都可以理解它)

private Vector3 GetProjectedLocation(        Vector3 pointposition,Vector3 cameraposition,Matrix4 cameraRotationMatrix)    {        float horiZentalFov = MathHelper.degreesToradians(60.0f);        float vIEwRatio = (float)this.RenderSize.WIDth / this.RenderSize.Height;        Vector3 cameraReference = new Vector3(0,-1);        Vector3 transformedReference = Vector3.transform(cameraReference,Matrix4.Invert(cameraRotationMatrix));        Vector3 cameraLookatTarget = cameraposition + transformedReference;        Matrix4 modelMatrix = Matrix4.IDentity;        Matrix4 vIEwMatrix = Matrix4.LookAt(cameraposition,cameraLookatTarget,new Vector3(0,1,0));        Matrix4 projectionMatrix = Matrix4.CreatePerspectiveFIEldOfVIEw(horiZentalFov,vIEwRatio,0.1f,100.0f);        Matrix4 viewmodelMatrix = vIEwMatrix * modelMatrix;        return Helper.Project(            pointposition,viewmodelMatrix,projectionMatrix,new Rectangle(new Point(),this.RenderSize));    }

我还写了另一个名为Project cuz的函数我没有在OpenTK中找到它:

public static Vector3 Project(Vector3 point,Matrix4 viewmodel,Matrix4 projection,Rectangle vIEwport)    {        Vector4 point4 = new Vector4(point,1.0f);        Vector4 pointModel = Vector4.transform(point4,viewmodel);        Vector4 pointProjection = Vector4.transform(pointModel,projection);        pointProjection.W = (float)(1.0 / pointProjection.W);        pointProjection.X *= pointProjection.W;        pointProjection.Y *= pointProjection.W;        pointProjection.Z *= pointProjection.W;        return new Vector3                   {                       X = (float)((pointProjection.X * 0.5 + 0.5) * vIEwport.WIDth + vIEwport.X),Y = (float)((pointProjection.Y * 0.5 + 0.5) * vIEwport.Height + vIEwport.Y),Z = (float)((1.0 + pointProjection.Z) * 0.5)                   };    }

当所有三个值(偏航,俯仰和滚动)都为零时,它似乎按预期工作.我可以通过改变偏航和使用音高值垂直移动水平点.滚动也按照它应该的方式移动它.

当我将音高设置为90度时,问题就出现了.在这种情况下,偏航而不是水平移动,垂直移动,更糟糕的是,仅将其向下移动.无论我走向正面还是负面(-20度或20度)它总是会下降.有趣的是,当音高为90度时,Roll具有相同的效果,它只能像偏航那样垂直向下移动.将音高设置为-90deg具有相同的效果,但此时的偏航和滚动仅将其向上移动.

当我的两个轴平行时我看起来像万向节锁定问题而我丢失了一个方向,在这种情况下是滚动.我仍然不明白为什么偏航只能朝一个方向发挥作用.

无论如何,问题在于这种锁定在垂直查看时发生,这是我的程序在大多数时间看起来的地方.我已经读过如果我改变旋转顺序,我可以将万向节锁移到顶部(不太常见的地方).我尝试过多次订单而找不到好订单.所以这是我的代码,也许我做错了:

private Matrix4 CreateRotationMatrix(char axis,float radians)    {        float c = (float)Math.Cos(radians);        float s = (float)Math.Sin(radians);        if (axis == 'X')        {            return new Matrix4(1.0f,0.0f,c,-s,s,1.0f);        }        if (axis == 'Y')        {            return new Matrix4(c,1.0f,1.0f);        }        return new Matrix4(c,1.0f);    }    private Matrix4 MatrixFromEulerAngles(Vector3 euler,string order)    {        return CreateRotationMatrix(order[2],GetEulerAngle(order[2],euler))               * CreateRotationMatrix(order[1],GetEulerAngle(order[1],euler))               * CreateRotationMatrix(order[0],GetEulerAngle(order[0],euler));    }    private float GetEulerAngle(char angle,Vector3 euler)    {        if (angle == 'X') return euler.X;        if (angle == 'Y') return euler.Y;        return angle == 'Z' ? euler.Z : 0f;    }

你有什么主意吗?

以下是我知道的订单:
“XYZ”,“XZY”,“YXZ”,“YZX”,“ZXY”,“ZYX”

我可以提供一个示例程序来自己查看问题.

解决方法 我认为问题的一部分来自你在第一个代码块中使用LookAt函数.这些函数的工作方式是通过查看与您正在查找的方向垂直的全局“向上”向量的部分来计算局部“向上”向量.在您查找的情况下,全局“向上”向量(上述代码中的(0,0))没有与查看方向垂直的分量,结果可能是未定义的或不稳定的.当你向下看时也是如此.

相反,尝试将LookAt函数的“向上”向量计算为:

Vector3 localUpVec = Vector3.transform(new Vector3(0,0),Matrix4.Invert(cameraRotationMatrix));Matrix4 vIEwMatrix = Matrix4.LookAt(cameraposition,localUpVec);

或者甚至更好,您可以完全绕过LookAt功能并自行计算:

Matrix4 vIEwMatrix = Matrix4.Subtract( Matrix4.Transpose(cameraRotationMatrix),Matrix4.Translation(cameraposition) );
总结

以上是内存溢出为你收集整理的c# – 使用偏航和俯仰创建旋转矩阵并模拟FPS摄像机全部内容,希望文章能够帮你解决c# – 使用偏航和俯仰创建旋转矩阵并模拟FPS摄像机所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1230786.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存