class Ball{
static final float R=20; // radius for all balls
PVector pos; // position
PVector speed; // speed
Ball(PVector pos,PVector speed){
this.pos=pos;
this.speed=speed;
}
void move(){
pos.add(speed);
// take out balls outside big circle
// should not happen
if(dist(pos.x,pos.y,width/2,height/2) > bigR+R){
speed=pos.get();
return;
}
// getting close to big circle
if(dist(pos.x,pos.y,width/2,height/2) > bigR-R-5){
// make new speed direction
// vector from centre to hit point
PVector P=new PVector(pos.x-width/2,pos.y-height/2);
// angle between speed av P
PVector tmp=speed.get();
tmp.mult(-1);
float V= PVector.angleBetween(P,tmp);
//tmp.rotate(-2*V);
tmp=rotateVector(tmp,-2*V);
speed=tmp.get();
PVector nextP=new PVector(pos.x+speed.x,pos.y+speed.y);
// going wron way ?
if(dist(nextP.x,nextP.y,width/2,height/2) > bigR-R-5){
speed=rotateVector(speed,4*V+PI/100.0);
}
else
{
speed=rotateVector(speed,-PI/100.0);
}
//makeSound();
}
}
void invertSpeed(){
speed.mult(-1);
}
void replace(){
pos.mult(0.8);
}
void swapSpeed(Ball otherBall){
PVector tmp= speed.get();
speed=otherBall.speed.get();
otherBall.speed.set(tmp);
makeSound("hit");
}
boolean touch(Ball otherBall){
return (dist(pos.x,pos.y,otherBall.pos.x,otherBall.pos.y) < 2*R-maxSpeed-1);
}
boolean hangOn(Ball otherBall){
return dist(pos.x,pos.y,otherBall.pos.x,otherBall.pos.y) < 2*R-10;
}
void draw(){
float c=map(speed.mag(),0,6,100,255);
fill(int(c),0,0);
ellipse(pos.x,pos.y,R,R);
//draw speed: line(pos.x,pos.y,pos.x+20*speed.x,pos.y+20*speed.y);
}
}