0
\$\begingroup\$

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
m3dRotationMatrix44(mRotate, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);

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?

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 14, 2013 at 17:47

1 Answer 1

2
\$\begingroup\$

From what you describe I suspect that it has something to do with your timer. I think the timer is calculating the delta time from the last time you pressed a key and not from and display update. It's pretty simple keeping your own delta time (in a display or update function):

 int timeSinceStart = glutGet(GLUT_ELAPSED_TIME);
 int deltaTime = timeSinceStart - oldTimeSinceStart;
 oldTimeSinceStart = timeSinceStart;

Other then that, as someone else pointed out, you probably want to do update the rotation in the update function based on the state of the key (down or up) not in the keypress itself. You would use the glut key down key up function manage the state of that key.

http://www.swiftless.com/tutorials/opengl/keyboard.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .