LWJGL Forum

Archive => Resolved Bugs/RFE => Topic started by: darkhog on May 09, 2012, 19:59:28

Title: [CLOSED] getTime/getDelta
Post by: darkhog on May 09, 2012, 19:59:28
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 ?
Title: Re: [RFE] getTime/getDelta
Post by: 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 :)
Title: Re: [RFE] getTime/getDelta
Post by: darkhog on May 09, 2012, 22:47:19
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()
Title: Re: [RFE] getTime/getDelta
Post by: matheus23 on May 10, 2012, 15:20:12
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 ;)
Title: Re: [RFE] getTime/getDelta
Post by: CodeBunny on May 15, 2012, 17:43:52
I think he means something like:

Code: [Select]
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.
Title: Re: [RFE] getTime/getDelta
Post by: CodeBunny on May 15, 2012, 19:16:27
Additionally, you can keep the long format by using the following method instead:

Code: [Select]
    ......
    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).
Title: Re: [RFE] getTime/getDelta
Post by: ra4king on May 15, 2012, 20:27:07
Additionally, you can keep the long format by using the following method instead:

Code: [Select]
    ......
    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 ;)
Title: Re: [RFE] getTime/getDelta
Post by: CodeBunny on May 16, 2012, 11:18:36
Oops, yes, that should be:
Code: [Select]
long delta = ((time - lastTime) * resolution) / Sys.getTimerResolution();
Title: Re: [RFE] getTime/getDelta
Post by: matheus23 on May 16, 2012, 11:36:13
I think he means something like:

Code: [Select]
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))...
Title: Re: [RFE] getTime/getDelta
Post by: CodeBunny on May 16, 2012, 17:30:08
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.
Title: Re: [RFE] getTime/getDelta
Post by: Simon Felix on May 21, 2012, 01:58:27
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.
Title: Re: [RFE] getTime/getDelta
Post by: princec on May 21, 2012, 08:54:08
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 :)
Title: Re: [RFE] getTime/getDelta
Post by: CodeBunny on May 21, 2012, 13:25:36
@Simon Felix
That's not how this would be used. Example:

Code: [Select]
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 (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.
Title: Re: [RFE] getTime/getDelta
Post by: Simon Felix on May 25, 2012, 11:37:44
@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:
Code: [Select]
move(10 * getDelta(), 0)
Title: Re: [RFE] getTime/getDelta
Post by: matheus23 on May 25, 2012, 15:19:10
@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:
Code: [Select]
move(10 * getDelta(), 0)

Lol...
that code snippet was ment like that:
Code: [Select]
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.
Title: Re: [RFE] getTime/getDelta
Post by: CodeBunny on May 26, 2012, 04:00:44
No question that it could be used correctly. However, matheus23 already wrote code snippet which demonstrates how it might get mis-used:

All code can be used wrongly - what's important is how easily it can be used correctly.

You don't constrain an API on the sole reason people could attempt retarded things with it.
Title: Re: [RFE] getTime/getDelta
Post by: princec on May 26, 2012, 09:43:06
The more API we add to LWJGL beyond that which is minimally necessary to achieve what it does, the more API nobody knows about, the more potential bugs and the more maintenance. This is why we have historically avoided adding cruft like this to the core classes and plonked them into the util jar.

Cas :)
Title: Re: [RFE] getTime/getDelta
Post by: matheus23 on May 26, 2012, 15:46:54
No question that it could be used correctly. However, matheus23 already wrote code snippet which demonstrates how it might get mis-used:

All code can be used wrongly - what's important is how easily it can be used correctly.

You don't constrain an API on the sole reason people could attempt retarded things with it.

You could add a static variable easily in some class like "DeltaUtil", and call
Code: [Select]
DeltaUtil.update() every game-cycle, which is doing the exact same thing:
Code: [Select]
[variable-name] = getDeltaTimePrivate(); // Maybe another name and let getDelta() do this:
Code: [Select]
public double getDelta() {
    return [variable-name];
}

That's it ;)
Title: Re: [RFE] getTime/getDelta
Post by: CodeBunny on May 27, 2012, 05:50:14
Quote
The more API we add to LWJGL beyond that which is minimally necessary to achieve what it does, the more API nobody knows about, the more potential bugs and the more maintenance. This is why we have historically avoided adding cruft like this to the core classes and plonked them into the util jar.

Cas :)

Makes sense.

@Simon
That seems like a less intuitive API to me.