[quote author=mediakitchen link=topic=4.msg7358#msg7358 date=1286554366]
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]
[/quote]

[quote author=Fu-Kwun Hwang link=topic=4.msg7356#msg7356 date=1286536017]
You have the following code
[code]
dt = (r1+r2-d)/(vp1-vp2);
[/code]
What if vp1==vp2???


You also have the following code

[code]
CGFloat d=sqrt(dx*dx+dy*dy);
//First calculate the component of velocity in the direction of (dx,dy)
CGFloat vp1= speed.x*dx/d+speed.y*dy/d;
CGFloat vp2= collideeSpeed.x*dx/d+collideeSpeed.y*dy/d;
[/code]

What if d==0???

It could also be other kind of bug in your code. You need to trace the code (find out value for variables) and try to find out the problem.
It is not easy to find the bug.  Good luck!
[/quote]

@prof, i've found the bug , it's on collision.
@mediakitchen i have multiple balls , how do you compare each array without having compare the same one.
for example Array 1 (ball1,ball2,ball3), Array 2 (ball1,ball2,ball3) how do you skip the same last one?