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 › Why not following frame rate (novice)
Page Index Toggle Pages: 1
Why not following frame rate? (novice) (Read 1609 times)
Why not following frame rate? (novice)
Sep 20th, 2009, 3:14pm
 
I'm just starting to learn Processing. I've been able to produce some nice animated line drawings, but every time I try to use 'for' to control things, I lose the connection with the frame rate and get something that moves at hyper-speed.  It seems to me, for example, that the following code should produce a moving line per the frame rate, but instead it produces an apparent solid block. Any help you can offer this newbie will be much appreciated.

void setup() {
 size(400, 400);
 frameRate(50);
}

void draw() {
 background(125);
 for (int i = 0; i < 255; i++) {
   line(i, 50, i, 200);
 }
}
Re: Why not following frame rate? (novice)
Reply #1 - Sep 20th, 2009, 3:25pm
 
Think of draw(){ stuff } as: "do this within each frame."  So if you're running from 0 to 255, that'll happen each frame...if you want it to only go one step per frame, you need to declare the counter variable outside of the loop:
Code:

int i=0;
void draw(){
stuff.do();
i++; // or: i=(i+1)%255 to make a repeating loop
}
Re: Why not following frame rate? (novice)
Reply #2 - Sep 20th, 2009, 3:33pm
 
This is the code to do what you want.

Code:
int i = 0; // or whatever name you call it

void setup() {
size(400, 400);
frameRate(50);
}

void draw() {
background(125);
line(i, 50, i, 200);
i++;
if (i>height) {
i = 0;
}
}


Your for loop is doing something completely different, it is drawin a line under a line under a line under a... 255 times on each frame.
Re: Why not following frame rate? (novice)
Reply #3 - Sep 21st, 2009, 4:07am
 
Thanks to both for the replies. In that case, it's not clear to me how 'for' and 'while' can function in conjunction with draw. To be a bit more specific about my aims: I'd like to oscillate between incrementing and decrementing a variable, setting a random limit for either direction within a global range. E.g. within the global range 0-255, choose a value at random and a limit at random towards which that value will move. When it's reached that limit, choose a new limit and move towards it. And to do the increment/decrement and test only once every time a frame is drawn.

Another question is which books or online resources you recommend  that have more expansive explanations of the syntax.

Thanks.
Re: Why not following frame rate? (novice)
Reply #4 - Sep 21st, 2009, 4:25am
 
That's a topic I plan to put in my own Processing FAQ (currently only in my head...), as it is a frequently hit problem.

Think of draw() as a loop itself. Rather an infinite while() loop, so you have to manage your own counter(s), update them, test them within the function itself.
0p0 shows something along these lines (and what you want): global declaration is the (int i = 0; part, if (i > height) { is the i < height; (inverted) part and i++; is the same.
Re: Why not following frame rate? (novice)
Reply #5 - Sep 21st, 2009, 8:11am
 
A friend at work gave me a solution that involves initializing a random starting value and a random target value, then going into 'draw' with this structure:

if value = target, set new target

else if value < target, increment

else if value > target, decrement

It's good enough for now. If there are other solutions, I'm happy to hear them.
Re: Why not following frame rate? (novice)
Reply #6 - Sep 21st, 2009, 10:46am
 
This only slightly different from what your friend suggests, but I usually use a multiplier:

float dir=1;
if ((value > maxValue) || (value < minValue)) dir = -dir;
value += incrementAmount * dir;

But, it depends what kind of oscillation you want, for instance, if you want something a little more natural and less zigzaggy, sin() is a useful function.  Essentially, you increment (and loop) an angle around and around a circle (max value == TWO_PI), and by taking the sin() of that angle you get a number that ranges naturally from -1 to 1.  (Traveling along the wave.)  You can use that result as a multiplier to get your oscillation:

Quote:
float a=0;

void setup(){
  size(400,100);
  noStroke();
}

void draw(){
  fill(0,15);
  rect(0,0,width,height);
  a = (a+.03) % TWO_PI;
  fill(255);
  ellipse(width/2+sin(a)*(width/3), height/2, 15, 15);
}
Re: Why not following frame rate? (novice)
Reply #7 - Sep 22nd, 2009, 11:03am
 
Thanks, BenHem - I've not had a chance to test your method yet, but I'll put it in the 'library' for later consideration.
Re: Why not following frame rate? (novice)
Reply #8 - Sep 25th, 2009, 2:15pm
 
BenHem, i did work with your code today. It does produce a more fluid motion, thanks. I do have 72 values being calcuated via sin() every frame, and I wonder if that is a drag on the rendering or not - I'm using 40 fps at the moment. I wish I understood the trig better so that I could control it more - can you recommend any sources that explain it and how it relates to code?

many thanks
Re: Why not following frame rate? (novice)
Reply #9 - Sep 26th, 2009, 9:24pm
 
sorry, didn't understand what you meant at first.  No, calculating 72 values shouldn't slow you down at all.  If you want a comparative speed value, you can use frameRate(999); to break the speed limit, and println(frameRate); to see the fps.

Here's a good illustration of what's happening to make a sine wave:
http://en.wikipedia.org/wiki/Sine_wave#Occurrences
Re: Why not following frame rate? (novice)
Reply #10 - Oct 1st, 2009, 7:07am
 
hmm - interesting about the frame rate. I didn't realize that it varied so much. Can you explain that a bit? I imagine when it's lower than the requested rate it's simply a matter of being unable to keep up. But what about when it's a bit higher?

Another question that may or may not be related: can you tell me why my lines are sometimes jittery in this program?

http://www.mti.dmu.ac.uk/~rherrema/applet/

The frame rate here is 400, which I suppose is unnecessarily high, but I get the same problem even if I use something like 35 fps. The code is using bezier curves and your recommended sin functions.
Re: Why not following frame rate? (novice)
Reply #11 - Oct 3rd, 2009, 4:07am
 
ben_hem wrote on Sep 21st, 2009, 10:46am:
This only slightly different from what your friend suggests, but I usually use a multiplier:

float dir=1;
if ((value > maxValue) || (value < minValue)) dir = -dir;
value += incrementAmount * dir;



The problem I keep running into, Ben, is that the -dir only lasts for one interation - as soon as the value is back within range, it's +dir again, and so the process simply stalls. How can I set the sign of the dir to last until the next time value exceeds a boundary
Page Index Toggle Pages: 1