3D 相机看着任意点,但从不滚动这样做
(在 C#/XNA 4.0 中)
我有 Vector3cameraPosition、Vector3targetPosition
我想让我的相机看着目标,始终“面向”它,但从不滚动
所以滚动始终是中立的,以查看瞄准相机总是调整俯仰或偏航
我已经尝试了无数的方法和信息的组合,我在这里和网上找到,但我还没有找到任何可以正常工作的东西。我认为我的问题可能是我的“向上”向量(我已经尝试过 .Up, 1,0,0, 0,1,0, 0,0,1)
当我移动相机时,我会这样做:
CameraPosition += moveSpeed * vectorToAdd;
UpdateViewMatrix();
UpdateViewMatrix() 是..好吧,我已经尝试了我所看到的一切。最简单的是...
View = Matrix.CreateLookAt(CameraPosition, targetPosition, upVector);
其中 upVector 是 Vector3.Up, 1, 0, 0; 0, 1, 0; 0、0、1 或其他更“适当”的尝试来获取我的实际向上向量。这听起来可能是我的问题..
这应该非常简单,救命!
(In C#/XNA 4.0)
I have Vector3 cameraPosition, Vector3 targetPosition
I want to have my camera look at the target, always 'facing' it, but never rolling
So roll always is neutral, to view the target the camera always either adjusts pitch or yaw
I've tried countless combinations of methods and information I find here and on the web but I haven't found anything that works properly. I think my issue may be my 'Up' vector (which I've tried .Up, 1,0,0, 0,1,0, 0,0,1)
When I move my camera I do:
CameraPosition += moveSpeed * vectorToAdd;
UpdateViewMatrix();
UpdateViewMatrix() is.. well, I've tried everything I have seen. At most simple...
View = Matrix.CreateLookAt(CameraPosition, targetPosition, upVector);
Where upVector has been Vector3.Up, 1, 0, 0; 0, 1, 0; 0, 0, 1, or other more 'proper' attempts to get my actual up vector. This sounds like it's probably my problem..
This should be dead simple, help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,我在某个地方让自己感到困惑,不知道事情应该是什么样子,以及我在我的世界空间中实际考虑的“向上”是什么。我不得不用完 (0, 0, 1) 向量,效果很好
Ends up I was confusing myself somewhere about what things should look like and what I was actually considering "up" in my worldspace. I had to use up vector of (0, 0, 1) and things worked great
假设 Y 向上的世界空间,没有滚动意味着相机侧面向量的 Y = 0。您必须获取视图方向向量 (
normalize(targetPos -cameraPos)
),并获取这样的侧面向量它与视图正交并且 Y = 0,换句话说view.x * side.x + view.z * side.z = 0
、view.y = 0
。这是一个简单的二维垂直线,side = normalize(vector3(-view.z, 0, view.x))
。获得侧向量后,您可以得到
up = cross(view, side)
并构造视图矩阵。根据坐标系的惯用手,您可能需要取反侧向量。
请注意,
view.y = +-1
处有一个奇点(side 是零长度向量);这是因为未针对该俯仰角定义滚转 - 任何滚转值都会给出相同的变换。Assuming a Y-up world space, absence of roll means that camera side vector has Y = 0. You have to get your view direction vector (
normalize(targetPos - cameraPos)
), and get such side vector that it's orthogonal to view and has Y = 0, in other wordsview.x * side.x + view.z * side.z = 0
,view.y = 0
. This is a simple 2d perpendicular,side = normalize(vector3(-view.z, 0, view.x))
.After you have the side vector, you can get
up = cross(view, side)
and construct the view matrix.Depending on the handedness of your coordinate system, you may need to negate the side vector.
Note that there's a singularity at
view.y = +-1
(side is a zero-length vector); this is because the roll is not defined for this pitch angle - any roll value gives the same transformation.