我正在编写一个简单的程序来检测碰撞并给出适当的合成速度。每当我添加实际设置结果速度的代码时,没有对象渲染,只是在添加时短暂闪烁。我不认为我使用Processing有多重要,语法非常标准。
我使用的公式
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();
}
处理板上的人帮了我一把。当i==j
时,它会对自己检查同一个球,并获得一些荒谬的信息。一个简单的if(i!=j) {
包装实现了这一点。