// example demonstrating a simple vector class // and primitive proximity detection. import processing.opengl.*; Particle part[]; int num; void setup() { size(400,400, OPENGL); num=10; part=new Particle[num]; for(int i=0; iwidth) x=0; if(y<0) y=height; else if(y>height) y=0; if(state==ROTATING) { stateCnt--; if(stateCnt==0) initState(STRAIGHT); } } public void checkCollision(Particle _part) { Vec2D diff; diff=new Vec2D(x,y); diff.sub(_part.x,_part.y); // check for collision if(diff.length()<30) { initState(ROTATING); } } public void initState(int _newState) { if(_newState==ROTATING) { state=ROTATING; dirD=random(0.25,1.5); if(random(100)>50) dirD=-dirD; stateCnt=(int)random(60,120); } else if(_newState==STRAIGHT) { state=STRAIGHT; dirD=0; stateCnt=(int)random(60,120); } } public void draw() { ellipse(x,y, 10,10); for(int i=0; i<3; i++) ellipse(x+dirNormal.x*(i*5+15), y+dirNormal.y*(i*5+15), 5,5); } } class Vec2D { float x,y; public Vec2D(float _x,float _y) { x=_x; y=_y; } public void add(float _x,float _y) { x=x+_x; y=y+_y; } public void sub(float _x,float _y) { x=x-_x; y=y-_y; } public float length() { return sqrt(x*x+y*y); } public void norm() { float l=length(); x/=l; y/=l; } public Vec2D getNormal() { Vec2D n=new Vec2D(-y,x); n.norm(); return n; } public void rotate(float _deg) { // Floats are not precise enough, so doubles // are used for the calculations double cosval=cos(_deg); double sinval=sin(_deg); double tmpx=x*cosval - y*sinval; double tmpy=x*sinval + y*cosval; x=(float)tmpx; y=(float)tmpy; } }