2011年9月5日 星期一

smartGeometry_V0.1( processing _ 3D )

video inside

延伸之前的2D Particle System
去創作3D的效果
使其生成3D的Mesh似的量體
Try to make more than the 2D particle system.
Using the same code but extend to 3D 
to make this mesh-like particle system.

改變其節點數量與自然力量
即可改變其造型
Changing the number of the nods and the magnitude of the force
will change the whole BODY form.

下一步希望能在3D空間中拉開節點
如同在2D中控制造型 
The next step will hope to drag and drop the nods in 3D
as we did in 2D.
Try to figure it out next.





Code is :
import toxi.geom.*;
import processing.pdf.*;
import peasy.*;

PeasyCam gcam;

import processing.dxf.*;
boolean record = false;

ArrayList ballCollection = new ArrayList();

float r = 80;
float con = 2;
float newx, newy;
boolean start;
float fillColor=random(255);

Ball BallItem;

void setup(){
 size(1200,900,P3D);
   gcam = new PeasyCam(this, 2000);
 for(int i =0; i<40; i++){
 Vec3D origin = new Vec3D (random(0,width),random(0,height),random(0,height));
 Ball myBall = new Ball(origin,random(0,255));
 ballCollection.add(myBall); 
 }
}

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

 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),random(-2,2)); 
Vec3D gravity = new Vec3D(0,0.2,0.3);
Vec3D acc = new Vec3D(0,0,0);
float fillColor;
float magn  = 50;

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=0;i<100;i+=10){
    noFill();
    stroke(50);
    //strokeWeight(10);
  strokeWeight(10);
    stroke(0,255,0);
  point(loc.x,loc.y,loc.z); 
  strokeWeight(1);
  pushMatrix();
  translate(loc.x,loc.y,loc.z);

  //ellipse(0,0,r,r);
  //rotateY(PI/2);
  //ellipse(0,0,r,r);
  //rotateX(PI/2);
  //ellipse(0,0,r,r);  
  fill(255);
  //box(20); 
  popMatrix();
 }
}

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;
  }
  
   if (loc.z>height || loc.z<0){
   vec.z=(vec.z)*-1;
  }
}

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

 void setx(float givenX){
  loc.x = givenX; 
 }
 void sety(float givenY){
  loc.y = givenY;
 }
  void setz(float givenZ){
  loc.z = givenZ;
 }
  
  void drawLines(){

    for(int i=0; i
     Ball other = (Ball) ballCollection.get(i);
     float distance = loc.distanceTo(other.loc);
     if(distance> 0 && distance<500){
       stroke(0);
      //stroke(random(255),random(255),random(255));
      line(loc.x, loc.y, loc.z, other.loc.x, other.loc.y,other.loc.z);
     }
    }

  }
  
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);
}

}



1 則留言:

  1. all the problem with the code is about the for loop
    just add
    i<ballCollection.size(); i++){
    after all the error parts, then it works fine

    回覆刪除