Rotation

Hi,

Im using key presses to ratate a shape on the canvas. At the minute I have the rotate set as a small incremental when a button is pressed.

However, I want to be able to change this so that instead of the user having to hold down the button to rotate the shape 90 degrees, i want to them to be able to push the button once, and it will automatically rotate the 90 degrees.

I have set the small incremental that i already had into a for loop, so that when the button was pressed the shape would rotate the total 90 degrees with one key press. I was however hoping for the gradual effect of a rotation, like in the nehe examples when the shapes are constantly rotated. Instead it simply snapped round straight away the 90 degrees.

Does anyone know a way of getting the effect that I am after?

Thanks.

Use a boolean variable that indicates whether or not the scene is animated, and a float variable containing the rotation angle.


boolean animated = false;
float angle = 0.0f;
float[] axis = { 0.0f, 0.0f, 1.0f };

public void display(GLDrawable glDrawable)
{
      ...
      glPushMatrix();
            glRotatef(angle, axis[0], axis[1], axis[2]);
            //render stuff here
            ...
      glPopMatrix();
      ...
      if(animated)
            angle += 5.0f;
}

public void keyPressed(KeyEvent e)
{
      if(e.getKeyCode() == VK_A)
            animated = ! animated;
}