gpt4 book ai didi

c# - 相机翻转问题

转载 作者:太空狗 更新时间:2023-10-29 23:42:34 24 4
gpt4 key购买 nike

我正在使用 XNA3.1 引擎在 C# 中编写游戏。但是我的相机有一个小问题,基本上我的相机在滚动时旋转超过 180 度时往往会“翻转”(当相机达到 180 度时,它似乎会翻转回 0 度)。获取 View 矩阵的代码如下:

Globals.g_GameProcessingInfo.camera.viewMat = Matrix.CreateLookAt(Globals.g_GameProcessingInfo.camera.target.pos, Globals.g_GameProcessingInfo.camera.LookAt, up);                //Calculate the view matrix

Globals.g_GameProcessingInfo.camera.LookAt 变量在相机正前方的位置 1 个单位,相对于相机的旋转,“向上”变量通过以下函数获得:

static Vector3 GetUp()      //Get the up Vector of the camera
{
Vector3 up = Vector3.Zero;
Quaternion quat = Quaternion.Identity;
Quaternion.CreateFromYawPitchRoll(Globals.g_GameProcessingInfo.camera.target.rot.Y, Globals.g_GameProcessingInfo.camera.target.rot.X, Globals.g_GameProcessingInfo.camera.target.rot.Z, out quat);

up.X = 2 * quat.X * quat.Y - 2 * quat.W * quat.Z; //Set the up x-value based on the orientation of the camera
up.Y = 1 - 2 * quat.X * quat.Z - 2 * quat.Z * quat.Z; //Set the up y-value based on the orientation of the camera
up.Z = 2 * quat.Z * quat.Y + 2 * quat.W * quat.X; //Set the up z-value based on the orientation of the camera
return up; //Return the up Vector3
}

最佳答案

我在使用 gluLookAt 的 OpenGL 中遇到了同样的问题。我用自己的相机类解决了这个问题:

void Camera::ComputeVectors()
{
Matrix4x4 rotX, rotZ;
Quaternion q_x, q_y, q_z;
Quaternion q_yx, q_yz;
q_x.FromAngleAxis(radians.x, startAxisX);
q_y.FromAngleAxis(radians.y, startAxisY);
q_z.FromAngleAxis(radians.z, startAxisZ);
q_yx = q_y * q_x;
q_yx.ToMatrix(rotZ);
q_yz = q_y * q_z;
q_yz.ToMatrix(rotX);
axisX = startAxisX;
axisZ = startAxisZ;
axisX.Transform(rotX);
axisZ.Transform(rotZ);
axisY = axisX.Cross(axisZ);

position = startPosition;
position -= center;
position.Transform(q_yx);
position += center;
}

它可能过于复杂,但有效。 axisY 是您的向上矢量。完整代码 list 位于: http://github.com/filipkunc/opengl-editor-cocoa/blob/master/PureCpp/MathCore/Camera.cpp

希望对您有所帮助。

关于c# - 相机翻转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2057367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com