LWJGL
May 23, 2013, 08:37:29 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.9.0 released!
 
   Home   Help Search Login Register  



Pages: [1] 2
  Print  
Author Topic: [RFE] getTime/getDelta  (Read 3154 times)
darkhog
Newbie
*
Posts: 3


« on: May 09, 2012, 12: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 ?
Logged
princec
Nerdus Imperius
*****
Posts: 1870



WWW
« Reply #1 on: May 09, 2012, 15:04:40 »

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

Cas Smiley
Logged

darkhog
Newbie
*
Posts: 3


« Reply #2 on: May 09, 2012, 15:47:19 »

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

Cas Smiley

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()
Logged
matheus23
Regular nerd
**
Posts: 62


« Reply #3 on: May 10, 2012, 08:20:12 »

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

Cas Smiley

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 Wink
Logged

My github account and currently active project: https://github.com/matheus23/UniverseEngine
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #4 on: May 15, 2012, 10:43:52 »

I think he means something like:

Code:
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.
Logged
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #5 on: May 15, 2012, 12:16:27 »

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

Code:
    ......
    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).
Logged
ra4king
Newbie
*
Posts: 33


I'm the King!


WWW
« Reply #6 on: May 15, 2012, 13:27:07 »

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

Code:
    ......
    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 Wink
Logged

-Roi
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #7 on: May 16, 2012, 04:18:36 »

Oops, yes, that should be:
Code:
long delta = ((time - lastTime) * resolution) / Sys.getTimerResolution();
Logged
matheus23
Regular nerd
**
Posts: 62


« Reply #8 on: May 16, 2012, 04:36:13 »

I think he means something like:

Code:
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))...
Logged

My github account and currently active project: https://github.com/matheus23/UniverseEngine
CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #9 on: May 16, 2012, 10: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.
Logged
Simon Felix
Talks Too Much
***
Posts: 103



WWW
« Reply #10 on: May 20, 2012, 18: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.
Logged

Download Cultris II, the fastest Tetris clone from http://gewaltig.net/
princec
Nerdus Imperius
*****
Posts: 1870



WWW
« Reply #11 on: May 21, 2012, 01: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 Smiley
Logged

CodeBunny
Nerdus Imperius
*****
Posts: 561



WWW
« Reply #12 on: May 21, 2012, 06:25:36 »

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

Code:
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.
Logged
Simon Felix
Talks Too Much
***
Posts: 103



WWW
« Reply #13 on: May 25, 2012, 04: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:
move(10 * getDelta(), 0)
Logged

Download Cultris II, the fastest Tetris clone from http://gewaltig.net/
matheus23
Regular nerd
**
Posts: 62


« Reply #14 on: May 25, 2012, 08: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:
move(10 * getDelta(), 0)

Lol...
that code snippet was ment like that:
Code:
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.
Logged

My github account and currently active project: https://github.com/matheus23/UniverseEngine
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!