我在球之間的碰撞分辨率方面存在問題。 其實碰撞反應非常現實,但動力不守恆,爲什麼?在球之間的碰撞分辨率之後,動量不守恆
我使用基於此文件的算法:http://www.vobarian.com/collisions/2dcollisions2.pdf。
在Java代碼中我的算法是:
/**
* Resolve the collision creating new velocities according to physical laws.
*/
public void resolveCollisionWith(Ball ball) {
//First resolve intersection for calculate correct new velocities.
resolveIntersectionWith(ball);
//Unit normal vector uN is the unit-vector that links the two centers.
Vector uN = mPosition.subtraction(ball.getPosition()).normalize();
//Unit tangent vector uT is the unit-vector normal to uN. It's tangent to both the two balls.
Vector uT = new Vector(-uN.getY(), uN.getX());
//Project the two balls velocities onto the collision axis(uT and uN vectors).
double v1n = uN.dot(mVelocity), v1t = uT.dot(mVelocity);
double v2n = uN.dot(ball.getVelocity()), v2t = uT.dot(ball.getVelocity());
//Calculate the post collision normal velocities (tangent velocities don't change).
double v1nPost = (v1n*(mMass-ball.getMass()) + 2*ball.getMass()*v2n)/(mMass+ball.getMass());
double v2nPost = (v2n*(ball.getMass()-mMass) + 2*mMass*v1n)/(mMass+ball.getMass());
//Convert scalar velocities to vectors.
Vector postV1N = uN.multiplication(v1nPost), postV1T = uT.multiplication(v1t);
Vector postV2N = uN.multiplication(v2nPost), postV2T = uT.multiplication(v2t);
//Change the balls velocities.
mVelocity.set(postV1N.addition(postV1T));
ball.getVelocity().set(postV2N.addition(postV2T));
}
/**
* When two balls collide can occur an intersection(the distance between the centers
* is less than the sum of the radii) that dephases the response.
* The method fix this situation bringing back the two ball according to their mass.
*/
private void resolveIntersectionWith(Ball ball){
Vector n = mPosition.subtraction(ball.getPosition());
// How much the distance between centers is less than the radii's sum.
double offset = getRadius() + ball.getRadius() - n.length();
n.normalize();
n.multiply(offset);
// Bring back the two ball according to their mass.
mPosition.add(n.multiplication(ball.getMass() * 1.0/(mMass + ball.getMass())));
ball.getPosition().subtract(n.multiplication(mMass * 1.0/(mMass + ball.getMass())));
}
/**
* Normalizes and returns this vector.
*/
// ***INSIDE VECTOR CLASS***
public Vector normalize() {
//Avoid division by zero.
if (mX != 0 || mY != 0) {
double lenght = length();
mX /= lenght;
mY /= lenght;
}
return this;
}
謝謝!
「勢頭不守恆」是什麼意思?球慢下來?加速? – 2013-04-22 16:53:26
當動量守恆時:m1 * v1 + m2 * v2 = m1 * v1'+ m2 * v2'(m1是第一個球的質量,v1是碰撞前的速度,v1是碰撞後的速度) – VanDir 2013-04-22 16:58:00
發表'resolveIntersectionWith'? – Antimony 2013-04-22 16:59:36