I have spent the day trying to figure this out with no joy.

I have created two test movies using Flash, one with 3 balls and one with 5, the links are as follows:

http://www.mediakitchen.co.uk/physics/testFile3balls.html
http://www.mediakitchen.co.uk/physics/testFile5balls.html

Simply click on the ball at the bottom and drag mouse to launch the ball towards the other balls. If you pull it back as much as possible before releasing mouse it will propell the ball faster. It is when it goes faster the problem occurs, especially when you aim towards the middle of the group of upper balls. It is random when it doesn't work so please try a few times.

Ok I thought I  found the problem and changed my loop to a double loop. I could really do with help fixing this problem.

Thanks

Paul

[code]

function initialiseGameConstants() {

objGame.friction = 0.8;

objGame.dV = 0.08;

objGame.D = 14; // Ball radius
objGame.D2 = (2 * objGame.D) * (2 * objGame.D)

}

function moveBallObjects(e:Event):void {

var howManyBallObjects:Number = objGame.ballObjects.length;

for (var i:Number = 0; i < howManyBallObjects; i++) {

var ballObject = objGame.ballObjects[i];

var V = (ballObject.horizontalVelocity * ballObject.horizontalVelocity) + (ballObject.verticalVelocity * ballObject.verticalVelocity);

if (V == 0) {

continue;
}

V = Math.sqrt(V);

var k = (V - objGame.dV) / V;

if (k < 0)
{
k = 0;
} // end if

ballObject.horizontalVelocity = ballObject.horizontalVelocity * k;
ballObject.verticalVelocity = ballObject.verticalVelocity * k;

ballObject.tempX = ballObject.tempX + ballObject.horizontalVelocity;
ballObject.tempY = ballObject.tempY + ballObject.verticalVelocity;

objGame.numTotalBalls = 3;

for (var j:Number = 0; j < objGame.numTotalBalls; j++) {

var firstBallObject = objGame.ballObjects[j];

for (var f:Number = (j + 1); f < objGame.numTotalBalls; f++) {

var otherBallMC = objGame.ballObjects[f];

checkForCollisionWithBall(firstBallObject, otherBallMC);

}

}

// ---------------------------------
// Update Position of Ball On Screen
// ---------------------------------

ballObject.x = ballObject.tempX;
ballObject.y = ballObject.tempY;

}

}

function checkForCollisionWithBall(firstBallRef:MovieClip, secondBallRef:MovieClip):void {

var dx = secondBallRef.tempX - firstBallRef.tempX // horizontal distance between the 2 balls
var dy = secondBallRef.tempY - firstBallRef.tempY // vertical distance between the 2 balls

var dxy = (dx * dx) + (dy * dy)

if (dxy < 0.00001) {

return

}

if (dxy < objGame.D2) {

// --------------------
// We have a collision!
// --------------------

// ------------------------------------------------------------------------------
// We now perform the square root to calculate the distance between the two balls
// ------------------------------------------------------------------------------

dxy = Math.sqrt(dxy)

var cs = dx/dxy
var sc = dy/dxy

// -----------------------------------------------------------
// Calculate component of velocity in the direction of (dx,dy)
// -----------------------------------------------------------

var vp1 = firstBallRef.horizontalVelocity * cs + firstBallRef.verticalVelocity * sc
var vp2 = secondBallRef.horizontalVelocity * cs + secondBallRef.verticalVelocity * sc

var dt = (objGame.D + objGame.D - dxy) / (vp1 - vp2)

firstBallRef.tempX = firstBallRef.tempX - (firstBallRef.horizontalVelocity * dt)
firstBallRef.tempY = firstBallRef.tempY - (firstBallRef.verticalVelocity * dt)

secondBallRef.tempX = secondBallRef.tempX - (secondBallRef.horizontalVelocity * dt)
secondBallRef.tempY = secondBallRef.tempY - (secondBallRef.verticalVelocity * dt)

dx = secondBallRef.tempX - firstBallRef.tempX // horizontal distance between the 2 balls
dy = secondBallRef.tempY - firstBallRef.tempY // vertical distance between the 2 balls

var distance = Math.sqrt(dx * dx + dy * dy)
var ax = dx/distance
var ay = dy/distance

var va1 = (firstBallRef.horizontalVelocity * ax + firstBallRef.verticalVelocity * ay)
var vb1 = (-1 * firstBallRef.horizontalVelocity * ay + firstBallRef.verticalVelocity * ax)

var va2 = (secondBallRef.horizontalVelocity * ax + secondBallRef.verticalVelocity * ay)
var vb2 = (-1 * secondBallRef.horizontalVelocity * ay + secondBallRef.verticalVelocity * ax)

var vaP1 = va1 + (1 + 1) * (va2 - va1)/(1 + 1/1)
var vaP2 = va2 + (1 + 1) * (va1 - va2)/(1 + 1/1)

firstBallRef.horizontalVelocity = vaP1 * ax - vb1 * ay
firstBallRef.verticalVelocity = vaP1 * ay + vb1 * ax

secondBallRef.horizontalVelocity = vaP2 * ax - vb2 * ay
secondBallRef.verticalVelocity = vaP2 * ay + vb2 * ax

firstBallRef.tempX = firstBallRef.tempX + firstBallRef.horizontalVelocity * dt
firstBallRef.tempY = firstBallRef.tempY + firstBallRef.verticalVelocity * dt

secondBallRef.tempX = secondBallRef.tempX + secondBallRef.horizontalVelocity * dt
secondBallRef.tempY = secondBallRef.tempY + secondBallRef.verticalVelocity * dt

}

}

[/code]