# Implementing smooth object rotation

I declared two variables on class level. One is the angle the object is rotated by. The second is a stopwatch.

float yRot = 0.0f;
static CStopWatch rotTimer;


I rotate the cube using the SpecialKeys function.

void SpecialKeys(int key,int x,int y)
{

if(key == GLUT_KEY_RIGHT)
{
yRot = rotTimer.GetElapsedSeconds() * 10.0f;
}

if(key == GLUT_KEY_LEFT)
{
yRot = rotTimer.GetElapsedSeconds() * -10.0f;
}

glutPostRedisplay();
}


Finally the rotation matrix is calculated using the rotation variable, in the RenderScene function.

// Create a rotation matrix based on the current value of yRot


It actually works. My problem is that when I hold the key the animation looks fine. But if I press it the animation looks a little jumpy, like the angle is bigger then when I hold it. I suppose this hangs together with the stopwatch. How to solve this issue?

• Is this because of the key repeat? When you press a key, it'll be sent once, then a delay, then he key will be "spammed" to the app. You need to query key states rather than wait for input events (or use the input events to manage your own key state). Don't use GLUT for games, it is completely the wrong toolkit as it is not designed to deal with this issues. Try SDL or GLFW as alternatives. – Sean Middleditch Oct 14 '13 at 17:47

 int timeSinceStart = glutGet(GLUT_ELAPSED_TIME);