PDA

View Full Version : Simulation Timer



ajmrm
12-26-2004, 07:43 AM
Hi,
I'm creating a simulation project and I have some doubt. It'll be helpful if you guys can guide me.

How to write multiple timer? Let's say, I have to place an object every 33ms and there is another function to calculate the trajectory and find the time taken for the object to move.

There is another timer function to move the object with respect to the time which I find out from the function.
//-------------------------------------------
//---------------SAMPLE CODE-------------
//-------------------------------------------
float time;

main()
{
glutTimerFunc(33, Timer1, 1);
glutTimerFunc(time, Timer2, 2);
}

void Timer1(int value)
{
AddObject();
glutPostReDisplay();
glutTimerFunc(33, Timer1, 1);
}

void Timer2(int value)
{
MoveObject();
glutPostRedisplay();
glutTimerFunc(time, Timer2, 1);
}

void CalculatePath()
{
....
....
// Finding the path the object move stored in linked list...
....
}

//-------------------------------------------

Now my concern is, is this the correct way to write mulitple timer?

In the AddObject() I wanted to place a sphere in random (x,y) coordinates.

Help needed.
Thanx. :)

jwatte
12-26-2004, 01:16 PM
You might be helped by http://www.mindcontrol.org/~hplus/graphics/game_loop.html

Typically, when you have multiple timers, you want to figure out which event is the next in line, and sleep/block until that time. Then dequeue, execute, and repeat. When you get timer callbacks (rather than specific timed events), you should make sure that the timer callback interval is small enough to dover your worst-case acceptable timing jitter.