Code

// based off of code by Zachary Lieberman from "Making Things Move" example 02 _ 06


int numFish = 50;


Fish school[];          // declare class (who moves)

//---------------------------------------------------------
void setup(){

  size(600,400);

  school = new Fish[numFish];   //allocation
  for (int i=0; i<numFish; i++){
    school[i] = new Fish();  //construction

  }
}

//---------------------------------------------------------
void draw(){

  background(15,35,75);

  for (int i=0; i<numFish; i++){

    // set attraction properties
    school[i].attraction();

    // draw fish
    school[i].render();

  }
}


//-----------------------------------------------------------------

class Fish {


  float x,y,w,h;
  float xOld, yOld;                    // snapShots of prev position;
  float angle;                         // based on my last position, what is my angle?

  float blurRate;                      // controls the speed at wich we "catch" the target
  float one_m_blurRate;                // one minus blur rate


//-----------------------------------------------------------------
  Fish (){

    w = 30;
    h = 15;
    x = random(0, width);
    y = random(0, height);

    blurRate = 0.98;
    one_m_blurRate = 1 - blurRate;
  }

//-----------------------------------------------------------------
  void attraction(){

    if (abs (mouseX-x) <100 && abs (mouseY - y) <100){

      float catch_x = mouseX;
      float catch_y = mouseY;

      x = blurRate * x + one_m_blurRate * catch_x;
      y = blurRate * y + one_m_blurRate * catch_y;

      // now calculate angle:
      float xDiff = x - xOld;      
      float yDiff = y - yOld;  
      angle = (float)atan2(yDiff, xDiff);



      // take snapshot:
      xOld = x;
      yOld = y;


    }
  }


//-----------------------------------------------------------------
  void render(){


    pushMatrix(); 
    translate(x,y);
    rotate(angle);  
    ellipseMode(CENTER);        // use x y as center, not corner
    noStroke();
    fill(249, 237, 237);            
    ellipse(0,0,w,h);


    popMatrix();
  }

}


050 -- Dynamic Simulations I: Individual Particles: Due 11/1

Statement:Create an interactive system in which multiple instances of a single type of unconnected particles are influenced by forces that are (at least partially) governed by user input. By "unconnected", I mean that there are no interparticle forces of any kind, e.g. no springs, interparticle collisions, flocking forces, etc. However, you can have particles influenced by forces from features of their environment, such as the cursor, walls, static objects, or a priori gradient fields.

For some possible inspiration, you could look into some projects by Look into works by Casey Reas, Lia, and Marius Watz; but you need not make works which are specifically accretive, as many of their examples are.

For assignments numbered 05x, you are welcome to use code from the examples that I showed in class. You may also find the following examples helpful:
-- magnet forces
-- wall collisions

hide statement