Camera Scale Offset

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
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Camera Scale Offset

Post by Beelz »

Does anyone know a conversion to keep the object centered while zooming in and out? It seems I'm failing pretty hard...

Code: Select all

love.graphics.scale(camSettings.scale, camSettings.scale)

if cameraFollowShip then
	camSettings.x = -ship.x 
	camSettings.y = -ship.y
end
	
love.graphics.translate(camSettings.x + love.graphics.getWidth()/2, camSettings.y + love.graphics.getHeight()/2)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Camera Scale Offset

Post by davisdude »

This is how I did it:

Code: Select all

push = function( layer )
	love.graphics.push( 'transform' ) -- Shouldn't interfere
		love.graphics.origin() -- Prevent scaling and zooming and etc multiple times
		love.graphics.translate( self.offsetX + self.screenX, self.offsetY + self.screenY ) -- Allow for rotation and zooming to occur from center of screen
		love.graphics.rotate( self.rotation )
		love.graphics.scale( self.scaleX * layer.relativeScale, self.scaleY * layer.relativeScale )
		love.graphics.translate( -self.offsetX - self.screenX, -self.offsetY - self.screenY ) -- Move zoom and rotation back to normal for drawing.

		love.graphics.translate( self.offsetX + self.screenX, self.offsetY + self.screenY )
		love.graphics.translate( -self.x, -self.y )
end
pop = function( layer )
	love.graphics.pop()
end
Some of the code could be condensed, I just made them separate in order to be easier to follow.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Camera Scale Offset

Post by pgimeno »

... + love.graphics.getWidth()/camSettings.scale/2, ... + love.graphics.getHeight()/camSettings.scale/2)
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

[SOLVED] Camera Scale Offset

Post by Beelz »

Thank you both for the quick reply, problem solved! :awesome:

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 191 guests