 # CDK - Objects velocities are weird in some cases

Hi all… Im using the CollisionDetectionKit v 1.5 by Corey…

For anyone who isnt familiar with the kit , just take for granted that ob1 and ob2 are the 2 objects colliding, and that myAngle is the value of the acute angle between the objects colliding…

The problem is that when the two objects collide at some angles (at vertical positions ) , they react oddly ( stop moving, or merge into each other, or shoot away in other directions very fast )… It seems to work fine for all other angles…

Code:

``````function resolveCollisions(collisions)
{
var ob1;
var ob2;
var vx1;
var vx2;
var vy1;
var vy2;

var vn1;
var vn1orig;
var vn2;

var sin;
var cos;

var m1;
var m2;

var myAngle;

for (var k:uint = 0;k<collisions.length;k++)
{
ob1 = collisions[k].object1;
ob2 = collisions[k].object2;

vx1 = vx [circObjects.indexOf(ob1)];
vx2 = vx [circObjects.indexOf(ob2)];
vy1 = vy [circObjects.indexOf(ob1)];
vy2 = vy [circObjects.indexOf(ob2)];

myAngle = Math.abs(collisions[k].angle);

if (myAngle > (Math.PI/2) )
myAngle = Math.abs(Math.PI - myAngle);

cos = (Math.cos (Math.abs(myAngle)) );
sin = (Math.sin (Math.abs(myAngle)) );

m1 = masses[circObjects.indexOf(ob1)];
m2 = masses[circObjects.indexOf(ob2)];

vn1 = (vx1)/cos;

//To calc vn2, need vn1 before the collisions effect is taken into account
vn1orig = vn1;

vn2 = (vx2)/cos;

vn1 = d * ( (m1-m2)/(m1+m2)*vn1 + (2*m2)/(m1+m2)*vn2 );

vn2 = d * ( (2*m1)/(m1+m2)*vn1orig + (m2-m1)/(m1+m2)*vn2 );

vx1 = vn1 * cos;
vx2 = vn2 * cos;
vy1 = vn1 * sin;
vy2 = vn2 * sin;

vx [circObjects.indexOf(ob1)] = vx1;
vx [circObjects.indexOf(ob2)] = vx2;
vy [circObjects.indexOf(ob1)] = vy1;
vy [circObjects.indexOf(ob2)] = vy2;

trace ( myAngle * (180/3.14) );
}
}
``````