[SOLVED] Dynamic camera scaling dependent on resolution?

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
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

[SOLVED] Dynamic camera scaling dependent on resolution?

Post by unixfreak »

Math has never been my strong point, and i'm in a pickle figuring this out for a platformer engine i've been making.

The default window size is 1024 x 768, when not fullscreen.

Let's say the user had 1920 x 1080 as their resolution, when toggling into fullscreen (desktop) mode the window will simply have a wider draw distance.

How can i calculate the scaling of a camera dynamically, depending on the resolution so that the on screen drawables are always scaled proportionately to the default windowed resolution?

Some screenshots showing the differences, ideally i would like to get the camera scale adjusted in fullscreen mode to look close enough to the windowed version. So that any screen resolution could be used?

Windowed:
Image

Fullscreen (but unwanted extra draw distance):
Image
Last edited by unixfreak on Sat Apr 08, 2017 5:34 pm, edited 1 time in total.
User avatar
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: Dynamic camera scaling dependent on resolution?

Post by unixfreak »

Well more fiddling about this seems to work:

Code: Select all

 local scale =  (default_width / (love.graphics.getWidth() * love.graphics.getHeight()) *1000)
camera.scaleX = scale
camera.scaleY = scale
Although the width is not perfect, how can i improve that to get the exact horizontal view distance in any resolution?
alloyed
Citizen
Posts: 80
Joined: Thu May 28, 2015 8:45 pm
Contact:

Re: Dynamic camera scaling dependent on resolution?

Post by alloyed »

Here's how I do it. I also usually do letterboxing but that's an extra step I'll just drop for now.
Side thing: if you're making a horizontal platformer, maybe you'd prefer to only ever use scale_x, so that the distance between the player and the edge of the screen is fixed and same deal for a vertically facing game. This way you keep the screen size relevant to the gameplay at the expense of looking weird in edge case resolutions

Code: Select all

-- target:  This is the "intended" resolution, the one where you want the camera scale to be x1
-- screen: The actual resolution, as returned by love.graphics.getDimensions()
local scale_x = screen_w / target_w
local scale_y = screen_h  / target_h
local scale = math.min(scale_x, scale_y) -- for pixel perfect scaling, floor the result
-- before drawing
love.graphics.scale(scale, scale)
User avatar
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: Dynamic camera scaling dependent on resolution?

Post by unixfreak »

alloyed wrote: Sat Apr 08, 2017 5:22 pm Here's how I do it. I also usually do letterboxing but that's an extra step I'll just drop for now.
Side thing: if you're making a horizontal platformer, maybe you'd prefer to only ever use scale_x, so that the distance between the player and the edge of the screen is fixed and same deal for a vertically facing game. This way you keep the screen size relevant to the gameplay at the expense of looking weird in edge case resolutions

Code: Select all

-- target:  This is the "intended" resolution, the one where you want the camera scale to be x1
-- screen: The actual resolution, as returned by love.graphics.getDimensions()
local scale_x = screen_w / target_w
local scale_y = screen_h  / target_h
local scale = math.min(scale_x, scale_y) -- for pixel perfect scaling, floor the result
-- before drawing
love.graphics.scale(scale, scale)
Thank you so much, and for explaining it too, that scales perfectly.
That's really helped, much better than the "hack" i came up with. :awesome:
Many thanks!
Post Reply

Who is online

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