#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# DeltaTime

Hey, I'm doing a game and I'd like to know if it's possible to have the delta time in Processing ? :)

Tagged:

• I'm afraid those delta nano time calculations are done internally via local variables: :(
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L2379

Most we can access is the `protected` field frameRateLastNanos.
Which is nothing more than the previous System.nanoTime():
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L2445

There's also field frameRate:
https://Processing.org/reference/frameRate.html

Dunno how useful that'll be for ya, but here's some test I did now: 8-|

``````long prev;

void setup() {
frameRate(60);
println(1e3/60); // 60 FPS = 1e3/60 = 16.666666
}

void draw() {
double delta = (-prev + (prev = frameRateLastNanos))/1e6d;
println(delta, TAB, frameRate);
}
``````
• I'm checking it, deltaTime is used for not base a program on processor power

• you can maybe use millis() or access the underlying java millisecond and "nanosecond" timers.

http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime

if you set the framerate as high as possible then it'll effectively disable the 60fps 'ticks' that are the default.

• What are "1e3" and "1e6d" ??

• 1000 and 1000000

engineering notation for numbers, used to confuse people.

https://en.wikipedia.org/wiki/Engineering_notation

the d in 1e6d is to denote a double precision floating point number.

• A simple method is to just use `millis()`, which will give you the time from sketch start in milliseconds. This doesn't give you nanoseconds, but should be fine for many applications.

To compute a time difference, just save millis() to a variable on an event -- then compare that variable to the current millis(), e.g.:

``````int lastTime = 0;
int delta = 0;
void draw(){
delta = millis() - lastTime;
println(delta);
// draw code here
lastTime = millis();
}
``````

A simple example of a timed action is to test how long a key was held down:

``````void draw(){
// draw code here
}
void keyPressed(){
int time1 = millis();
}
void keyReleased(){
int time2 = millis() - time1;
println(time2)
}
``````
• That's the same thing, millis or nanosecond ^^

• you might need to use a long rather than an int for nanoTime(). and nanoseconds may be negative... see the javadoc for System.nanoTime()

• @poisson86 -- re:"That's the same thing" -- no, I don't believe that milliseconds and nanoseconds are the same thing -- from discussion I've seen I believe that System.nanoTime is different, e.g.

nanoTime is usually significantly more accurate than currentTimeMillis but it's a relatively expensive call as well.currentTimeMillis() runs in a few (5-6) cpu clocks, nanoTime depends on the underlying architecture and can be 100+ cpu clocks.