Page 1 of 1

Help with camera issues.

Posted: Wed Jun 26, 2013 7:50 pm
by thyrgle
**Moved from the General forum**

Hi, I am trying to figure out how to use this module: http://nova-fusion.com/2011/04/19/camer ... he-basics/. I have two balls. One ball is called "player" the other is called "enemy". I want my camera to be focused on the midpoint between the two balls. Next, I want the camera to zoom out enough, so that both balls can be seen (with the addition of some padding). This effect, I believe, is similar to Super Smash Bros. I have tried this:

Code: Select all

function updateCamera()
   --Useful legibility vars.
   local p_x = player.body:getX()
   local p_y = player.body:getY()
   local e_x = enemy.body:getX()
   local e_y = enemy.body:getY()

   --Zoom it out based on distance.
   local x = p_x - e_x
   local y = p_y - e_y
   local dist = math.sqrt(x*x + y*y) --Distance formula
   local pad = 0.45
   local scale = math.log(math.abs(dist))*pad --Yeah, this worked best so far, I don't know why...
   camera:setScale(scale,scale)

   --Make camera look at the center between the two balls.
   local mid_point_x = (p_x + e_x)/2 --Midpoint formula
   local mid_point_y = (p_y + p_x)/2
   local offset_x = W/2*scale --Take into account the love2d coordinate system with the offsets
   local offset_y = H/2*scale
   camera:setPosition(mid_point_x-offset_x, mid_point_y-offset_y)
end
My original updateCamera function, which is called in the love.update function, had simply used dist*pad instead of log(abs(dist))*pad, but for some reason, the camera seems to move better with the log (not perfectly though, sometimes a ball will go out of the visibility range, and I also don't know why it works better). Is there a way to fix my updateCamera function so it produces a Super Smash Bros like camera effect?

Thanks.

Re: Help with camera issues.

Posted: Thu Jun 27, 2013 1:56 am
by substitute541
Have you ever tried doing abs(dist)*pad?

Edit: Managed to fix your updateCamera function.

Code: Select all

function updateCamera()
	--Useful legibility vars.
	local p_x = player.body:getX()
	local p_y = player.body:getY()
	local e_x = enemy.body:getX()
	local e_y = enemy.body:getY()

	--Zoom it out based on distance.
	local x = p_x - e_x
	local y = p_y - e_y
	local dist = math.sqrt(x*x + y*y)
	local pad = 0.45
	local diagonal = math.sqrt(W^2 + H^2) -- the diagonal of the game window
	local scale = math.abs(dist)/(diagonal/2) + pad -- this works :D
	camera:setScale(scale,scale)

	--Make camera look at the center between the two balls.
	local mid_point_x = (p_x + e_x)/2
	local mid_point_y = (p_y + e_y)/2 -- originally it was p_y + p_x
	local offset_x = (W/2 - player.shape:getRadius()*3) * scale -- offseted by a bit since it was a little off
	local offset_y = H/2 * scale
	camera:setPosition(mid_point_x-offset_x, mid_point_y-offset_y)
end
In action:
eball2.love
(17.09 KiB) Downloaded 133 times

Re: Help with camera issues.

Posted: Thu Jun 27, 2013 5:57 am
by Robin
You can leave out the math.abs, because dist is already guaranteed to be non-negative. (For x in R, x * x > 0, and so x * x + y * y > 0 and bla bla etc.)

Re: Help with camera issues.

Posted: Thu Jun 27, 2013 7:19 am
by thyrgle
substitute541 wrote:Have you ever tried doing abs(dist)*pad?

Edit: Managed to fix your updateCamera function.

Code: Select all

function updateCamera()
	--Useful legibility vars.
	local p_x = player.body:getX()
	local p_y = player.body:getY()
	local e_x = enemy.body:getX()
	local e_y = enemy.body:getY()

	--Zoom it out based on distance.
	local x = p_x - e_x
	local y = p_y - e_y
	local dist = math.sqrt(x*x + y*y)
	local pad = 0.45
	local diagonal = math.sqrt(W^2 + H^2) -- the diagonal of the game window
	local scale = math.abs(dist)/(diagonal/2) + pad -- this works :D
	camera:setScale(scale,scale)

	--Make camera look at the center between the two balls.
	local mid_point_x = (p_x + e_x)/2
	local mid_point_y = (p_y + e_y)/2 -- originally it was p_y + p_x
	local offset_x = (W/2 - player.shape:getRadius()*3) * scale -- offseted by a bit since it was a little off
	local offset_y = H/2 * scale
	camera:setPosition(mid_point_x-offset_x, mid_point_y-offset_y)
end
In action:
eball2.love
Thank you so much for your help! Sorry for the late reply!