[CLOSED] getTime/getDelta

Started by darkhog, May 09, 2012, 19:59:28

Previous topic - Next topic

darkhog

Since almost no game can live without those, could you add to Sys module getTime/getDelta functions as stated in http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_4_%28Timing%29 ?

princec

What do you want it to do that Sys.getTime() & Sys.getTimerResolution() doesn't do? (Or System.nanoTime() or System.currentTimeMillis())

Cas :)

darkhog

Quote from: princec on May 09, 2012, 22:04:40
What do you want it to do that Sys.getTime() & Sys.getTimerResolution() doesn't do? (Or System.nanoTime() or System.currentTimeMillis())

Cas :)

Nothing. It should be black box-type function that would make programmer's life less miserable (don't thinkering about those functions or searching wiki). This would greatly improve programming experience as it would make it easier and more fun. Those who want more control, will still be able to write their own functions, and those who don't would just call Sys.getTime() or Sys.getDelta()

matheus23

Quote from: darkhog on May 09, 2012, 22:47:19
Quote from: princec on May 09, 2012, 22:04:40
What do you want it to do that Sys.getTime() & Sys.getTimerResolution() doesn't do? (Or System.nanoTime() or System.currentTimeMillis())

Cas :)

Nothing. It should be black box-type function that would make programmer's life less miserable (don't thinkering about those functions or searching wiki). This would greatly improve programming experience as it would make it easier and more fun. Those who want more control, will still be able to write their own functions, and those who don't would just call Sys.getTime() or Sys.getDelta()

These Methods are split, because it would then be really unuseful, if you only get a method like this:

public static long getTime2() {
    return getTime() / getTimerResolution();
}

That would give you only a 1-second precise time number. Making that function totally useless.

With getTime() and getTimerResolution() you have the abilitiy to get Nano, or Milli-second time ;)
My github account and currently active project: https://github.com/matheus23/UniverseEngine

CodeBunny

I think he means something like:

public class Sys
{
    ......
    private long lastTime;

    // Returns the number of seconds that have passed since the last time getTime() has been called.
    public double getDelta()
    {
        long time = Sys.getTime();
        double delta = (double) (time - lastTime) / Sys.getTimerResolution();
        lastTime = time;
        return delta;
    }
    ......
}


By using a double, you get the maximum possible amount of accuracy, and you can then convert it to whatever format you want without losing accuracy if this is not sufficient.

And I'm all for this. It doesn't hamper anything already existing, it's a very simple method that is easily understood, and it would simplify the API for game loops. Sounds like a win all around.

CodeBunny

Additionally, you can keep the long format by using the following method instead:

    ......
    private long lastTime;

    // Returns the duration of time since the last call to getDelta, in the supplied resolution (1000 for milliseconds, etc)
    public long getDelta(long resolution)
    {
        long time = Sys.getTime();
        long delta = ((time - lastTime * resolution)) / Sys.getTimerResolution();
        lastTime = time;
        return delta;
    }
    ......


I prefer the first method, though (I like floating-point time stamps). It'd be simple to provide both, however (either synced together or independent).

ra4king

Quote from: CodeBunny on May 15, 2012, 19:16:27
Additionally, you can keep the long format by using the following method instead:

    ......
    private long lastTime;

    // Returns the duration of time since the last call to getDelta, in the supplied resolution (1000 for milliseconds, etc)
    public long getDelta(long resolution)
    {
        long time = Sys.getTime();
        long delta = ((time - lastTime * resolution)) / Sys.getTimerResolution();
        lastTime = time;
        return delta;
    }
    ......


I prefer the first method, though (I like floating-point time stamps). It'd be simple to provide both, however (either synced together or independent).
You multiply the difference of time and lastTime by resolution, not lastTime by resolution. I think that's what you meant since there are double parenthesis ;)
-Roi

CodeBunny

Oops, yes, that should be:
long delta = ((time - lastTime) * resolution) / Sys.getTimerResolution();

matheus23

Quote from: CodeBunny on May 15, 2012, 17:43:52
I think he means something like:

public class Sys
{
    ......
    private long lastTime;

    // Returns the number of seconds that have passed since the last time getTime() has been called.
    public double getDelta()
    {
        long time = Sys.getTime();
        double delta = (double) (time - lastTime) / Sys.getTimerResolution();
        lastTime = time;
        return delta;
    }
    ......
}


By using a double, you get the maximum possible amount of accuracy, and you can then convert it to whatever format you want without losing accuracy if this is not sufficient.

And I'm all for this. It doesn't hamper anything already existing, it's a very simple method that is easily understood, and it would simplify the API for game loops. Sounds like a win all around.

Oh.. never thought about doubles... I agree to that one. That Delta method could be easyly used for these movement methods (move(10 * getDelta(), 0))...
My github account and currently active project: https://github.com/matheus23/UniverseEngine

CodeBunny

Yup. Perfect for update loops.

This addition really makes a lot of sense to me, since it just encapsulates the recommended code that everyone is supposed to copy and paste.

Simon Felix

Won't work since getDelta() uses the current time. If you use it to move 100000 objects, the first object will move a little bit, the last one quite a bit more.

I also dislike the idea. If you want to rely on time, you should be forced to think about it. Otherwise time jumps (VMs) and freezes (window moving) will bite you. If you want to simplify things just provide a way to call two user-specified methods: One method that will be called once per frame for rendering and the other method exactly 60 times per second, except when the window is dragged/resized or when the system cannot keep up.
Download Cultris II, the fastest Tetris clone from http://gewaltig.net/

princec

I dislike the idea because it generally goes a bit beyond what LWJGL provides, which is the lowest-level binding to a common denominator API for the three OSes. Generally all API cruft of this nature is what I think LWJGL should be avoiding where possible.

Cas :)

CodeBunny

@Simon Felix
That's not how this would be used. Example:

while(!game.shouldExit())
{
    double delta = Sys.getDelta();
    for(Updateable object : objectsToUpdate)
    {
        object.update(delta);
    }
}


It's simply measuring the time passed since the last update and telling all the objects to update by that amount of time. All that it really does is encapsulate the recommended methods here: http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_4_%28Timing%29.

@princec
That's a sensible argument, and though I don't think this is really adding complexity (it's mainly just streamlining the use of methods already provided) I see your point.

I guess I wish that LWJGL had an additional, optional library that provided utility code of this nature. That way, people who only want the low-level binding can just use that, but you could have a higher-level API for more general development. We already have something similar in Slick-Util; it would just be nice if there was more general stuff in it.

Simon Felix

Quote from: CodeBunny on May 21, 2012, 13:25:36
@Simon Felix
That's not how this would be used. Example: ...

No question that it could be used correctly. However, matheus23 already wrote code snippet which demonstrates how it might get mis-used:
move(10 * getDelta(), 0)
Download Cultris II, the fastest Tetris clone from http://gewaltig.net/

matheus23

Quote from: Simon Felix on May 25, 2012, 11:37:44
Quote from: CodeBunny on May 21, 2012, 13:25:36
@Simon Felix
That's not how this would be used. Example: ...

No question that it could be used correctly. However, matheus23 already wrote code snippet which demonstrates how it might get mis-used:
move(10 * getDelta(), 0)


Lol...
that code snippet was ment like that:
double delta = getDelta();
// for every entity, and everything else:
object.move(10 * delta, 0);


EDIT: What I might should mention: I acctually wouldn't mis-use it in the real case.
My github account and currently active project: https://github.com/matheus23/UniverseEngine