# OpenGL 2.1 rotation , Gimball lock

I'm working on a little app for a school project , which should be manipulating 3D objects. And i'm stuck with the rotation , as i'm facing what i think is called the gimball lock ? here's a video I uploaded that clarifies my problem : https://youtu.be/MT8nTAkvD6k

Now , from what I have understood , multiple rotations change the orientation of the object , but keep the X , Y , Z axis fixed , is that it ? Without knowing a lot about openGL , i thought that some projections like these would be enough : say Alpha is the angle following the rotation around X; The new Y and Z (let's Call them Y' and Z')axis would have this equation :

Y'=cos(Alpha)Y+sin(Alpha)Z

Z'=-sin(Alpha)Y+cos(Alpha)z

Based on this , i wrote this code (this method displays the teapot)

void Teapot::PrintObj()
{
glPushMatrix();
Translated();
RotatedX();
RotatedY();
RotatedZ();

glColor3ub(_Color.red,_Color.green,_Color.blue);
glutSolidTeapot(2);

if(_Selected){WiredTeapot();Guizmo();}

glPopMatrix();
};


Now this is the RotatedX method :

void Object3D::RotatedX()
{
rotY.x=0;
rotY.y=cos((angles.x/180.0f)*3.141592);
rotY.z=sin((angles.x/180.0f)*3.141592);

rotZ.x=0;
rotZ.y=-sin((angles.x/180.0f)*3.141592);
rotZ.z=cos((angles.x/180.0f)*3.141592);

glRotated(angles.x,rotX.x,rotX.y,rotX.z);
}


all the variables are declared within the class : the rotX , rotY and rotZ are the 3 vector axis , every time the object undergoes a rotation , these axis are modified;

The RotatedY method now :

void Object3D::RotatedY()
{
rotX.x=cos((angles.y/180.0f)*3.141592);
rotX.y=0;
rotX.z=-sin((angles.y/180.0f)*3.141592);

rotZ.x=sin((angles.y/180.0f)*3.141592);
rotZ.y=0;
rotZ.z=cos((angles.y/180.0f)*3.141592);
glRotated(angles.y,rotY.x,rotY.y,rotY.z);
}


Same thing , if this method is called it means that the object is rotated around the Y axis , hence the modification of the X and Z axis position.

and now the RotatedZ :

void Object3D::RotatedZ()
{
rotX.x=cos((angles.z/180.0f)*3.141592);
rotX.y=sin((angles.z/180.0f)*3.141592);
rotX.z=0;

rotY.x=-sin((angles.z/180.0f)*3.141592);
rotY.y=cos((angles.z/180.0f)*3.141592);
rotY.z=0;
glRotated(angles.z,rotZ.x,rotZ.y,rotZ.z);


I'm really struggling to make this work ; I've looked on the net and saw things like Quaternions (i don't even know what that is).

Thanks for helping me !

• I didn't see anything that looks like gimbal lock to me for what it's worth. – Alan Wolfe Jul 21 '15 at 4:44
• When i rotate the object around the X , and then i try to rotate it on Y for example, it doesn't turn around the Y , i mean , not the Y-axis of the object at least – HdjoWattever Jul 21 '15 at 5:03
• ah i see. i might be wrong then, it's hard to tell. In case it helps, quaternions are a way of representing rotations using a 4d vector. they are basically a normalized 4d unit vector on a sphere. Links if you want them: 3dgep.com/understanding-quaternions and euclideanspace.com/maths/geometry/rotations/conversions/… – Alan Wolfe Jul 21 '15 at 13:42
• Thanks a lot , but i would like to keep it simple , couldn't I use the GLM library to do all the math (the matrices multiplcations) myself ? i mean without using the quaternions. – HdjoWattever Jul 22 '15 at 7:47
• I think you're just not rotating properly. After you rotate the object its local axis will have changed, but you aren't taking that into account when applying subsequent rotations. That is, after rotating around x, rotating around y is actually MUCH more complex than just a single sin or cos around the y axis, because you actually moved the y axis during the original rotation. Using matrices instead of raw trigonometry will make this much, much easier. – Sean Middleditch Sep 13 '15 at 18:57

To avoid gimball lock you have to use quaternions. Since you are using the GLM library you already have code for that.

q1 = cos(a/2) + i ( x1 * sin(a/2)) + j (y1 * sin(a/2)) + k ( z1 * sin(a/2))

q2 = cos(a/2) + i ( x2 * sin(a/2)) + j (y2 * sin(a/2)) + k ( z2 * sin(a/2))

q3 = cos(a/2) + i ( x3 * sin(a/2)) + j (y3 * sin(a/2)) + k ( z3 * sin(a/2))

a = rotation angle
x1, y1, z1 = rotation axis 1
x2, y2, z2 = rotation axis 2
x3, y3, z3 = rotation axis 3