FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   code for infinite fountain
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: code for infinite fountain  (Read 2061 times)
julijas

Email
code for infinite fountain
« on: Apr 6th, 2005, 4:09pm »

Hi all,
 
I am developing code for game with serial input.
This code demonstrate fountain simulation based on mouseX input. But this program is limitness. Particles stop flowing when variable terminate.  
How to make this flowing endless, infinity?
How to make more fountains based on separate inputs-variables?
 
Thanks for help in advance!
 
Julijas
 
int MAX_array =1000;
int max_pix_amount = 0;
int pix_amount =0;
 
pix[] pix_fountain = new pix[MAX_array];
 
void setup() {
  size(400,400);
  for (int i = 0; i < MAX_array; i++) {
    float gray = random(255);
    pix_fountain[i] = new pix (gray,0,0,random(0,10),random(0,4));
  }
}
 
void loop() {
  background(255);
  pix_amount = mouseX/10;
  max_pix_amount += pix_amount;
  // if (max_pix_amount >MAX_array) { max_pix_amount =0;
  //} it works for to spray infinity of pixels (rects) but after some time it start twinkle
 
  for (int i = 0; i < max_pix_amount; i++) {
    pix_fountain[i].spray();
    pix_fountain[i].draw();
 
  }
}
 
class pix
{
 
  float g;
  float xpos;
  float ypos;
  float xvel;
  float yvel;
 
  pix(float g_, float xp, float yp, float xv,float yv) {
    g = g_;
    xpos = xp;
    ypos = yp;
    xvel = xv;
    yvel = yv;
  }
 
  void draw () {
    rectMode(CENTER_DIAMETER);
    fill(g);
    noStroke();
    rect(xpos,ypos,5,5);
  }
 
  void spray () {
    xpos = xpos + (mouseX/100)+ xvel;
    ypos = ypos + (mouseX/150) + yvel;
 
  }
}
 
st33d

WWW Email
Re: code for infinite fountain
« Reply #1 on: Apr 7th, 2005, 12:54am »

Either put pixels back at start:
Code:

int MAX_array =1000;
int max_pix_amount = 0;
int pix_amount =0;
pix[] pix_fountain = new pix[MAX_array];
void setup() {
  size(400,400);
  for (int i = 0; i < MAX_array; i++) {
    float gray = random(255);
    pix_fountain[i] = new pix (gray,0,0,random(0,10),random(0,4));
  }
}  
void loop() {
  background(255);
  pix_amount = mouseX/10;
  if (max_pix_amount < MAX_array){
  max_pix_amount += pix_amount;
  }
  if (max_pix_amount > MAX_array){
  max_pix_amount = MAX_array;
  }
  for (int i = 0; i < max_pix_amount; i++) {
    pix_fountain[i].spray();
    pix_fountain[i].draw();
    pix_fountain[i].check_lost();
  }
}
 
class pix
{
  float g;
  float xpos;
  float ypos;
  float xvel;
  float yvel;
  pix(float g_, float xp, float yp, float xv,float yv) {
    g = g_;
    xpos = xp;
    ypos = yp;
    xvel = xv;
    yvel = yv;
  }
  void draw () {
    rectMode(CENTER_DIAMETER);
    fill(g);
    noStroke();
    rect(xpos,ypos,5,5);
  }
  void spray () {
    xpos = xpos + (mouseX/100)+ xvel;
    ypos = ypos + (mouseX/150) + yvel;
  }
  void check_lost () {
  if (xpos > width || ypos > height){
  xpos = 0;
  ypos = 0;
  }
  }
}

Or maybe go crazy:
Code:

int pix_amount = 0;
pix[] pix_fountain = new pix[1];
void setup(){
  size(400,400);
  pix_fountain[0] = new pix (random(255),0,0,random(0,10),random(0,4));
}
void loop(){
  background(255);
  pix_amount = (mouseX/10)+1;
  new_fountain(pix_fountain[pix_fountain.length-1],pix_amount);
  for (int i = pix_fountain.length-(pix_amount)-1; i < pix_fountain.length-1; i++){
    pix_fountain[i] = new pix (random(255),0,0,random(0,10),random(0,4));
  }
  for (int i = 0; i < pix_fountain.length-1; i++){
    pix_fountain[i].spray();
    pix_fountain[i].draw();
  }
}
class pix{
  float g;
  float xpos;
  float ypos;
  float xvel;
  float yvel;
  pix(float g_, float xp, float yp, float xv,float yv){
    g = g_;
    xpos = xp;
    ypos = yp;
    xvel = xv;
    yvel = yv;
  }
  void draw(){
    /*
    rectMode(CENTER_DIAMETER);
    fill(g);
    noStroke();
    rect(xpos,ypos,5,5);
    */
    point(xpos,ypos);
  }
  void spray(){
    xpos = xpos + (mouseX/100)+ xvel;
    ypos = ypos + (mouseX/150) + yvel;
  }
}
 
//increases array
void new_fountain(pix new_p, int amount){
  pix [] temp = new pix[pix_fountain.length + amount];
  System.arraycopy(pix_fountain, 0, temp, 0, pix_fountain.length);
  temp[pix_fountain.length] = new_p;
  pix_fountain = temp;
}

You can see that you can make objects as you go. So you might have several fountains and have them switch on for the different input.
 
Hope this helps.
 

I could murder a pint.
julijas

Email
Re: code for infinite fountain
« Reply #2 on: Apr 7th, 2005, 4:33pm »

Thank you, st33d!
Its great!
One another question:
How to make flow more depedent on input? (when input is equal to 0, particles nevertheless is flowing, but I don't need it), I tryed like this: if (mouseX<=0){ pix_amount=0;} ,but it doesn't work..
 
Pages: 1 

« Previous topic | Next topic »