Brian Chia
       
 
categories
actionscript
actionscript 1.0
actionscript 2.0
misc
 
processing
 
links
brian chia
kelvin zhao
ronald leong
 
 
Momentum v02
 
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;

ball2._x=-ball2._width/2+Stage.width-100;
ball2._y=-ball2._height/2+300;

this.onEnterFrame=function():Void{

// displace the balls position by adding velocity
ball1._x+=v1.vx;
ball2._x+=v2.vx;
ball1._y+=v1.vy;
ball2._y+=v2.vy;

var dx:Number=ball2._x-ball1._x;
var dy:Number=ball2._y-ball1._y;
var d:Number=Math.sqrt(dx*dx+dy*dy);

// simple hit collision detection, repel reaction occur
if(Math.abs(d)<ball1._width/2+ball2._width/2){

// find the angle of collision
var a:Number=Math.atan2(dy,dx)*(180/Math.PI);

// rotate velocity vectors by angle of collision to the same axis
v1.rotate(a);
v2.rotate(a);

// conservation of momentum
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,v1.vy);
v2.reset(vNw2,v2.vy);

// rotate velocity vectors back
v1.rotate(-a);
v2.rotate(-a);

// trace the new velocity
trace(v1);
trace(v2);

}

}



suggestion

 
 
 
         
  © copyright 2007 THINKLUNATIC.    
Brian Chia Jonathan Ng