运用 HTML5 Canvas 在两个或多个对象之间施加重力
在 Canvas 中应用两个或更多对象之间的重力
var distX = ob1.x - ob2.x, distY = ob1.y - ob2.y; var val = distX *distX + distY * distY; var r = Math.sqrt(val); var F = 50 / val; var rhat_x = distX / r; var rhat_y = distY / r; var Fx = F * rhat_x; var Fy = F * rhat_y; ob1.vx += -Fx; ob1.vy += -Fy; ob2.vx += Fx; ob2.vy += Fy;
广告