i will use this blog to track down my programing life.
these days,i am focousing on processing.how to imitate balls colliding,this is what i am thinking.
and this below is what i get gradually:
--------------------------------------this is the first one i make ,only one ball-----------------------------------------
Circle mycircle;
void setup()
{
size(400,400);
smooth();
mycircle=new Circle();
}
void draw()
{
background(255);
mycircle.move();
mycircle.judge();
mycircle.display();
}
class Circle
{
float x;
float y;
int r;
float angle;
Circle()
{
x=100;
y=100;
r=20;
angle=HALF_PI-20;
}
void move()
{
x+=5*cos(angle);
y+=5*sin(angle);
}
void judge()
{
if(y+r>=400||y-r<=0)
angle=2*PI-angle;
if(x+r>=400||x-r<=0)
angle=3*PI-angle;
}
void display()
{
ellipse(x,y,2*r,2*r);
}
}
------------------------------------the second one---------------------------------
Circle mycircle0;
Circle mycircle1;
Circle mycircle2;
void setup()
{
size(400,400);
smooth();
mycircle0=new Circle(200,200,60);
mycircle1=new Circle(250,320,160);
mycircle2=new Circle(100,150,250);
}
void draw()
{
background(0);
mycircle0.move();
mycircle0.judge();
mycircle0.display();
mycircle1.move();
mycircle1.judge();
mycircle1.display();
mycircle2.move();
mycircle2.judge();
mycircle2.display();
}
class Circle
{
float x;
float y;
int r;
float angle;
Circle(int xx,int yy,int zz)
{
x=xx;
y=yy;
r=20;
angle=HALF_PI-zz;
}
void move()
{
x+=5*cos(angle);
y+=5*sin(angle);
}
void judge()
{
if(y+r>=400||y-r<=0)
angle=2*PI-angle;
if(x+r>=400||x-r<=0)
angle=3*PI-angle;
}
void display()
{
fill(255,0,0);
ellipse(x,y,2*r,2*r);
}
}
------------------------------the third one--------------------------------
int M=5;
int speed=5;
Circle [] mycircle=new Circle[M];
int k=0;
int c=255;
void setup()
{
size(400,400);
smooth();
for(int i = 0; i < M; i ++)
{
mycircle[i] =new Circle(random(50,300),random(50,300),random(50,300));
}
}
void draw()
{
background(0);
for(int i = 0; i < M; i ++)
{
mycircle[i].move();
mycircle[i].judge();
mycircle[i].display();
for(int j = 0; j < M; j ++)
{
if(i!=j)
{
if(dist(mycircle[i].x,mycircle[i].y,mycircle[j].x,mycircle[j].y)<=mycircle[i].r+mycircle[j].r)
{
c=(int)random(20,240);
k++;
float m;
m=atan((mycircle[i].y-mycircle[j].y)/(mycircle[i].x-mycircle[j].x));
mycircle[i].angle=PI-mycircle[i].angle-2*m;
mycircle[j].angle=PI-mycircle[j].angle-2*m;
println("cash in NO "+k);
}
}
}
}
}
class Circle
{
float x;
float y;
int r;
float angle;
Circle(float xx,float yy,float zz)
{
x=xx;
y=yy;
r=(int)random(12,20);
angle=HALF_PI-zz;
}
void move()
{
x+=speed*cos(angle);
y+=speed*sin(angle);
}
void judge()
{
if(y+r>=400||y-r<=0)
angle=2*PI-angle;
if(x+r>=400||x-r<=0)
angle=3*PI-angle;
}
void display()
{
fill(c,255,0);
ellipse(x,y,2*r,2*r);
}
}
from this i learn:math is of great importance; learning can be intersting.