Newbie question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
mediakitchen
Prole
Posts: 8
Joined: Fri Oct 08, 2010 5:39 am

Re: Newbie question

Post by mediakitchen »

Hope you do collision reactions too:)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Newbie question

Post by vrld »

mediakitchen wrote: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:
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
If you have the velocity of each ball, you can give ball1 the new direction of x,y and ball2 the new direction of -x,-y. To model a physically correct reaction, you need to know the mass too, but then you might be better off using love.physics.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
mediakitchen
Prole
Posts: 8
Joined: Fri Oct 08, 2010 5:39 am

Re: Newbie question

Post by mediakitchen »

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

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
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests