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 & HelpPrograms › Help with increasing speed & changing color SLOWLY
Page Index Toggle Pages: 1
Help with increasing speed & changing color SLOWLY (Read 2208 times)
Help with increasing speed & changing color SLOWLY
Apr 27th, 2009, 6:47pm
 
How do I change a variable slowly over time?  I am trying to change the color and the speed of random ellipses.   I'm very new at Processing so forgive my messy code.  Here's what I have so far:


int sphereCount = 50;
float [] sphereSize = new float [sphereCount];
float [] xPosition = new float [sphereCount];
float [] yPosition = new float [sphereCount];
float [] xSpeed = new float [sphereCount];
float [] ySpeed = new float [sphereCount];

// These variables are used to determine the color of the particles

int redValue = 255;
int blueValue = 255;
int greenValue = 255;

void setup(){
 
 size(900,300);


 for(int i=0; i<sphereCount; i++){
   sphereSize[i] = random(10,120);
   xPosition[i] = random(0,width);
   yPosition[i] = random(0, height);
   xSpeed [i] = random(-2,2);
   ySpeed [i] = random(-2,2);
   
       
  }
   
}
void draw(){
background(0);

 
for(int i=0; i<sphereCount; i++){
 xPosition[i] += xSpeed[i];
 yPosition[i] += ySpeed[i];
 
 if(xPosition[i]>width){
  xSpeed[i] *= -1;
}

if(xPosition[i]<0){
  xSpeed[i] *= -1;
}

if(yPosition[i]>height){
  ySpeed[i] *= -1;
}

if(yPosition[i]<0) {
  ySpeed[i] *= -1;
}

/* When the mouse button is pressed the particles increase in
speed and the color slowly changes red.  This represents heat being
applied to the gas and causing the particles to agitate at higher
velocities */

 if (mousePressed == true) {
     xSpeed [i] += 0.05;
     xSpeed [i] += 0.05;
   redValue += 1;
   blueValue -= 2;
   greenValue -= 2;
   //delay(500);
 }
   //else {
   // xSpeed [i] = i;
   // ySpeed [i] = i;
    fill(redValue,blueValue,greenValue,90);
   //}
  // fill(255,0,0,90);
 
   
 smooth();
 
 noStroke();
 ellipse(xPosition[i],yPosition[i],sphereSize[i],sphereSize[i]);
}
println(redValue);
}

Also, I wanted to make the ellipses into 3D spheres.  Would this be pretty difficult to do starting from the code I have so far.

Thanks!

Stephanie

Re: Help with increasing speed & changing color SLOWLY
Reply #1 - Apr 27th, 2009, 6:50pm
 
I thought about using the ceil() function along with the delay function to change the color to red slowly...  RGB values need to be integers but if I increase the red value by 1 per frame the color immediately changes to red instead of slowly changing.
Re: Help with increasing speed & changing color SLOWLY
Reply #2 - Apr 27th, 2009, 8:38pm
 
how exactly should it change? just slowly turn to red and stay red? oder back to white again?
Re: Help with increasing speed & changing color SLOWLY
Reply #3 - Apr 28th, 2009, 1:24am
 
The problem is that the value increases by 1, but 60 times per second! (per default). So even a brief pressing will go fast.
delay() is rarely needed, as it stops the sketch.
A solution is to increase the color only every n frames.
The test should look like:

if (mousePressed && frameCount % 20 == 0) {
% is modulo, remainder of integer division. So it is zero when the frameCount is a multiple of 20, ie. 3 times per second. You can lower the value for faster change.
Re: Help with increasing speed & changing color SLOWLY
Reply #4 - Apr 28th, 2009, 11:01am
 
Hi Stephanie,

the revelation you're looking for is that the red, green and blue values passed to background(), fill(), and stroke() do not need to be integers.

Increment/decrement by fractional values to your heart's content! Smiley

If you have this problem for other programming tasks, remember that you can always use floats to keep track of progress and convert the float value to an int as and when you need it.

eg

Code:
myFloat += 0.01;
myIntFunction(int(myFloat));


-spxl
Re: Help with increasing speed & changing color SLOWLY
Reply #5 - Apr 28th, 2009, 11:33am
 
With regards to spheres in place of ellipses...

Open your window with size(300, 900, P3D) or size(300, 900, OPENGL). Processing will complain if you have included the OPENGL library, which you can do by one of the menu options.

The sphere(radius) function draws a sphere at the current "origin", that is, at (0,0,0). To "move" it away from the origin the trick is to move the origin using translate(). You'll then want to "put the origin back" so you don't lose track of where you are (translate() moves relative to the current state).

eg

pushMatrix(); // save the 3D state
translate(xPosition[i], yPosition[i], zPosition[i]);
sphere(sphereSize[i]);
popMatrix(); // restore the 3D state

If you don't want to use z positions, just use 0 for the z-coordinate.

Another thing you might want to consider is a change in coordinates. Your program uses x values from 0 to width, y values from 0 to height. In "3D", it's not so easy to know the exact pixel coordinates (on the screen) for 3D coordinates, so it is often helpful to start off with your 3D coordinates centred on the screen and use coordinates from -something to +something, where you might need to experiment as to how big the something should be.

To do this, just move the origin somewhere near the top of the draw() function (before you do any of your drawing).

translate(width / 2, height / 2);  // move the origin to the middle of the screen

The coordinates of the screen are now something like...

(-w/2, -h/2, 0) --- (0, -h/2, 0) --- (w/2, -h/2, 0)
|
|
(-w/2, 0, 0) -------- (0, 0, 0) ----- (w/2, 0, 0)
|
|
(-w/2, h/2, 0) ---- (0, h/2, 0) --- (w/2, h/2, 0)

where w represents the width and h represents the height (and you have to excuse the dodgy text diagram!). Changing the z values from 0 will give the apparent effect of making things bigger or smaller relative to the centre of the screen (perspective).

-spxl
Page Index Toggle Pages: 1