PDA

View Full Version : problem with time based animation


ferum
2006.08.04, 09:18 AM
ok, having followed One Sad Cookie's tutorial (http://onesadcookie.com/Tutorials) I have implemented time based animation in my program, or so I thought. Here is my code:
void ProcessKeyboardInput(unsigned char key, int x, int y)
{
if (key == 32)
{
inMotion = 1;
}
}

void KeyUp(unsigned char key, int x, int y)
{
if (key == 32)
{
inMotion = 0;
prevTime = 0;
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (inMotion)
{
if (prevTime == 0)
prevTime = glutGet(GLUT_ELAPSED_TIME);
curenTime = glutGet(GLUT_ELAPSED_TIME);
timeDif = curenTime - prevTime;
timeDifer = timeDif / 1000.0f;
curenTime = prevTime;

coordy += timeDifer * 2;
if (coordy > 480)
coordy = -50.0;
}

glLoadIdentity();
glPushMatrix();
glTranslatef(320.0 + coordx, 50.0 + coordy, 0.0);
glColor3f(0.0, 0.5, 0.0);
drawOBJ(&ship);
if (inMotion)
{
glColor3f(1.0, 0.0, 0.0);
drawOBJ(&flame);
}
glPopMatrix();
glutSwapBuffers();
}


when I hold down key #32 (the spacebar) my crude, programmer art "ship" flies upwards... at an ever accelerating rate:\ what gives?

OneSadCookie
2006.08.04, 09:27 AM
I don't see anything in that code to suggest that that should happen.

Perhaps you're using glTranslatef to move the object, and not resetting the transformation when you're done?

ferum
2006.08.04, 09:53 AM
Perhaps you're using glTranslatef to move the object, and not resetting the transformation when you're done?
I don't think I am, I'm using glLoadIdentity().

unknown
2006.08.04, 01:54 PM
you have all your animation code in your drawing routine, that is asking for trouble.

ThemsAllTook
2006.08.04, 02:13 PM
I think I see your problem. Looks like you'd want to do prevTime = curenTime; rather than curenTime = prevTime;.

ferum
2006.08.04, 05:45 PM
Looks like you'd want to do prevTime = curenTime; rather than curenTime = prevTime;.

Ah ha! that fixed it. Thanks!:)