圆形碰撞虫,圆形轨道不可移动的圆圈

时间:2011-04-05 00:08:55

标签: java geometry velocity

我在下面有两个圆圈碰撞的工作代码,但现在我有特殊的圆圈,不应该移动。当试图将它们与现有代码一起应用时,移动的圆圈开始围绕静态,“螺栓”,不动的圆圈运行(当我说轨道时,我的意思是圆圈围绕圆圈旋转,同时触摸它一段时间直到它到达另一边,然后继续它的原始方向)。我试图让代码的一部分用于静态圆圈(参见巨大的注释部分),但它只有一半工作。我的正常大小的圆圈在大多数情况下工作正常,但有时它仍然在轨道上运行。此外,我有另一个特殊的圆圈,不受摩擦影响,并且更小,并且几乎总是绕轨道运行。有谁知道我的问题是什么?我一半了解这里的数学,我从一些gamasutra文章中得到了它。

//move them apart so they don't intersect anymore
float distance = (float) Math.sqrt(((circleA.getCenterX() - circleB.getCenterX()) * (circleA.getCenterX() - circleB.getCenterX())) + ((circleA.getCenterY() - circleB.getCenterY()) * (circleA.getCenterY() - circleB.getCenterY())));
float separation = (circleA.getRadius() + circleB.getRadius()) - distance;

float xSepA = separation * (circleA.getCenterX() - circleB.getCenterX()) / distance / 2; //distance to move circleA in x dir
float ySepA = separation * (circleA.getCenterY() - circleB.getCenterY()) / distance / 2; //distance to move A in y dir
float xSepB = separation * (circleB.getCenterX() - circleA.getCenterX()) / distance / 2; //same for B
float ySepB = separation * (circleB.getCenterY() - circleA.getCenterY()) / distance / 2;

if (circleA.isStatic()) {
    xSepA = 0;
    ySepA = 0;
    xSepB = separation * (circleB.getCenterX() - circleA.getCenterX()) / distance; //same for B
    ySepB = separation * (circleB.getCenterY() - circleA.getCenterY()) / distance;
}
if (circleB.isStatic()) {
    xSepA = separation * (circleA.getCenterX() - circleB.getCenterX()) / distance;
    ySepA = separation * (circleA.getCenterY() - circleB.getCenterY()) / distance;
    xSepB = 0;
    ySepB = 0;
}

//moving them

circleA.setX(circleA.getX() + xSepA);
circleA.setY(circleA.getY() + ySepA);
circleB.setX(circleB.getX() + xSepB);
circleB.setY(circleB.getY() + ySepB);
//change velocity to bounce
Vector2f va = circleA.getVelocityVector();
Vector2f vb = circleB.getVelocityVector();
Vector2f vn = MathUtil.sub(circleA.getPositionVector(), circleB.getPositionVector());
vn.normalise();



float aa = va.dot(vn);
float ab = vb.dot(vn);

float optimizedPA = (2f * (aa - ab)) / (circleA.getMass() + circleB.getMass());
float optimizedPB = (2f * (ab - aa)) / (circleA.getMass() + circleB.getMass());

Vector2f newVA = MathUtil.sub(va, MathUtil.scale(vn, optimizedPA * circleB.getMass()));
Vector2f newVB = MathUtil.sub(vb, MathUtil.scale(vn, optimizedPB * circleA.getMass()));

//    if (circleA.isStatic()) {
//        optimizedPB = (2f * (ab - aa)) / (circleB.getMass());
//        newVA = va;
//        newVB = MathUtil.sub(vb, MathUtil.scale(vn, optimizedPB));
//        System.out.println(vb + " " + newVB);
//    } else if (circleB.isStatic()) {
//        optimizedPA = (2f * (aa - ab)) / (circleA.getMass() + circleB.getMass());
//        newVA = MathUtil.sub(va, MathUtil.scale(vn, optimizedPA));
//        newVB = vb;
//        System.out.println(va + " " + newVA);
//    }

circleA.setVX(newVA.getX());
circleA.setVY(newVA.getY());
circleB.setVX(newVB.getX());
circleB.setVY(newVB.getY());

1 个答案:

答案 0 :(得分:1)

要了解向量方法的工作原理,您可以从articleexample获得一些见解。这种“轨道”异常的一个来源是可变载体的不正确实现;彻底的单元测试至关重要。没有简单的方法来调试像这样的代码片段;你可能需要准备一个sscce

相关问题