我有一个小圆圈,在一个更大的圆圈内。小圆圈在飞来飞去,如果小圆圈到达大圆圈的边界,它应该会发生碰撞。我几乎设法做到了,但它仍然不能完美地工作。有时圆在边界之前碰撞,有时在边界之后。这是我的代码:
if (!(Math.pow((xSmallCircle + radiusSmallCircle) - (xBigCircle), 2) + Math.pow((
ySmallCircle + radiusSmallCircle) - yBigCircle, 2) < Math.pow(radiusBigCircle + 10, 2))) {
xVelocity *= -1;
yVelocity *= -1;
} else if (!(Math.pow((xSmallCircle - radiusSmallCircle) - (xBigCircle), 2) + Math.pow((
ySmallCircle - radiusSmallCircle) - yBigCircle, 2) < Math.pow(radiusBigCircle + 10, 2))) {
xVelocity *= -1;
yVelocity *= -1;
} else if (!(Math.pow((xSmallCircle + radiusSmallCircle) - (xBigCircle), 2) + Math.pow((
ySmallCircle - radiusSmallCircle) - yBigCircle, 2) < Math.pow(radiusBigCircle + 10, 2))) {
xVelocity *= -1;
yVelocity *= -1;
} else if (!(Math.pow((xSmallCircle - radiusSmallCircle) - (xBigCircle), 2) + Math.pow((
ySmallCircle + radiusSmallCircle) - yBigCircle, 2) < Math.pow(radiusBigCircle + 10, 2))) {
xVelocity *= -1;
yVelocity *= -1;
}
任何想法为什么这不起作用?