Newbie question
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 8
- Joined: Fri Oct 08, 2010 5:39 am
Re: Newbie question
Hope you do collision reactions too:)
Re: Newbie question
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:)
local x,y = ball1.x-ball2.x, ball1.y-ball2.y -- points from ball2 to ball1
To move the circles so that they don't intersect each other, first make the vector length 1:
local distance = math.sqrt(x*x + y*y)
x,y = x/distance, y/distance
then move ball 1 by x*distance/2,y*distance/2 and ball2 by - x*distance/2,-y*distance/2
Note that you don't actually have to calculate the distance, since the term will be canceled out. In total the response may look like this
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
-
- Prole
- Posts: 8
- Joined: Fri Oct 08, 2010 5:39 am
Re: Newbie question
Thanks vrld - I will look at your code now. In the meantime I would like to show my code so far. Please note I have based this on some examples on a physics site and I don't really understand some of it as I struggle with vectors.
This code so nearly works but occasionally one or more balls just shoot off or get stuck together
If anyone can understand this and spot anything that could be causing the problems I would be most grateful.
Thanks
Paul
This code so nearly works but occasionally one or more balls just shoot off or get stuck together
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
If anyone can understand this and spot anything that could be causing the problems I would be most grateful.
Thanks
Paul
Who is online
Users browsing this forum: No registered users and 3 guests