DRiFTy
DRiFTy

Reputation: 11369

Rotation in OpenGL ES

I am trying to rotate a sphere continuously on a given axis using this code:

gl.glRotatef(axisX, 0, 1, 0);
gl.glRotatef(axisY, 0, 0, 1);

axisX = (axisX+1)%360;
axisY = (axisY+1)%360;

The variables axisX and axisY are both being incremented by one right now which would make the rotation go in a diagonal direction up to the right. The object gets to about 45 degrees of rotation and then start to turn and start rotation the other way. How can I get it to continuously rotate on an axis other than just x and y by themselves?

Note: I am trying to hook up a virtual joystick to control the axisX and axisY values and have the sphere rotate on the axis represented by the joystick. If anyone has any advice on that, that would be great as well.

Edit: I've change it so that if I use gl.glRotatef(angle, axisX, axisY, axisZ); that it works and keeps the rotation going, but the rotation isn't smooth, it looks like the rotation starts over when I switch the rotation axis.

Upvotes: 2

Views: 837

Answers (1)

Trax
Trax

Reputation: 1910

glLoadIdentity(); // Reset rotation and give a new one
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);

The first argument is the angle, then the axis you want to pivot around.

About your edit: Looks like you are mixing axis and angles here. The axis should a unit length vector and the angle go from 0 to 2*PI.

Another way would be to use quaternions, then transform the quaternion to matrix and load the matrix as your current modelview matrix.

Hope that helps.

Upvotes: 2

Related Questions