Help with camera issues.

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.
Post Reply
thyrgle
Prole
Posts: 10
Joined: Wed Jun 26, 2013 6:30 pm

Help with camera issues.

Post 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.
Attachments
Elementball.love
(17.78 KiB) Downloaded 111 times
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Help with camera issues.

Post 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 131 times
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with camera issues.

Post 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.)
Help us help you: attach a .love.
thyrgle
Prole
Posts: 10
Joined: Wed Jun 26, 2013 6:30 pm

Re: Help with camera issues.

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

Who is online

Users browsing this forum: Google [Bot] and 5 guests