i already solved the momentum and energy things, but the matter is the velocity vector after collision in 2D/3D coordinate.
in pool/billiard, the mass of the balls are the same (m1=m2=m). so to get the velocity after collision is by switching the velocity.
v1a = v2b
v2a = v1b
thanks,
Hi,
I was physics major in graduate school. If you need help, please contact me. I used to write an elastic/inelastic, bound/boundless collision simulation in Java applet. If you need source code, I can try to find it and (if I can) send to you as well.
qizhi@sinotar.com
www.sinotar.com -- internet values.
My friend and I made this a while ago. Sorry for the lack of comments, it was mostly written very quickly after scribbling on lots of pieces of paper.
It calculates a perfectly elastic collision given a position, velocity vector, and mass.
/*
the all-knowing, all-seeing, do-everything bouncing ball function
give unto me the position of your balls, their masses, and their component vectors
and I shall bestow unto you their resultant vectors in an array of doubles
by abe and john, april 5 2002
*/
double[] calculateBounce( double x1, double y1, double m1, double dx1, double dy1,
double x2, double y2, double m2, double dx2, double dy2 )
{
// calculate a bunch of things
double angle = findAngle( x2 - x1, y1 - y2 ),
b1vector = findAngle( dx1, dy1 ),
b1mag = findDist( dx1, dy1 ),
b1parl = b1mag * Math.cos( b1vector - angle ),
b1perp = b1mag * Math.sin( b1vector - angle ),
b2vector = findAngle( dx2, dy2 ),
b2mag = findDist( dx2, dy2 ),
b2parl = b2mag * Math.cos( b2vector - angle ),
b2perp = b2mag * Math.sin( b2vector - angle ),
newb1parl = collision( b1parl, m1, b2parl, m2 ),
newb1vector = findAngle( newb1parl, b1perp ) + angle,
newb1mag = findDist( newb1parl, b1perp ),
newb2parl = collision( b2parl, m2, b1parl, m1 ),
newb2vector = findAngle( newb2parl, b2perp ) + angle,
newb2mag = findDist( newb2parl, b2perp ) ;
// return an array with the resultant vectors { dx1, dy1, dx2, dy2 }
return new double[] { newb1mag * Math.cos( newb1vector ), newb1mag * Math.sin( newb1vector ),
newb2mag * Math.cos( newb2vector ), newb2mag * Math.sin( newb2vector ) } ;
}
double collision( double v1, double m1, double v2, double m2 )// linear bouncing
{
return ( m1 - m2 ) / ( m1 + m2 ) * v1 + 2 * m2 * v2 / ( m1 + m2 ) ;
}
public double findDist( double x, double y )
{
return Math.sqrt( x * x + y * y ) ;
}
public double findAngle( double x, double y )
{
if( x == 0 && y >= 0 )
return Math.PI / 2 ;
if( x == 0 && y < 0 )
return 3 * Math.PI / 2 ;
double a = Math.atan( (double) y / x ) ;
if( x < 0 )
a += Math.PI ;
return a ;
}
hi,
you can check my physics if you want at
http://www.geocities.com/calzada_us/Billard3D/Pool.html
Franck
source for ballI collide ballJ
-
Ball ballI = Pool.instance().getBall(i);
Ball ballJ = Pool.instance().getBall(j);
Debug.log(
Debug.FINE, Events.class, "setBallEvent BALL_COLLIDE_BALL", "ballI={0}",
ballI
);
Debug.log(
Debug.FINE, Events.class, "setBallEvent BALL_COLLIDE_BALL", "ballJ={0}",
ballJ
);
double v1, v2, r1, r2, s = 2, t, v, apr = 1.0;
double diffX = ballI.getPosition().x - ballJ.getPosition().x;
double diffY = ballI.getPosition().y - ballJ.getPosition().y;
double diffSpeed = Math.sqrt(diffX * diffX + diffY * diffY);
diffX /= diffSpeed;
diffY /= diffSpeed;
v1 = ballI.getVitesse().x * diffX + ballI.getVitesse().y * diffY;
r1 = ballI.getVitesse().x * diffY - ballI.getVitesse().y * diffX;
v2 = ballJ.getVitesse().x * diffX + ballJ.getVitesse().y * diffY;
// impact velocity
r2 = ballJ.getVitesse().x * diffY - ballJ.getVitesse().y * diffX;
//
t = (v1 + v2) / s;
v = t + apr * (v2 - v1) / s;
ballI.getVitesse().x = v * diffX + r1 * diffY;
ballI.getVitesse().y = v * diffY - r1 * diffX;
v = t + apr * (v1 - v2) / s;
ballJ.getVitesse().x = v * diffX + r2 * diffY;
ballJ.getVitesse().y = v * diffY - r2 * diffX;
ballI.setNewAcceleration();
ballJ.setNewAcceleration();
Debug.log(
Debug.FINE, Events.class, "setBallEvent BALL_COLLIDE_BALL done", "ballI={0}",
ballI
);
Debug.log(
Debug.FINE, Events.class, "setBallEvent BALL_COLLIDE_BALL done", "ballJ={0}",
ballJ
);
if (Pool.instance().isShotting()) {
ballI.setCollideSoundEnable(true);
}