我的 OpenGL 相机似乎有问题。当我第一次创建它时,一切正常。然而,自从导入模型和创建对象以来,我注意到发生了一些奇怪的事情。
首先,这是我的运动代码:
float xRot = (Pitch / 180 * 3.141592654f),
yRot = (Yaw / 180 * 3.141592654f);
float sinX = float(sin(xRot)) * myInput.Sensitivity,
sinY = float(sin(yRot)) * myInput.Sensitivity,
cosY = float(cos(yRot)) * myInput.Sensitivity;
if(myInput.Keys['W']) //Forwards
{
curPos.x += sinY;
curPos.z -= cosY;
curPos.y += sinX;
}
else if(myInput.Keys['S']) //Backwards
{
curPos.x -= sinY;
curPos.z += cosY;
curPos.y -= sinX;
}
if(myInput.Keys['A']) //Left
{
curPos.x -= cosY;
curPos.z -= sinY;
}
else if(myInput.Keys['D']) //Right
{
curPos.x += cosY;
curPos.z += sinY;
}
//Move camera up and down
if(myInput.Keys['Q'])
curPos.y-= myInput.Sensitivity; //up
else if(myInput.Keys['E'])
curPos.y+= myInput.Sensitivity; //down
//Check col
if(curPos.y < 0)
curPos.y = 0;
else if(curPos.y >= 300) //Gimbal lock encountered
curPos.y = 299;
其次,这是我计算 gluLookAt 的动作:
double cosR, cosP, cosY; //temp values for sin/cos from
double sinR, sinP, sinY; //the inputed roll/pitch/yaw
cosY = cosf(Yaw*3.1415/180);
cosP = cosf(Pitch*3.1415/180);
cosR = cosf(Roll*3.1415/180);
sinY = sinf(Yaw*3.1415/180);
sinP = sinf(Pitch*3.1415/180);
sinR = sinf(Roll*3.1415/180);
//forward position
forwardPos.x = sinY * cosP*360;
forwardPos.y = sinP * 360;
forwardPos.z = cosP * -cosY*360;
//up position
upPos.x = -cosY * sinR - sinY * sinP * cosR;
upPos.y = cosP * cosR;
upPos.z = -sinY * sinR - sinP * cosR * -cosY;
基本上我注意到,如果相机在 Y 轴上超过 300,则视图开始旋转,如果我在 x/z 轴上移动太远,也会发生同样的事情。
谁能看到我的代码有什么问题?当我应该在 rad 工作时,我在 deg 工作吗?