在固定高度平移四元数相机

发布于 2024-10-29 01:52:20 字数 493 浏览 4 评论 0原文

我有一个用 C# 编写的四元数相机(无耻地取自在线教程)。我试图将相机在世界空间中的位置(表示为 Vector3)转换为相机“面向”的方向。但是,我希望相机在 Y 轴上保持在固定高度(假设相机永远不会“颠倒”,因为它可能不会旋转超过垂直向上或向下看 Y 轴)。

简而言之,我希望摄像头的行为方式与 RTS 游戏中的摄像头类似,例如《命令与生存》。征服。向前按应该会让你在地图上“向上”移动,保持在地形上方的恒定高度。下面给出的我天真的尝试是不够的,因为相机的速度是由相机俯视世界的角度决定的。

var Y = mPosition.Y;
mPosition += mInverseViewMatrix.Backward * amount;
mPosition.Y = Y;

上面,mPosition是一个代表世界位置的Vector3。 mInverseViewMatrix 是视图矩阵的逆矩阵。

我正在使用 XNA 和 C#,但任何有关四元数和向量操作的一般建议可能会有用。

I have a quaternion camera written in C# (shamelessly taken from an online tutorial). I'm trying to translate the position of the camera in world space, which is represented as a Vector3, in the direction the camera is "facing." However, I want the camera to stay at a fixed height in the Y-axis (Assume the camera will never be "upside down", in that it may not be rotated past looking vertically up or down the Y-axis).

Put simply, I want the camera to behave in a similar manner to a camera in a RTS game, such as Command & Conquer. Pressing forward should move you "up" the map, staying at a constant height above the terrain. My naive attempt, given below, is insufficient, as the speed of the camera is determined by the angle the camera looks down on the world.

var Y = mPosition.Y;
mPosition += mInverseViewMatrix.Backward * amount;
mPosition.Y = Y;

In the above, mPosition is a Vector3 representing the world position. mInverseViewMatrix is the inverse of the view matrix.

I am using XNA and C#, but any general advice regarding quaternion and vector manipulation will probably be of use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜点 2024-11-05 01:52:20

我用两种方式处理了这个问题。

1) 使用四元数旋转指向“向前”的单位向量,使其指向适当的方向。将向量的 y 分量清零。重新标准化向量。乘以速度并添加到当前位置。

已编辑
2)复制四元数。将四元数的 xz 分量清零。重新标准化四元数。将单位向量旋转四元数,乘以速度,然后添加到位置。旋转的四元数表示是修改后的轴角度表示。因此,如果将发生旋转的轴的“x”和“z”部分清零,那么剩下的唯一旋转就是围绕 y 轴的“偏航”旋转(假设“向前”与 z 轴对齐) -轴)。

I've handled this in two ways.

1) Use the quaternion to rotate a unit vector pointing 'forward' so that it's pointing the appropriate direction. Zero-out the y component of the vector. Re-normalize the vector. Multiply by velocity and add to the current position.

Edited
2) Make a copy of the quaternion. Zero-out the x and z components of the quaternion. Re-normalize the quaternion. Rotate a unit vector by the quaternion, multiply by velocity, and add to position. A quaternion representation of rotation is a modified axis-angle representation. So if you zero-out the 'x' and 'z' portions of the axis around which rotation is happening, then the only rotation remaining is 'yaw' the rotation around the y axis (assuming that 'forward' is aligned with the z-axis).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文