programing studying notes no.1--用processing描述球体碰撞

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.




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值