# Another Quaternion question

This topic is 2582 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, I'm using jogl and am simply trying to rotate a sphere in all three axes: x, y, and z.

Here is the code that draws the ball, I'll post two other snippets after it...

 private void drawBall( GL2 gl ) { /* now add the texture */ rdsTexture.enable( gl ); rdsTexture.bind( gl ); /* rotate for inclination and toolface */ Quat4f result = this.rotate(roll, pitch, yaw); result.normalize(); float[ ] modelView = this.quatToMatrix( result ); /* the default view is straight and level */ gl.glMultMatrixf( modelView , 0 ); /* draw a sphere */ GLUquadric quadric = glu.gluNewQuadric( ); glu.gluQuadricTexture( quadric , true ); glu.gluSphere( quadric , 6.0f , 64 , 64 ); } 

okay, now here is the code for the this.rotate ...
 public Quat4f rotate( double roll , double pitch , double yaw ) { // Assuming the angles are in radians. float cY = ( float ) Math.cos( Math.toRadians( yaw ) / 2.0f ); float sY = ( float ) Math.sin( Math.toRadians( yaw ) / 2.0f ); float cX = ( float ) Math.cos( Math.toRadians( pitch ) / 2.0f ); float sX = ( float ) Math.sin( Math.toRadians( pitch ) / 2.0f ); float cZ = ( float ) Math.cos( Math.toRadians( roll ) / 2.0f ); float sZ = ( float ) Math.sin( Math.toRadians( roll ) / 2.0f ); Quat4f q = new Quat4f( ); q.w = cZ * cY * cX + sZ * sY * sX; q.x = cZ * cY * sX - sZ * sY * cX; q.y = cZ * sY * cX + sZ * cY * sX; q.z = sZ * cY * cX - cZ * sY * sX; q.normalize( ); return q; } 

and finally, the code for this.quatToMatrix ...
 private float[ ] quatToMatrix( Quat4f q ) { float[] matrix = new float[16]; // First Column matrix[0] = 1 - 2 * (q.y * q.y + q.z * q.z); matrix[1] = 2 * (q.x * q.y + q.z * q.w); matrix[2] = 2 * (q.x * q.z - q.y * q.w); matrix[3] = 0; // Second Column matrix[4] = 2 * (q.x * q.y - q.z * q.w); matrix[5] = 1 - 2 * (q.x * q.x + q.z * q.z); matrix[6] = 2 * (q.z * q.y + q.x * q.w); matrix[7] = 0; // Third Column matrix[8] = 2 * (q.x * q.z + q.y * q.w); matrix[9] = 2 * (q.y * q.z - q.x * q.w); matrix[10] = 1 - 2 * (q.x * q.x + q.y * q.y); matrix[11] = 0; // Fourth Column matrix[12] = 0; matrix[13] = 0; matrix[14] = 0; matrix[15] = 1; return matrix; } 

[font="Arial"]Here is what is happening ... I can pitch (rotate around x-axis) fine, but if I roll to the right or left by 90 degrees and then pitch, the ball appears to yaw. Does something look amiss here?[/font]

##### Share on other sites
I'm continuing to research but everything seems to say that the ordering is correct. Basically create a quaternion from euler rotations (roll, p[itch, and yaw). Then normalize it, and multiply it against the view matrix. Yet I'm still having issues where the other axes to rotate about move. So for example I pitch 90 degrees, and then yaw, but now my yaw is rolling instead. It's because my y-axis has been rotated to the z-axis. Is there any way around this?

##### Share on other sites
Looks like Gimbal Lock to me, you're still using Euler angles inside your rotation function, so, you're getting the rotation as you would if you were using Euler angles.

1. 1
2. 2
3. 3
Rutin
24
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013121
×