Use of timers

Started by Mattbooth, February 16, 2005, 11:21:15

Previous topic - Next topic

Mattbooth

Hi Guys

New to LWJGL so little dim on the subject, basically i'm doing a project that originally i tried in C++, I need to put in a timer so the every incriment of the timer my object moves.  Now in C++ glutTimerFunc was used and that was pretty easy, but i decided i hated C++ with a passion and went back to good old java which is where i found LWJGL.

And basically i'm clueless about how to impliment a timer in the code.  I've looked at the NeHe tutorials but they werent much help in the matter.

I'm trying to make a simple fish swimming movement at the mo.

Any help would be appreciated

Cheers

Orangy Tang

Don't use a timer, you want a proper game loop that runs continously and updates the positions of your objects before you render everything. Kev's space invaders example (should be in the lwjgl examples? or search the forums) should have an easy example in it.

If you really do want to get some timing info, have a shufti at the Sys class.

Edit: game loop here: http://www.cokeandcode.com/info/tut2d-4.html

Mattbooth

Hi

i dont see how that helps when you cant specify how often the points are retrieved.

I am creating a simple Quad, i need to call a method every say second to update the points.  The method uses the sin curve to get hte points which are returned.

At the moment the call method to update the points are in the run() like in the NeHe examples.  This just updates when the processor is free so the speed is random hence why it just rather fast on my machine.

Suggestions?

:?

Orangy Tang

I'm sure Kev's tutorial must have covered this in some way, but in short:

1. Lock your framerate/update speed to some fixed number (like 60fps). Then it'll be consistant between machines. Typically that means using display vsync (in the Display class) or Display.sync2.

2. Do all your movement based on time elapsed between frames (via Sys.getTime (or whatever the actual method is called)). Google "time based movement" and you'll get loads.

I used to like 2, but I've actually changed to 1 now. Methinks 2 is probably better for 'proper' 3d rather than 2d.

Teeth

Or you could keep a record of your game's time for your quad, and every time it goes over your time limit (in your example, one second), call your update and remove that time limit from your stashed time.

bit of pseudocode:
float squaretick = 1.0;
float squaretime = 0.0;

void MainLoop (float time)
{
    squaretime += time;
    if (squaretime > squaretick)
    {
        squaretime -= squaretick;
        UpdateSquare();
    }
}


That way you can call your loop as much as you like and your square will be updated once every squaretick time units.
Though, for smooth movement it would be better to just multiply the square's speed or what-have-you by the time passed into the loop as Tang mentions. Most games will do it that way.