我想寫一個簡單的物理模擬,其中半球和質量不同的球在完美彈性和無摩擦的環境中彈跳。我寫我自己的代碼下面這個資源:http://www.vobarian.com/collisions/2dcollisions2.pdf,我也測試了從這裏代碼:Ball to Ball Collision - Detection and Handling二維球碰撞問題:沒有能量守恆
問題EDITED
與Rick Goldstein和拉爾夫的幫助下,我已經得到了我的代碼工作(有一個錯字..)。非常感謝你的幫助。不過,我仍然困惑於爲什麼其他算法不適合我。球沿正確的方向反彈,但系統的總能量永遠不會被保存。速度變得越來越快,直到球剛開始在屏幕上的靜態位置閃爍。我實際上想在我的程序中使用這些代碼,因爲它比我寫的更簡潔。
這裏是我寫的功能算法(雖然我沒有從其他來源獲得第一位)。它在氣泡類別:
public void resolveCollision(Bubble b)
{
// get the minimum translation distance
Vector2 delta = (position.subtract(b.position));
float d = delta.getMagnitude();
// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta.multiply(((getRadius() + b.getRadius())-d)/d);
// resolve intersection --
// inverse mass quantities
float im1 = 1/getMass();
float im2 = 1/b.getMass();
// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1/(im1 + im2)));
b.position = b.position.subtract(mtd.multiply(im2/(im1 + im2)));
//get the unit normal and unit tanget vectors
Vector2 uN = b.position.subtract(this.position).normalize();
Vector2 uT = new Vector2(-uN.Y, uN.X);
//project ball 1 & 2 's velocities onto the collision axis
float v1n = uN.dot(this.velocity);
float v1t = uT.dot(this.velocity);
float v2n = uN.dot(b.velocity);
float v2t = uT.dot(b.velocity);
//calculate the post collision normal velocities (tangent velocities don't change)
float v1nPost = (v1n*(this.mass-b.mass) + 2*b.mass*v2n)/(this.mass+b.mass);
float v2nPost = (v2n*(b.mass-this.mass) + 2*this.mass*v1n)/(this.mass+b.mass);
//convert scalar velocities to vectors
Vector2 postV1N = uN.multiply(v1nPost);
Vector2 postV1T = uT.multiply(v1t);
Vector2 postV2N = uN.multiply(v2nPost);
Vector2 postV2T = uT.multiply(v2t);
//change the balls velocities
this.velocity = postV1N.add(postV1T);
b.velocity = postV2N.add(postV2T);
}
這裏是行不通
public void resolveCollision(Bubble b)
{
// get the minimum translation distance
Vector2 delta = (position.subtract(b.position));
float d = delta.getMagnitude();
// minimum translation distance to push balls apart after intersecting
Vector2 mtd = delta.multiply(((getRadius() + b.getRadius())-d)/d);
// resolve intersection --
// inverse mass quantities
float im1 = 1/getMass();
float im2 = 1/b.getMass();
// push-pull them apart based off their mass
position = position.add(mtd.multiply(im1/(im1 + im2)));
b.position = b.position.subtract(mtd.multiply(im2/(im1 + im2)));
// impact speed
Vector2 v = (this.velocity.subtract(b.velocity));
float vn = v.dot(mtd.normalize());
// sphere intersecting but moving away from each other already
if (vn > 0.0f) return;
// collision impulse (1f is the coefficient of restitution)
float i = (-(1.0f + 1f) * vn)/(im1 + im2);
Vector2 impulse = mtd.multiply(i);
// change in momentum
this.velocity = this.velocity.add(impulse.multiply(im1));
b.velocity = b.velocity.subtract(impulse.multiply(im2));
}
我們,如果你發現任何我知道的一個。謝謝
雅這是錯字...我一直在電腦前呆了太久,我多次查了一遍數學。謝謝。看看我的編輯雖然,我真的想讓其他算法的工作和它絕對不是同一個問題 – Cbas 2011-02-16 02:51:26
至於你的第二點,我不檢查,看看我是否已經通過氣泡解決了我的循環中的碰撞,但我不認爲這很重要,因爲我在這個resolveCollision方法中做的第一件事是將它們拉開。當循環在碰撞中找到另一個球時,它不會再發生碰撞並且不會再次調用這個方法 – Cbas 2011-02-16 02:56:36