We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › New Library for Time the start of PolymonkeyLibs
Page Index Toggle Pages: 1
New Library for Time the start of PolymonkeyLibs (Read 2435 times)
New Library for Time the start of PolymonkeyLibs
Dec 29th, 2009, 3:03am
 
Hi All,

I'm in the process of publishing a lot of the common code that I've been using over the years and I thought why not start a collection. And so PolymonkeyLibs begins. http://code.google.com/p/polymonkey/

The first official package is polymonkey.time it features 3 classes that relate to (you guessed it) time.

Time - used for keeping track of TimeDelta (the time in seconds of the previous frame) can also speed up and slow down time.

TimerEvent - A helper class that will automatically call a timerEvent() method in a set number of seconds.

StopWatch - A helper class for timing how long an operation takes.

Docs, Examples and version 001 of the library are in the Download tab of the page above. If you've got questions / problems / requests then send em this way.

Matt Ditton


Disclaimer: Although the internals are as accurate as possible. Everything gets converted to floats or ints when talking to processing. So your mileage my vary. And although it works like a charm I wouldn't use it to run a pacemaker or anything.
Re: New Library for Time the start of PolymonkeyLibs
Reply #1 - Dec 29th, 2009, 7:51pm
 
Hey Matt,

it's always nice to look under the hood of other people's projects and see some of the same ideas that you've had.

I just had a look at the time.java file and saw

Code:
previousTime = currentTime;

currentTime = System.currentTimeMillis();
   
// remember kids: time is counted as a long with 1000 to a second (millisecond)
// hense the 0.001f to get the float value of deltaTime.
deltaTime = (currentTime - previousTime) * 0.001d * timeScale;
   
countTime += deltaTime;


..then went to look inside something I made in January, Esfera mod by spxl 6, and see almost exactly the same process:

Code:
// Timing calculations
clockPrevMillis = clockMillis;
clockMillis = millis();
 
int millisSinceLastFrame = clockMillis - clockPrevMillis;
 
// frameMillis and runMillis are affected by the timeWarp...
int frameMillis = int(millisSinceLastFrame * timeWarp);
runMillis += frameMillis;


About the only difference is that I've kept the times in milliseconds, and my code appears in the main draw() function of my sketch.

Also, I hadn't gone on to timer events or anything like that, but did create an Oscillator class with it's own kind of automation - though you have to call the tick() method each frame.

Hmm... Looking some more at your code - I don't entirely follow what is going on in TimerEvent... I'm supposing it calls a method called "timerEvent" in the main sketch every targetTime seconds (of elapsed Time.deltaTime), but I'm not familiar with things like "parent.getClass().getMethod("timerEvent", new Class[] { TimerEvent.class });"

I see that the StopWatch uses system time, not the Time time. I have some questions about it:

In getResultMillis() and getResultSeconds(), if "run" is 1 (started) then you return t2... which will be zero if you've only started the stopwatch once. Do you think using System.currentTimeMillis() - t1 would make more sense?

Every time you start(), t1 is set based on the current system clock (so it is essentially a reset). How about being able to use start() and stop() multiple times to get a cumulative count, with a separate reset() method?

From what I remember years ago having stopwatches on watches (I don't use a watch anymore - time from my phone), there are two buttons: start/stop and split/reset. Start/stop controls the flow of time - like a play/pause button on a CD player. Split allows you to pause the display for reading) whilst the clock keeps ticking in the background - we don't need that in code, since the return value from the getResult methods is "instantaneous" and can be stored in a variable by the caller. A reset() method could be useful though, and work both running and stopped ("run" values 1 and 2).

-spxl
Re: New Library for Time the start of PolymonkeyLibs
Reply #2 - Dec 29th, 2009, 9:56pm
 
Hi spxl,

You know what they say about great minds Wink

I modeled the Time.getDeltaTime() idea on Unity's system of deltaTime. Which is really useful when moving objects. So if you want the position to move a quarter the width in one second then all you need to do is

Code:

float speed = width/4;

pos += speed * time.getDeltaTime();


Nice catch with the StopWatch. I added the ability to get the result before calling stop and I never tested it. I'm adding the fix now. And adding that reset() while I'm there. I've also added a RunningTotal to the whole thing.

On the subject of TimerEvent. This kind of thing "parent.getClass().getMethod("timerEvent", new Class[] { TimerEvent.class });" comes from Java Reflection. That's how the class is able to call a method in the main sketch. There is a nice example of it in the library section of dev.processing.org http://dev.processing.org/libraries/basics.html (it's about 3/4 down the page)

MattD
Re: New Library for Time the start of PolymonkeyLibs
Reply #3 - Dec 30th, 2009, 8:19am
 
timerevents are great! im also doing something like this in a timeline class im adding to my tween/animation library. i should be through it with early next month...
Re: New Library for Time the start of PolymonkeyLibs
Reply #4 - Dec 30th, 2009, 8:53pm
 
Sweet, that tween library is very nice btw...
Page Index Toggle Pages: 1