Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D Camera Zoom

Started by GameDevMan Jan 3, 2009 at 1:08 PM 1 replies 5.2k views
Original Post
GameDevMan
GameDevMan
I made this harder than it needs to be by programming the engine in the way I did, but here's my question. I want to create a simple zooming effect with 2D sprites -- simply making the on-screen picture larger or smaller with no "pseudo-3D". I'm stuck on what kind of math to apply in order to get the camera to zoom in, but still stay on "target", as simply blowing out the vectors will throw the camera off-center. With my engine, (0,0) is the Top-Left corner of the screen as opposed to the center. Changing the Camera X higher will move the image to the left. I've successfully gotten the image to zoom in and out, but my only problem is to keep the camera 'centered' on its target. CameraZoom = 1 means that the image isn't zoomed. CameraZoom = 2 means that the image is scaled x2. CameraZoom = .5 means that the image is "x2" away. If anymore clarification is needed, let me know. Thanks a lot in advance. [Edited by - Rokkuman on January 3, 2009 1:43:36 PM]
HappyCoder
HappyCoder
I think you can do it like this.

screenCoords = zoom * (worldCoord - worldOffset) + screenCenter;

where

worldCoord is a point in the 2D scene to convert to screen coordinates
worldOffset is the offset of the world in world units
zoom is a number representing the zoom level
screenCenter is the zoom center on the screen or where the camera will zoom in and out on. You will probably just want to use (screenWidth / 2, screenHeight / 2)

Now you just need to take any points in the scene and pass them through those transformations. If you also need to transform dimensions, use this.

screenSize = zoom * size;
My current game project Platform RPG
erissian
erissian
Without knowing more about how you track and handle the data, my best suggestion is that you scale the camera position inversely. That is, if everything is scaled by 2, then the camera position will also be scaled by 2. In order to translate it back to the absolute screen coordinates, you will have to divide it by 2.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.