Base on this formula, v1 and v2 represent the velocity of the object after the collision. However, to solve an equation with 2 unknowns is to find another equation that has the same 2 unknowns. With this, we look at the Newtonian Kinetic Energy
Et=0.5mv2
where:
m is the mass of the body.
v is speed of the center of mass of the body.
By applying the 2 formulas to each other will results in:
v1=((m1-m2)*v1+2*m1*v1)/(m1+m2))
v2=((m2-m1)*v2+2*m2*v2)/(m1+m2))
In the example, i came up with a one dimensional momentum.
Mass and Velocity can be adjust through the slidebar.
import Vector;
// set mass for 2 balls var m1:Number=1+(m1_mc._x-ml1_mc._x)/30; var m2:Number=1+(m2_mc._x-ml2_mc._x)/30;
// set velocity for 2 balls var v1:Vector=new Vector((v1_mc._x-vl1_mc._x)/15,0); var v2:Vector=new Vector(-(v2_mc._x-vl2_mc._x)/15,0);
// perform scaling for the balls
ball1.circle_mc._xscale=ball1.circle_mc._yscale=m1*100;
ball2.circle_mc._xscale=ball2.circle_mc._yscale=m2*100;
// position balls
ball1._x=ball1._width/2;
ball2._x=-ball2._width/2+Stage.width;
this.onEnterFrame=function():Void{
// displace the balls position by adding velocity
ball1._x+=v1.vx;
ball2._x+=v2.vx;
// simple hit collision detection, repel reaction occur
if(Math.abs(ball1._x-ball2._x)<ball1._width/2+ball2._width/2){
// conservation of momentum goes here var vNw1:Number=((m1-m2)*v1.vx+2*m2*v2.vx)/(m1+m2); var vNw2:Number=((m2-m1)*v2.vx+2*m1*v1.vx)/(m1+m2);
// reset the velocity
v1.reset(vNw1,0);
v2.reset(vNw2,0);