2011年9月5日 星期一

smartGeometry_V0.1( processing _ 2D )


Video Inside



使用Processing建立一個Particle System.
開始使用2D的Particle.
目的為可以透過拖拉功能
改變2D 元件之間的關係 
Using Processing to biuld a Particle System. 
The first aim is to drag and drop the elements inside the system 
to let the particles influence each others.



在這樣的狀況之下
我們可以一方面使其具有自然的力量
一方面也可對其進行控制
In this condition,
we could let the system has its own natural force. 
but we can also change the location to break the relation
in other way.




想像有一張平面
是透過這些節點所控制的
因此我們可以輕易地移動端點來改變其變化
Imagine there is a surface composed by these nods.
We can easy changing the nods to make different surfaces.


當你按下 "r" 鍵的時候
他會產生一個PDF file
When you press "r" 
it will automatically produce a 
PDF.file

the Code is Below:
import toxi.geom.*;
import processing.pdf.*;

boolean record = false;

ArrayList ballCollection = new ArrayList();

float r = 20;//radius of the circle
float con = 2; //r*con = the range that once mouse cover the item
float newx, newy;
boolean start;
float fillColor=random(255);

Ball BallItem;

void setup(){
 size(800,800);
 for(int i =0; i<20; i++){
 Vec3D origin = new Vec3D (random(0,width),random(0,height),0);
 Ball myBall = new Ball(origin,random(0,255));
 ballCollection.add(myBall); 
 }// producing the balls
}

void draw(){
 background(0);
 if (record == true) {
//beginRaw(DXF, "output.dxf"); // Start recording to the file
beginRecord(PDF, "output.pdf");//record PDF file
}
 for(int i=0; i
 Ball newBall = (Ball) ballCollection.get(i);
 drawBall(newBall);
 }// create circle
  
  if (record) {
    endRecord();
record = false;
  }

}

 void drawBall(Ball item){
  item.run();
  mouseDragged(item); 
  if(start==false && mousePressed==false){
  start =true;
 }
}
void mousePressed(){
 for(int i=0; i
 Ball newBall = (Ball) ballCollection.get(i);  
 checkBounds(newBall);
 }
}

boolean checkBounds(Ball item){
   if(mouseX > item.loc.x()-r*con && mouseX < item.loc.x()+r*con 
   && mouseY > item.loc.y()-r*con && mouseY < item.loc.y()+r*con)
   {
      BallItem = item;
      return true;
    }
    else {
     return false; 
    }
}
 void mouseDragged(Ball item){
   if(mouseX > item.loc.x()-r*con && mouseX < item.loc.x()+r*con 
   && mouseY > item.loc.y()-r*con && mouseY < item.loc.y()+r*con
   && BallItem == item){
      if(start==true){
      newx = mouseX - item.loc.x();
      newy = mouseY - item.loc.y();
      start=false;
    }
    item.setx(mouseX-newx);
    item.sety(mouseY-newy);
   }
   item.display();
}

void keyPressed() {
if (key == 'R' || key == 'r') { // Press R to save the file
record = true;
}
}


class Ball{

Vec3D loc = new Vec3D(0,0,0);  
Vec3D vec = new Vec3D(random(-2,2),random(-2,2),0); 
Vec3D gravity = new Vec3D(0,0.2,0);
Vec3D acc = new Vec3D(0,0,0);
float fillColor;
float magn =20;// the foece against each others

Ball myBall;

Ball(Vec3D _loc, float _fillColor){
  loc = _loc;
  fillColor = _fillColor;
}

void run(){
 display();
 drawLines();
 bounce();
 move();
 //gravity();
 seperate(magn);
}

void display(){
  fill(fillColor);
  for(float i=r;i<50;i+=10){
    noFill();
  ellipse(loc.x,loc.y,r+i,r+i);
  }
}

void move(){
  vec.addSelf(acc);
  vec.limit(2);
  loc.addSelf(vec);
  acc.clear();
}

void bounce(){
  if(loc.x>width||loc.x<0){
   vec.x=(vec.x)*-1;
  }
  if (loc.y>height || loc.y<0){
   vec.y=(vec.y)*-1;
  }
}

void gravity(){
  //speedX= speedX+1;
  vec.addSelf(gravity);  
 }

 void setx(float givenX){
  loc.x = givenX; 
 }
 void sety(float givenY){
  loc.y = givenY;
 }
  
  void drawLines(){
    for(int i=0; i
     Ball other = (Ball) ballCollection.get(i);
     float distance = loc.distanceTo(other.loc);
     if(distance> 0 && distance<200){
      stroke(random(255),random(255),random(255));
      line(loc.x, loc.y, other.loc.x, other.loc.y);
     }
    }
  }
  
void seperate(float magnitude){
  Vec3D steer = new Vec3D();
  int count =0;

  for(int i=0; i
     Ball other = (Ball) ballCollection.get(i);
     float distance = loc.distanceTo(other.loc);
     if(distance>0 && distance<100  ){
       Vec3D diff = loc.sub(other.loc);
       diff.normalizeTo(1/distance);
       steer.addSelf(diff);
       //count++;
     }
    }
    ///if (count >0){
   //  steer.scaleSelf(1/count);
   //}
   steer.scaleSelf(magnitude);
   acc.addSelf(steer);
}

}

沒有留言:

張貼留言