Jump to content
  • Advertisement
Sign in to follow this  

3D Zooming

This topic is 5200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I easily figured out the best way to zoom for 2D and this is what I came up with:
double temp_x, temp_y;
double lambda;
double move_x, move_y;

temp_x = (Camera.View.x - Camera.Pos.x);
temp_y = (Camera.View.y - Camera.Pos.y);
lambda = arctan(temp_y / temp_x);
move_x = (lambda / (double)90);
move_y = ((double)90 - move_x);
	Camera.Pos.x -= move_x;
	Camera.Pos.y -= move_y;
} else {
	Camera.Pos.x += move_x;
	Camera.Pos.y += move_y;

Logically, that should work fine for a 2D spectrum, of course. Once I did this I felt very stupid, though, as I have no idea how to incorporate the Z axis into this type of an equasion (because the user will be able to rotate the camera around and such, so at times, the X axis will be facing towards and away from the user and the Z axis will be left & right) Any help is appreciated!

Share this post

Link to post
Share on other sites
This obviously depends on what 3D API you're using (perhaps your own software renderer?), but I always change the projection matrix for 3D zooming. The easiest way to do this in OpenGL is to change the clipping planes with glFrustum or change the FOV with Glut.

Share this post

Link to post
Share on other sites
glFrustum (-zoom*(SCREEN_W/SCREEN_H)/4, zoom*(SCREEN_W/SCREEN_H)/4, -zoom/4, zoom/4, maxDistance/2, maxDistance);

Where zoom is the height of the viewable area. Higher number means zoomed farther out. To zoom in use a lower number.

SCREEN_W and SCREEN_H is the screen resolution. This is to set the correct aspect ratio.

maxDistance is the distance to the far clip plane. Anything past this distance won't be drawn.

edit: Note that the near clip plane in this example is set to 1/2 the far clip plane. Play with the /4 and /2 until you get something usable. Better yet, read up on glFrustum.

Share this post

Link to post
Share on other sites
Yeah. Just to elaborate, what you were describing is more of a dolly (physically moving the camera forward and back), where changing the projection matrix (described by MidoriKid) can give u an actual zoom effect. The main difference being that if you are, for example, looking at a person on the other side of a brick wall and you dolly up to him, the camera goes through the wall. If you zoom though, the wall will still be in the view. If that doesn't make sense just picture a real video camera and how it works.

If you do, in actuality, want to dolly the camera, the peice missing from your code at the top is a forward vector. Basically you have a normalized vector pointing in the direction you want the camera to move. Then you simply multiply (scale) that vector by the amount u want to move, and the resulting vector is your new camera location.


Your camera is looking straight down the X axis. That vector would look like this:

[1.0f, 0.0f, 0.0f]

Now you want to move it 3 units forward.

[1.0f, 0.0f, 0.0f] * 3.0f = [3.0f, 0.0f, 0.0f];

Now thats a pretty simple situation, but you'll find it works for more complex situations as well. Just remember to normalize that forward vector!

Hope that makes sense,
Matt Hughson

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!