我正在尝试找出 JavaScript 数学来将两个碰撞的圆移开。
该图像的左侧是我已经拥有的内容的视觉表示:
x1, y1, x2 and y2 are the positions of the circles, r1 and r2 are the radii of the circles, theta is the angle between the circles in relation to the x-axis of the canvas.
如何计算两个圆的新 [x,y] 位置,以便它们相互“推”开,如图像右侧所示?
我还计划让小圆圈比大圆圈被推得更多。通过使用它们的标准化半径作为乘数,这应该很容易。
// Just take the vector difference between the centers
var dx = x2 - x1;
var dy = y2 - y1;
// compute the length of this vector
var L = Math.sqrt(dx*dx + dy*dy);
// compute the amount you need to move
var step = r1 + r2 - L;
// if there is a collision you will have step > 0
if (step > 0) {
// In this case normalize the vector
dx /= L; dy /= L;
// and then move the two centers apart
x1 -= dx*step/2; y1 -= dy*step/2;
x2 += dx*step/2; y2 += dy*step/2;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)