+++ CITIES AND MONSTERS +++

processing workshop 6, 7, 8, 9 JULY 11:00 - 18:00
IKA - akademie of fine arts 2 floor

 

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

 

Built with Processing

/**
* a_collition is a quick simulation of a mass of humans running arround, including colliton detection
* @author: Processing Wokrshop @ IKA 09 ika/akbild.ac.at
* @version: 1.0
*/


ArrayList ArrM;                           // define the variable ArrM as ArrayList();

void setup() {                            // setup define the basic layout of your sketch
  size(300,300);                          // set Size of the Window to 300/300
  frameRate(25);                          // frameRate try's to call draw function 25 times / second
  ArrM = new ArrayList();                 // initialize the variable ArrM as ArrayList();
  for (int i = 0; i < 100;i++) {          // loop a 100 times
    ArrM.add( new MENSCH() );             // and add 100 MENSCHen into the ArrM ArrayList();
  }
}

void draw() {                            // use this function to draw elements to the screen
  background(255);                       // set the background to white
  MENSCH M;                              // Define a variable M of the class MENSCH
  for (int i =0; i < ArrM.size();i++){   // cycle through all MENSCHen stored in the ArrayList arrM
    M = (MENSCH) ArrM.get(i);            // get the i'th MENSCH an cast it into the variable M
    M.Update(ArrM);                      // call the Update() Method of the Class MENSCH
    M.Render();                          // call the Render() Method of the Class MENSCH
  }
}

/**
* MENSCH is a class for www.processing.org
* MENSCH will simulate a human running around
* @author: Processing Workshop @ IKA 09 ika/akbild.ac.at
* @version: 1.1
*/

class MENSCH {

  PVector PVPos, PVDir;                                                    // PVPos = current Position, PVDir = current Motion
  float Size;                                                              // Size = personal Space of each Human
  final float MaxSpeed = 3.0;                                              // MaxSpeed = 3.0 m/s

  MENSCH() {
   this.Size = random(2,5);                                                // define a personal space between 2 and 5 Meters
   this.PVPos = new PVector( random(width),random(height) );               // position MENSCH somewhere into the Window
   this.PVDir = new PVector( random(-1,1),random(-1,1) );                  // create a random Motion
  }
  /** call to move the Person
  */

  void Update(ArrayList ArrTemp) {                                         // if the method void is called ask for an ArrayList containing all other humans
   float TempF;                                                            // define a float variable
   MENSCH TempM;                                                           // define a MENSCH variable
    PVector TempV;                                                         // define a PVector variable
    for (int i = 0; i < ArrTemp.size();i++){                               // cycle through all MENSCHen stored in ArrTemp
      TempM = (MENSCH) ArrTemp.get(i);                                     // get the i'th MENSCH to compare
      TempV = PVector.add(this.PVPos,this.PVDir);                          // get the future position of this MENSCH
      TempF = TempV.dist(TempM.PVPos);                                     // calculate the distance between the future position of this MENSCH and the other MENSCH
      if (TempF < this.Size + TempM.Size && ArrTemp.indexOf(this) != i) {  // if the distance between the future position if this MENSCH and
                                                                           // the other MENSCH is to small and it is not himself
        TempV = PVector.sub(TempV,TempM.PVPos);                            // get the Vector from the other MENSCH to the future position of this MENSCH
        TempV.limit((this.Size+TempM.Size)-TempF);                         // limits the length so that the deviation is a minimum
        this.PVDir.add(TempV);                                             // add deviation to the current direction so that the two MENSCHen do not collide
        pushStyle();                                                       // display collision detection to screen
        stroke(197,252,18);
        noFill();
        strokeWeight(2);
        ellipse(this.PVPos.x,this.PVPos.y,this.Size,this.Size);
        popStyle();
      }
    }
    this.PVDir.limit(MaxSpeed);                                            // limits the speed of the current Motion

    this.PVPos.add(this.PVDir);                                            // adds the current Motion to the Position
    this.PVDir.add( new PVector( random(-.1,.1),random(-.1,.1) ) );        // changes the current Motion a little bit
    this.PVDir.limit(MaxSpeed);                                            // limits the speed of the current Motion

    if (this.PVPos.x > width) {                                            // if a MENSCH walks out of the screen at the bottom
      this.PVPos.x -= width;                                               // it comes back at the top
    }
    else if (this.PVPos.x < 0) {                                           // top ==> bottom
      this.PVPos.x += width;
    }
    if (this.PVPos.y > height) {                                           // right ==> left
      this.PVPos.y -= height;
    }
    else if (this.PVPos.y < 0) {                                           // left ==> right
      this.PVPos.y += height;
    }
  }

  /** call to display the human on the screen
  */
  void Render() {
    pushStyle();
    strokeWeight(2);
    ellipse(this.PVPos.x,this.PVPos.y,this.Size,this.Size);
    strokeWeight(0);
    stroke(247,5,167);
    line(this.PVPos.x,this.PVPos.y,this.PVPos.x+this.PVDir.x*20,this.PVPos.y+this.PVDir.y*20);
    popStyle();
  }

}