2013-05-15 52 views
0

經過一段時間的搜索,我修改了我的問題。向量公式實現

我發現了很多球對球碰撞的例子,但似乎工作的唯一例子使用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,它的工作原理非常完美! 稍後再回答編輯!

+0

什麼是矢量? – Blender

+0

這是一個點光線,起源於0,0到2D圖上的指定點。順便說一句,我也使用攪拌機! – DaCoder

+0

[球對球碰撞 - 檢測和處理]的可能重複(http://stackoverflow.com/questions/345838/ball-to-ball-collision-detection-and-handling) –

回答

0

我正在c#中實現我自己的3d引擎,基於JavaScript中一個非常基本的3d開源引擎,名爲a3。我不知道如果我有100%瞭解你,但聽起來你只能找到Vector2d的例子,但是你不能使用這個類?

我就是這樣,你可以想象的是,javascript沒有原生的Vector2d類型,所以有人不得不實施。不要害怕試一試,只是一些高中數學功能,你應該能夠在幾分鐘內實現你自己的Vector2d類

以下鏈接包含實現,如果vector2d,vector3d,vector4d ,matrix3和matrix4在javascript中:https://github.com/paullewis/a3/tree/master/src/js/core/math希望它有幫助:)

+0

我可以發佈我用jar文件創建的.class,但是我找不到如何導入它! – DaCoder

+0

你的意思是在這裏分享你的代碼嗎?您可以嘗試使用github https://gist.github.com/上的gist來分享它,然後在此處分享鏈接,或編輯上面的問題並將代碼添加到它。 –