Split
Split

Reputation: 299

Problems with OpenGL rotation

In my OpenGL application, I have a camera which is controlled using the keyboard (movement) and mouse (looking around).

Everythings been working perfectly fine up until now, I have noticed that if I move my camera above 300 in the Y axis, it starts to mess up when moving the mouse. For example, if I go to Y =310, and move the mouse up, as it starts to look up it starts turning to the left as well.

I am not sure what the reason for this is. Can anyone help?

Heres the code to work out forward and up position for gluLookAt()

double cosR, cosP, cosY; //temp values for sin/cos from double sinR, sinP, sinY; //the inputed roll/pitch/yaw

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;

Upvotes: 1

Views: 168

Answers (1)

Michael Dorgan
Michael Dorgan

Reputation: 12515

Gimble Lock. It is explained here.

Quaternions are the standard solution to this problem.

Briefly, when two axis angles approach each other in your above calculation, it causes a loss of available movement. You need a 4th degree of freedom to prevent this - and this is what Quaternion math allow.

Upvotes: 8

Related Questions