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 › pulsating rectangle
Page Index Toggle Pages: 1
pulsating rectangle (Read 537 times)
pulsating rectangle
Dec 2nd, 2009, 3:06am
 
Im trying to get a rectangle to "pulse" 3 pixels. it should be easy but my if counter wont detect my said limit.

what am I doing wrong? Why wont it detect when  the int A hits 3 ?



Code:
int a = 50;
int pulse = 1;

void setup(){
size(200,200);
}

void draw(){

 rectMode(CENTER);
rect(width/2,height/2,a+=pulse,a+=pulse);

if(a==1){
a++;
}
if(a==3){
a--;}
println(a);
}
Re: pulsating rectangle
Reply #1 - Dec 2nd, 2009, 3:25am
 
there are more things messed up...

first by using a+=pulse inside your rect you dont only define the width/height but also add pulse to a

then you check if a == 1;
but you already start with 50, so it never gets 1.

anyway i would do it different

define your size ( could be a in your case) and a pulse variable.
you already did that.

now, only change the pulse and add it to a inside the rect command

now we need to change the pulsing. you could add up 1 like you tried. but if you want to get much smoother results you could make use of a sin function that always changes between -1 and 1

use frameCount as it is a counter we can easily make use of for the sin.

void draw(){
 smooth();
rectMode(CENTER);
background(0);
float a = 10;
float pulse = abs(sin(frameCount/20.0))*10.0; //20 is pulse speed. 10 is the resizing when pulsing
rect(50,50,a+pulse,a+pulse);

}
Re: pulsating rectangle
Reply #2 - Dec 2nd, 2009, 3:25am
 
You are initializing a to 50 so it will never reach 3 nor 5, because none of the conditions will met

What you need to do is init a to 0, and increment a to the rectangle width and height, and compare the results

Hope that helps
rS
Page Index Toggle Pages: 1