Results 1 to 5 of 5

Thread: Animation

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2005
    Location
    TX
    Posts
    4

    Animation

    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()
    Code :
    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 ();
     
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    314

    Re: Animation

    Some issues with your code:

    Code :
    if (key == GLUT_KEY_UP )	{		motion_speed--;
    Code :
    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:
    Code :
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2004
    Posts
    19

    Re: Animation

    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!!

  4. #4
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    Kristianstad,Skåne,Sweden
    Posts
    1,595

    Re: Animation

    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

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2004
    Posts
    19

    Re: Animation

    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!!

Similar Threads

  1. Help with an animation
    By Eztlicoatl in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-03-2015, 06:03 PM
  2. Animation name
    By Vespa in forum COLLADA
    Replies: 1
    Last Post: 10-14-2009, 04:51 AM
  3. Sprite Animation / Texture Animation
    By Rodrix in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 03-11-2007, 10:36 PM
  4. How to do Animation
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-13-2003, 04:41 AM
  5. 3D animation
    By Alhek in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-20-2003, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean