(SOLVED)Camera math! Keeping the game screen centered when user changes window size


(TaylorAnderson) #1

So I’m trying to get a system going where I can rescale and reposition the game to account for how the user is resizing the window. Right now I have a semi-decent system going for rescaling the game depending on the window size, but now I want it to be centered if there happens to be any white space around the edges (I don’t want the game to resize to the window 1:1, for obvious reasons :P).

I’m thinking its probably a good idea to use FP.camera for this, but I’ve been tinkering with it and I’m afraid I can’t figure out the math. This is what I have so far:

FP.camera.x = 0 - (stage.nativeWindow.width - FP.width - chrome.x)

FP.camera.y = 0 - (stage.nativeWindow.height - FP.height - chrome.y)

(I’ve figured out the size of the outer edges of the window manually, which is what chrome.x and chrome.y are)

Because the code for rescaling the window gets messy when I paste it here, basically all I do is: when the width or height of the window becomes greater than the width or height of the game multiplied by the screen’s scale, increase the screen’s scale by 0.1, and then the opposite for sizing down the window.

Any help on this would be massively appreciated!

-Taylor

EDIT: I guess second question would be: is this possible? seems like the game gets clipped when it ventures too far outside of the top left…


(TaylorAnderson) #2

HAHA! Nevermind. Figured it out myself.

The solution, if anyone cares:

FP.screen.x = (stage.nativeWindow.width - chrome.x - (FP.width * FP.screen.scale))/2

FP.screen.y = (stage.nativeWindow.height - chrome.y - (FP.height * FP.screen.scale))/2