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.
IndexProgramming Questions & HelpSyntax Questions › Time based animation
Page Index Toggle Pages: 1
Time based animation (Read 1339 times)
Time based animation
Jul 22nd, 2009, 2:24pm
 
Hi, I am tryin to work out how to do a time based animation using millis()

Basically I am translating Actionscript 3 code to Processing, but I think the way getTimer() in AS3 is different from the millis() method, this is the formula translated in the draw funciton

Code:

PVector position;
PVector velocity;
float gravity = 32;
float bounce = -0.6;
int oldTime;
int radius = 20;


void setup() {
 size(300, 300);
 smooth();
 //frameRate(30);
 ellipseMode(CENTER);
 
 oldTime = 0;
 
 position = new PVector(50, 50);
 velocity = new PVector(10, 0);
}

void draw() {
 background(0);
 
 // *** translated formula
 int time = millis();
 float elapsed = (time - oldTime) / 1000;
 oldTime = time;
 
 PVector accel = acceleration(position, velocity);
 position.x += velocity.x * elapsed;
 position.y += velocity.y * elapsed;
 velocity.x += accel.x * elapsed;
 velocity.y += accel.y * elapsed;
 
 noStroke();
 fill(255);
 ellipse(position.x, position.y, radius, radius);
 
 println(time + " - " + oldTime);
}

PVector acceleration(PVector p, PVector v) {
 return new PVector(0, gravity);
}


but I get elapsed = 0 of course

Any ideas how can I achive this in Processing?

Cheers
rS
Re: Time based animation
Reply #1 - Jul 22nd, 2009, 2:48pm
 
Not sure what is your exact issue (it is late!) but at least I would init oldTime to millis() in setup().
Re: Time based animation
Reply #2 - Jul 22nd, 2009, 3:44pm
 
Ys I did that, but got the same results, the issue is that the elapsed time is always 0 because there is no difference between time and oldTime

Cheers
rS
Re: Time based animation
Reply #3 - Jul 22nd, 2009, 5:14pm
 
Maybe the timelime Librarie is something for you :
http://processing.org/discourse/yabb2/num_1240800499.html

Re: Time based animation
Reply #4 - Jul 22nd, 2009, 10:38pm
 
You can change the 1000 in your division to 1000.0 to make Java keep the precision.

Edit: Below is not necessary
Use float for oldTime and time instead of int. Your values are getting cut off (rounded to zero).
Re: Time based animation
Reply #5 - Jul 23rd, 2009, 12:05am
 
@NoahBuddy
Thanks is working fine now, the problem was using int, the values where getting rounded, therefore elapsed = 0, now I get values of 0.016

I need to pay more attention when translating code, very silly mistake

Cheers
rS
Page Index Toggle Pages: 1