【发布时间】:2013-12-21 18:26:03
【问题描述】:
在看到画布有多简单后,我最近开始使用它。我的第一个项目只是在它移动时保持一个圆圈的边界。我又做了一些涉及圆圈运动的东西,现在......
我目前正在研究当它们击中时将两个圆圈相互弹开。您可以在此处查看示例:http://jsfiddle.net/shawn31313/QQMgm/7/
但是,我想使用更多真实世界的物理学。此刻,当圆圈相互碰撞时,它们只是改变了路径。
如下图:
// Dont be confused, this is just the Distance Formula
// We compare the distance of the two circles centers to the sum of the radii of the two
// circles. This is because we want to check when they hit each other on the surface
// and not the center.
var distance = Math.sqrt(Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2));
var r1 = c1.rad;
var r2 = c2.rad;
if (distance < r1 + r2) {
// Change the slope of both circle
// I would like to figure out a more effecience way of bouncing the circles back
// However, I have no idea how to determine the angle the ball was struck,
// and with that information bounce it off at that angle
c1.xi = -c1.xi; // path is reversed
c1.yi = -c1.yi;
c2.xi = -c1.xi;
c2.yi = -c1.yi;
}
但是,我希望圆圈按照交点和交角确定的相反方向移动。
我才 9 年级,不知道这样的公式会是什么样子。但我知道这是可能的,因为这种物理存在于许多游戏中。一个例子是 8 球比赛。当球相互撞击时,它们会根据球的撞击方式在桌子上移动。
如果我应该等到我对物理和数学有更深入的了解,我将不胜感激。
【问题讨论】:
-
如果圆的质量和速度相同,那么速度就会交换。但如果速度不同,事情就会变得更加复杂。
-
@Teepeemm 感谢您的评论。但是相交的角度是否重要?
-
我不这么认为。跟随 Pixou,我们可以旋转所有东西(至少在我们的脑海中),使重心向右移动。由于质量和速度匹配,这意味着速度的垂直分量完全相反。然后当圆圈撞击时,就像它们从水平墙上反弹一样,因此水平速度不会改变,垂直速度会被抵消。但这正是来自另一个圆圈的速度。取消旋转所有内容,您就会回到开始的参考框架中。
标签: javascript math canvas physics