Pausing technique

Hello,

I just read an article in the JAVA 2D section about this pausing technique within the game loop:

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1102196419

Linuxhippy suggested the following:

… I would suggest to check every frame how long computation took and after this calculate how far charakters, animation etc. should have moved. …

I just thougt a bit about this, but came to no proper solution. What would be the best way of defining the speed of an object?

Any hint will be appreciated.

Thank you.

Ralf

using jbanes’s GAGETimer:


void run() {
            AdvancedTimer at = new AdvancedTimer();
            at.start();
            long tick = 0;
            while (running) {
                        render();
                        double seconds = (double)(at.getClockTicks()-tick) / AdvancedTimer.getTicksPerSecond();
                        runObjects(seconds);
                        tick = at.getClockTicks();
            }
}
public void runObjects(double seconds) {
            if (forwardPressed)
                        xVelocity += runAccel*seconds;
            else if (backwardPressed)
                        xVelocity -= runAccel*seconds;

            x += velocityX*seconds;
            y += velocityY*seconds;
}

That’s how I do it. I hope that helps! :slight_smile: Oh, and if anyone sees anything really wrong with that, please tell me so I can stop doing it myself… hehe

Are you after help with timing or with character movement speeds?

If timing, see this tutorial about timing in Java games:

http://grexengine.com/sections/externalgames/articles/Kevin%20Glass-Space%20Invaders%20101-2.html

Remember also that Java 5 now has a System.nanoTime() method.

Aside:
Does anyone know if this new method is accurate on Windows?
(i.e. updated more than once every 10ms and doesn’t drift horrendously?)

If you’re asking about character movement, I can only say “what I would do”, which is to define the movement of characters relative to each other and then find some way to translate these relative values into the right units.
If you’re wondering about what units to use, I would guess people use pixels for a 2D sprite-based game, or the units of the “game world” if you’re doing 3D, but I don’t really know.

[quote]Aside:
Does anyone know if this new method is accurate on Windows?
(i.e. updated more than once every 10ms and doesn’t drift horrendously?)
[/quote]
System.nanoTime() is extremely accurate, even on Windows. I can’t tell you how often it updates, but I can tell you it’s fast enough. I do have some concerns about random drift, but I don’t worry about it because it all seems to even out in the end.

[quote]Remember also that Java 5 now has a System.nanoTime() method.
[/quote]
Don’t worry. Users may forget, but GAGETimer won’t. If System.nanoTime() is available, it will be selected as the preferred timing method. :slight_smile: