我最近開始玩帆布後,看到它是多麼容易。我的第一個項目就是在它移動時保持一個圓圈。我做了幾件涉及圈子運動的事情,現在...確定兩個圓相交的點和角度。
我目前正在努力,當他們擊中時,彼此彈跳兩圈。你可以在這裏看到這個例子: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
@Teepeemm感謝您的評論。但是,如果交叉的角度是重要的? – Shawn31313
我不這麼認爲。在Pixou之後,我們可以旋轉一切(至少在我們的腦海中),使得質心向右移動。由於質量和速度匹配,這意味着速度的垂直分量完全相反。然後當圓圈碰撞時,就好像它們從水平牆壁反彈出來一樣,所以水平速度不會改變,垂直速度也不會改變。但這正是來自另一個圈子的速度。不加旋轉的一切,你回到你開始的參考框架。 – Teepeemm