PDA

View Full Version : Animation



tip
03-14-2005, 09:39 PM
I am using the glutTimerFunc to speed up an object. I need a way to slow it down. And change the translate to go in the opposite direction after the object has stopped.

This function is the timer handle used for the glutTimerFunc()

void moveTri(int value)
{

/* stop after 30 steps */

if(motion_speed>=5000)
{
return;
}

glutPostRedisplay;

if(back_flag)
step--;
else
step++;

/* call animate again in 200 mili-Secs */

glutTimerFunc(motion_speed,moveTri,step);

}


The special key handler function. When the up arrow is pressed, the triangle should accelerate. when the down arrow is pressed, the triangle slows down. Keep pressing will slow down more and eventually stop. How to make the triangle go in reverse after triangle has stopped?

void specialKeyPressed(int key, int x, int y)
{
if (key == GLUT_KEY_UP )
{
motion_speed--;
back_flag = 0;
glutTimerFunc(motion_speed/60,moveTri,step);

cout << "triangle moving forward" << endl;
}
else if (key == GLUT_KEY_DOWN)
{
motion_speed+=1500;

if (motion_speed > 6000)
back_flag = 1;

cout << motion_speed << endl;
glutTimerFunc(motion_speed,moveTri,step);
cout << "triangle slowing down" << endl;
}
else if (key == GLUT_KEY_LEFT)
{
turn_left();
cout << "triangle moving left" << endl;
}
else if (key == GLUT_KEY_RIGHT)
{
cout << "triangle moving right" << endl;
}
}


The following code is the display function:

void display(void)
{
rquad = 1.0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glScaled(3,3.5,1);
glTranslatef(-10.0, -12.0,1.0);
get_list();
glTranslatef(0.0, step,0.0);
draw_tri();
glPopMatrix();

glFlush(); //clear buffers
glutSwapBuffers ();

}

T101
03-14-2005, 11:34 PM
Some issues with your code:


if (key == GLUT_KEY_UP ) { motion_speed--;

else if (key == GLUT_KEY_DOWN) { motion_speed+=1500;
Note how you're accelerating 1500 times as fast as you're decelerating?

Also, I can't comment on the exact parameters of the glutTimerFunc function, but
I'd say you're using a variable timer to slow things down instead of the normal method. You also seem to be dividing the motionspeed by 60 to get the first parameter. If that parameter is a frequency, it can work, but only as long as the motionspeed is greater than 0.

The normal method would be to do something like:


position = position + elapsedtime * speed
speed = speed + elapsedtime * acceleration

if speed > maxspeed
then speed = maxspeed

if speed < minspeed
then speed = minspeed

acceleration = 0

if accelerator_being_pressed
then acceleration += 1500

if brake_being_pressed
then acceleration -= 1000
You can use a time measurement for elapsed time, or you can use a free running (repeating) timer at a fixed interval (e.g. 30 times per second).

All this stuff can be extended into 3 dimensional movement, but that's a lot easier with a vector class.

pedrodx
03-17-2005, 05:35 AM
Hi!

Try these steps to increase performance of your code:

Alternatively using a glutTimerFunc to perform animation, use:

glutIdleFunc(display);

display() {
//draw routine
Sleep(timeToSleep);
//didn't remember the name of the variable you used, but think you'd understand...
//Sleep(int miliseconds) is a function from windows api (include <windows.h>) to do whatever it is told to..... :)

}

I didn't know what's your question, but I'm glad to consider that, because I'm involved with animation issues and I was told to do that way...

See ya!!

mikael_aronsson
03-17-2005, 05:41 AM
Putting a sleep in the idle function is never a good thing to do, the idle function should use as little time as possible, the sleep will also look up the user interface for the same amount of time, create a timer instead and use that, that gives pretty ok animation.

Changing the animation speed by changing the number of frames displayed per second is not a very good way to do animation anyway, you should keep the framerate constant.

Mikael

pedrodx
03-17-2005, 05:49 AM
Ahh... and consider... I THINK that you can't alter dinamically the motion_speed of the glutTimerFunc()... Once it is called, it will call every mili-seconds you passed to it like a first time the func drawTri().
Try to implement like I said before, the way you are implementing, the animation must be in the same speed all the time.... See ya!!