2013-04-11 239 views
1

在我的OpenGL应用程序中,我有一个使用键盘(移动)和鼠标(环顾四周)控制的相机。OpenGL旋转问题

万物一直工作完全正常到现在为止,我已经注意到,如果我将我的相机超过300在Y轴,它移动鼠标的时候开始陷入困境。例如,如果我转到Y = 310,向上移动鼠标,当它开始查看时,它也开始向左转。

我不确定这是什么原因。谁能帮忙?

赫雷什代码来制定出向前和向上的位置为gluLookAt()

双COSR,COSP,舒适; //来自 的sin/cos的温度值double sinR,sinP,sinY; //输入滚动/俯仰/偏航

if(Yaw > 359) Yaw = 0; 
if(Pitch > 359) Pitch = 0; 
if(Yaw < 0) Yaw = 359; 
if(Pitch < 0) Pitch = 359; 


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; 

回答

7

Gimble Lock。 It is explained here.

四元数是这个问题的标准解决方案。

简而言之,当您的上述计算中两个轴角互相接近时,会导致可用运动损失。你需要第四个自由度来防止这一点 - 而这正是四元数学允许的。

+0

TY @Basic - 打我的拼写错误。 –

+1

和进一步阅读的有用链接... http://www.cprogramming.com/tutorial/3d/quaternions.html @Michael NP;) – Basic