我试图让两个球碰撞,我已经阅读了很多关于这个主题的物理内容和讨论。我已经根据我阅读的内容编写了一些代码但是当两个球碰撞时它们的x坐标不同但在x方向上没有移动(并且y轴相同)时似乎存在问题。
举个例子,如果我让一个球从地面上的球上掉落(在x方向上有一点偏移),落球就会直接跳回空中而不是加速x方向
我也有一些代码,没有这个问题,但假设球具有相同的质量,在我的情况下并不总是如此。
我不知道我的代码中缺少什么,或者我如何转换其他代码以关注群众。
以下是更好理解的图片:
ball collision http://img197.imageshack.us/img197/1227/ballt.jpg
我的代码:
double collisionAngle = Math.atan2(element1.getPosY() - element2.getPosY(), element1.getPosX() - element1.getPosX());
double direction1 = Math.atan2(element1.getVectorY(), element1.getVectorX());
double direction2 = Math.atan2(element2.getVectorY(), element2.getVectorX());
double magnitude1 =
Math.sqrt(element1.getVectorX() * element1.getVectorX() + element1.getVectorY() * element1.getVectorY());
double magnitude2 =
Math.sqrt(element2.getVectorX() * element2.getVectorX() + element2.getVectorY() * element2.getVectorY());
double v1x = magnitude1 * Math.cos(direction1 - collisionAngle);
double v1y = magnitude1 * Math.sin(direction1 - collisionAngle);
double v2x = magnitude2 * Math.cos(direction2 - collisionAngle);
double v2y = magnitude2 * Math.sin(direction2 - collisionAngle);
double f1x =
(v1x * (element1.getMass() - element2.getMass()) + 2 * element2.getMass() * v2x)
/ (element1.getMass() + element2.getMass());
double f2x =
(v2x * (element2.getMass() - element1.getMass()) + 2 * element1.getMass() * v1x)
/ (element1.getMass() + element2.getMass());
double v1 = Math.sqrt(f1x * f1x + v1y * v1y);
double v2 = Math.sqrt(f2x * f2x + v2y * v2y);
double D1 = Math.atan2(v1y, f1x) + collisionAngle;
double D2 = Math.atan2(v2y, f2x) + collisionAngle;
element1.setVectorX(Math.cos(D1) * v1);
element1.setVectorY(Math.sin(D1) * v1);
element2.setVectorX(Math.cos(D2) * v2);
element2.setVectorY(Math.sin(D2) * v2);
代码不关心质量但适用于这种情况
double dx = element1.getPosX() - element2.getPosX();
double dy = element1.getPosY() - element2.getPosY();
double phi = Math.atan2(dy, dx);
//Transformations
double ux = (element1.getVectorX() + element2.getVectorX()) / 2;
double uy = (element1.getVectorY() + element2.getVectorY()) / 2;
//Calculate magnitude of each vector for mc1 and mc2
double mc1vx = element1.getVectorX() - ux;
double mc1vy = element1.getVectorY() - uy;
double mc2vx = element2.getVectorX() - ux;
double mc2vy = element2.getVectorY() - uy;
double mc1_vmagnitude = Math.sqrt(mc1vx * mc1vx + mc1vy * mc1vy);
double mc2_vmagnitude = Math.sqrt(mc2vx * mc2vx + mc2vy * mc2vy);
//calculate direction of each vector
double alfa = Math.atan2(mc1vy, mc1vx);
double beta = Math.atan2(mc2vy, mc2vx);
//Get new vectors
double v1x = mc1_vmagnitude * (Math.cos(2 * phi - alfa) * -1) + ux;
double v1y = mc1_vmagnitude * (Math.sin(2 * phi - alfa) * -1) + uy;
double v2x = mc2_vmagnitude * (Math.cos(2 * phi - beta) * -1) + ux;
double v2y = mc2_vmagnitude * (Math.sin(2 * phi - beta) * -1) + uy;
//apply new vectors
element1.setVectorX(v1x);
element1.setVectorY(v1y);
element2.setVectorX(v2x);
element2.setVectorY(v2y);
//to somewhat mediate the stick problem such them apart further…
element1.setPosX(element1.getPosX() + v1x, false);
element1.setPosY(element1.getPosY() + v1y, false);
element2.setPosX(element2.getPosX() + v2x, false);
element2.setPosY(element2.getPosY() + v2y, false
答案 0 :(得分:0)
我正在使用此链接http://www.plasmaphysics.org.uk/programs/coll2d_cpp.htm
回答我自己的问题我不是百分百确定这是如何在物理上起作用的,但它有效...