java - 圆碰撞错误,圆绕不动的圆运行

标签 java geometry velocity

我有下面的工作代码用于两个圆圈的碰撞,但现在我有特殊的圆圈,不应该永远移动。当尝试将它们与现有代码一起应用时,移动的圆圈开始围绕静态的、“ bolt 固定的”、不动的圆圈运行(当我说轨道时,我的意思是圆圈围绕圆圈旋转,同时接触它一段时间,直到它到达另一侧,然后继续沿原来的方向运行)。我尝试使部分代码适用于静态圆圈(请参阅巨大的注释掉部分),但只成功了一半。大多数时候,我的正常大小的圆圈工作得很好,但有时它仍然在轨道上运行。另外,我还有另一个特殊的圆,它不受摩擦力的影响,而且更小,而且几乎总是绕轨道运行。有谁知道我的问题是什么?我对这里发生的数学有一半了解,我从一些《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());

最佳答案

要了解 vector 方法的工作原理,您可能会从此 article 获得一些见解。和example 。这种“轨道”异常的根源之一是可变 vector 的错误实现。彻底的单元测试是必不可少的。没有简单的方法来调试这样的代码片段;您可能需要准备 sscce .

关于java - 圆碰撞错误,圆绕不动的圆运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5545749/

相关文章:

java - 使用 mysql 连接器 8 针对服务器 5.6.22 保存表情符号

java.lang.ArrayIndexOutOfBoundsException : length=10; index=10 异常

algorithm - 如果旅行推销员乘飞机旅行怎么办?

java - 递归地创建 Apollo 垫圈[有解决方案]

java - 当 URLResourceLoader 与 Velocity 一起使用时无法加载模板

java - 等到tomcat完成启动

java - 使用2个管道在线程之间传输

java - 初学者 java,显示与整数相关的特殊字符

css - Struts 菜单选项卡失去对 IE 11 的关注

java - 为什么 Apache Velocity 无法处理多个点键