2D碰撞和处理中的结果速度

时间:2013-12-03 17:39:31

标签: processing physics

我正在编写一个简单的程序来检测碰撞并给出合适的合成速度。每当我添加实际设置结果速度的代码时,没有渲染任何对象,只需在添加时短暂闪烁。我不认为我使用Processing很重要,语法很标准。

The formulas I'm using

for (int i =0; i<balls.length; i+=1) {
for (int j =0; j<balls.length; j+=1) {
  float x1 = balls[i].x;
  float y1 = balls[i].y;
  float vx1 = balls[i].vx;
  float vy1 = balls[i].vy;
  float m1 = balls[i].m;
  float size1 = balls[i].size;
  float x2 = balls[j].x;
  float y2 = balls[j].y;
  float vx2 = balls[j].vx;
  float vy2 = balls[j].vy;
  float m2 = balls[j].m;
  float size2 = balls[j].size;
  float v1=sqrt(pow(vx1, 2)+pow(vy1, 2));
  float v2=sqrt(pow(vx2, 2)+pow(vy2, 2));
  float t1=atan(x1/y1);
  float t2=atan(x2/y2);
  float c=atan((x2-x1)/(y2-y1));
  if (sqrt(sq(x2-x1)+sq(y2-y1)) < size1/2+size2/2) {
    balls[i].vx=cos(c)*((v1*cos(t1-c)*(m1-m2)+2*m2*v2*cos(t2-c))/(m1+m2))+v1*sin(t1-c)*cos(c+(PI/2));
    balls[i].vy=sin(c)*((v1*cos(t1-c)*(m1-m2)+2*m2*v2*cos(t2-c))/(m1+m2))+v1*sin(t1-c)*cos(c+(PI/2));
    balls[j].vx=cos(c)*((v2*cos(t2-c)*(m2-m1)+2*m1*v1*cos(t1-c))/(m1+m2))+v1*sin(t2-c)*cos(c+(PI/2));
    balls[j].vy=sin(c)*((v2*cos(t2-c)*(m2-m1)+2*m1*v1*cos(t1-c))/(m1+m2))+v1*sin(t2-c)*cos(c+(PI/2));
  }
  balls[i].draw();
  balls[j].draw();
}

1 个答案:

答案 0 :(得分:0)

处理委员会的人帮助了我。当i==j时,它正在检查相同的球并获得一些荒谬的信息。一个简单的if(i!=j) {包装器就可以了。