As mention earlier on, both the velocity vectors need to be rotate to the same axis. This will enable us to apply the velocity vectors into the law of conservation of momentum formula.
Rotating the vectors might sound complicated, however, thing is made easier with Vector Class. A Vector.rotate() method had been written to handle the rotation of vector easily:
v1.rotate(a);
where
a is the angle of collision (angle between the 2 velocity vectors).
Now we can apply the new velocity vectors into the momentum conservation formula to get 2 new values:
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);
The last step is just to rotate everything back using the angle of collision. Notice that the y values never change. We just deal with the x values, this is also why we need to rotate the vectors along the x axis.
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,(v1_mc._x-vl1_mc._x)/15); var v2:Vector=new Vector(-(v2_mc._x-vl2_mc._x)/15,-(v2_mc._x-vl2_mc._x)/15);
// 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+100;
ball1._y=ball1._height/2;