Translating a quaternion camera at a fixed height
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 verticall开发者_运维百科y 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.
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).
精彩评论