Re: Newbie question
Posted: Fri Oct 08, 2010 3:13 pm
Hope you do collision reactions too:)
The separating vector (the direction you need to move the circles so that they don't collide) is also easy to calculate:mediakitchen wrote:Hope you do collision reactions too:)
Code: Select all
-- move ball1 and ball2 so that they don't intersect
local x,y = ball1.x-ball2.x, ball1.y-ball2.y
ball1.x, ball1.y = ball1.x + x/2, ball1.y + y/2
ball2.x, ball2.y = ball2.x - x/2, ball2.y - y/2
Code: Select all
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