Hello Guest

[CLOSED] getTime/getDelta

  • 18 Replies
  • 23249 Views
[CLOSED] getTime/getDelta
« 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 ?

*

Offline princec

  • *****
  • 1933
    • Puppygames
Re: [RFE] getTime/getDelta
« Reply #1 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 :)

Re: [RFE] getTime/getDelta
« Reply #2 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()

Re: [RFE] getTime/getDelta
« Reply #3 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 ;)
My github account and currently active project: https://github.com/matheus23/UniverseEngine

Re: [RFE] getTime/getDelta
« Reply #4 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.

Re: [RFE] getTime/getDelta
« Reply #5 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).

*

Offline ra4king

  • **
  • 58
  • I'm the King!
    • Roi's Website
Re: [RFE] getTime/getDelta
« Reply #6 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 ;)
-Roi

Re: [RFE] getTime/getDelta
« Reply #7 on: May 16, 2012, 11:18:36 »
Oops, yes, that should be:
Code: [Select]
long delta = ((time - lastTime) * resolution) / Sys.getTimerResolution();

Re: [RFE] getTime/getDelta
« Reply #8 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))...
My github account and currently active project: https://github.com/matheus23/UniverseEngine

Re: [RFE] getTime/getDelta
« Reply #9 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.

Re: [RFE] getTime/getDelta
« Reply #10 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.
Download Cultris II, the fastest Tetris clone from http://gewaltig.net/

*

Offline princec

  • *****
  • 1933
    • Puppygames
Re: [RFE] getTime/getDelta
« Reply #11 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 :)

Re: [RFE] getTime/getDelta
« Reply #12 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.

@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.

Re: [RFE] getTime/getDelta
« Reply #13 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)
Download Cultris II, the fastest Tetris clone from http://gewaltig.net/

Re: [RFE] getTime/getDelta
« Reply #14 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.
« Last Edit: May 25, 2012, 15:20:51 by matheus23 »
My github account and currently active project: https://github.com/matheus23/UniverseEngine