Can some one please help me insert the coefficient of restitution (e) into the following code.
The code as is stands is elastic collision only :-
Code:var dx :Number=cararray[i2].x-cararray[i1].x, dy :Number=cararray[i2].y-cararray[i1].y, dist:Number=Math.sqrt(dx*dx+dy*dy); if (dist<=cardiameter) { // find the angle between the cars var angle:Number=Math.atan2(dy,dx), cosa :Number=Math.cos(angle), sina :Number=Math.sin(angle), // find the current linear momentum on each axis for each car // I think this rotates the coordinate system so that we can // determine a 2 dimensional collision var vx1p:Number=cosa*cararray[i1].velx+sina*cararray[i1].vely, vy1p:Number=cosa*cararray[i1].vely-sina*cararray[i1].velx, vx2p:Number=cosa*cararray[i2].velx+sina*cararray[i2].vely, vy2p:Number=cosa*cararray[i2].vely-sina*cararray[i2].velx; // p = momentum? // v = velocity? var p:Number=vx1p*cararray[i1].mass+vx2p*cararray[i2].mass, v:Number=vx1p-vx2p; vx1p=(p-cararray[i2].mass*v)/(cararray[i1].mass+cararray[i2].mass); vx2p=v+vx1p; // tell each car how fast they're going on each axis cararray[i1].velx=cosa*vx1p-sina*vy1p; cararray[i1].vely=cosa*vy1p+sina*vx1p; cararray[i2].velx=cosa*vx2p-sina*vy2p; cararray[i2].vely=cosa*vy2p+sina*vx2p; // Work out the where the cars are colliding along the // "Line of Action" so that we can project them far enough // away from each other to no longer intersect diff=((cardiameter)-dist)/2; var cosd:Number=cosa*diff, sind:Number=sina*diff; // update their positions cararray[i1].x-=cosd; cararray[i1].y-=sind; cararray[i2].x+=cosd; cararray[i2].y+=sind; }




Reply With Quote