I am very pleased to have found this site and especially this thread. I am creating a ball based game in a language called Lua and have been trying to use your code as follows:

I have multiple balls in the game. The code is so close to working perfectly - just occasionally I get a badly behaved ball that appears to shoot off the screen extremely fast.

Would appreciate if you can spot any issues. I have a maximum speed of 14. Also please note I have a tempX and tempY property for each ball. Once I have checked for all the collisions with all the balls. I then set their actual screen x and y positions.

Hope you can help me identify any issues with my code.

Thanks

Paul

[code]

local function checkForCollisionWithBall( firstBallRef, secondBallRef )

  -- ------------------
  -- Set some constants
  -- ------------------

  local D = 14 -- Radius of ball
  local D2 = (2 * D) * (2 * D) -- Radius squared

 
  -- ---------------------------------------------
  -- Calculate distance between balls on both axis
  -- ---------------------------------------------
 
  local dx = secondBallRef.tempX - firstBallRef.tempX -- horizontal distance between the 2 balls
  local dy = secondBallRef.tempY - firstBallRef.tempY -- vertical distance between the 2 balls
 
     
  local dxy = (dx * dx) + (dy * dy)
 
  if (dxy < 0.00001) then
 
     
 
      return
 
  end
 
 
 
  if (dxy < D2) then
 
      -- --------------------
      -- We have a collision!
      -- --------------------
     
      -- ------------------------------------------------------------------------------
      -- We now perform the square root to calculate the distance between the two balls
      -- ------------------------------------------------------------------------------
     
      dxy = mSqrt(dxy)
     
      local cs = dx/dxy
      local sc = dy/dxy
     
      -- -----------------------------------------------------------
      -- Calculate component of velocity in the direction of (dx,dy)
      -- -----------------------------------------------------------
     
      local vp1 = firstBallRef.horizontalVelocity * cs + firstBallRef.verticalVelocity * sc
      local vp2 = secondBallRef.horizontalVelocity * cs + secondBallRef.verticalVelocity * sc
     
     
 
      local dt = (D + 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
     
      local distance = mSqrt(dx * dx + dy * dy)
      local ax = dx/distance
      local ay = dy/distance
     
     
     
      local va1 = (firstBallRef.horizontalVelocity * ax + firstBallRef.verticalVelocity * ay)
      local vb1 = (-1 * firstBallRef.horizontalVelocity * ay + firstBallRef.verticalVelocity * ax)
     
      local va2 = (secondBallRef.horizontalVelocity * ax + secondBallRef.verticalVelocity * ay)
      local vb2 = (-1 * secondBallRef.horizontalVelocity * ay + secondBallRef.verticalVelocity * ax)
     
     
     
      local vaP1 = va1 + (1 + 1) * (va2 - va1)/(1 + 1/1)
      local 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

 
  end
 
end



[/code]