这是在JavaFX中开发的。静态与动态圆圈之间的碰撞响应
有2个球 - 一个动态球和一个固定球。第一个球(动态)从墙壁和任何其他方面反弹。
第二球的目的是成为第一球的障碍。所以,每当第一个球接触到第二个球时,第一个球应该立即弹开。目前,弹跳的准确性非常差,我不知道如何修复它。有时候第一个球会弹跳得很好,但通常它会在第二个球后面卡住,然后弹开。下面的图片将详细讨论这个问题。
这里是我的用于检测碰撞和应对它的代码:
//Pythagorean Theorem, to detect collision, by estimating the distance between the two circles
double dx = circle.getLayoutX() - circle2.getLayoutX();
double dy = circle.getLayoutY() - circle2.getLayoutY();
double radii = circle.getRadius() + circle2.getRadius();
double distance = (dx * dx) + (dy * dy);
double minDistance = radii * radii;
// I believe something is missing in the lines below, which is causing the problem.
if (distance < minDistance) { //If circle1(dynamic) collides with circle2(stationary)
c1SpeedX = c1SpeedX * -1; //Inverts direction.
}
我花了几个小时的谷歌,但我没能找到答案。我希望有人能提供解决方案并解释这个问题。提前感谢大家!
逻辑上就不是你想要的'遥远<= minDistance'?我还没有看过所有的细节,但这让我觉得很奇怪。具有讽刺意味的是一个类似的问题已经被问到:http://stackoverflow.com/questions/4613345/python-pygame-ball-collision-with-interior-of-circle?rq=1 – xQbert
我试过<和<=和它根本没有区别。 – Gregg1989