經過一段時間的搜索,我修改了我的問題。向量公式實現
我發現了很多球對球碰撞的例子,但似乎工作的唯一例子使用Vector2d或Vector2D。
這是一個問題,因爲我只允許使用普通的java庫,所以我的主要問題是:如何轉換示例(我將在後面發佈)以使用我可以使用的東西?
我有幾個變量,兩個球具有相同的質量,速度被分解成不同的變量,x和y。我也可以訪問他們的x和y pos。
這是我的應用程序中剩下的唯一問題。
我完全喪失瞭如何轉換下面的例子。
// get the mtd
Vector2d delta = (position.subtract(ball.position));
float d = delta.getLength();
// minimum translation distance to push balls apart after intersecting
Vector2d mtd = delta.multiply(((getRadius() + ball.getRadius())-d)/d);
// resolve intersection --
// inverse mass quantities
float im1 = 1/getMass();
float im2 = 1/ball.getMass();
// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1/(im1 + im2)));
ball.position = ball.position.subtract(mtd.multiply(im2/(im1 + im2)));
// impact speed
Vector2d v = (this.velocity.subtract(ball.velocity));
float vn = v.dot(mtd.normalize());
// sphere intersecting but moving away from each other already
if (vn > 0.0f) return;
// collision impulse
float i = (-(1.0f + Constants.restitution) * vn)/(im1 + im2);
Vector2d impulse = mtd.multiply(i);
// change in momentum
this.velocity = this.velocity.add(impulse.multiply(im1));
ball.velocity = ball.velocity.subtract(impulse.multiply(im2));
下面是問題的網址:
http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling
而且我已經採取了看他的源代碼。
感謝您花時間閱讀此問題。
SUCCESS!
我發現瞭如何使用Vector2d,它的工作原理非常完美! 稍後再回答編輯!
什麼是矢量? – Blender
這是一個點光線,起源於0,0到2D圖上的指定點。順便說一句,我也使用攪拌機! – DaCoder
[球對球碰撞 - 檢測和處理]的可能重複(http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling) –