# 3D Zooming

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

## 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);
if(scrolling_out)
{
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 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 on other sites
Yes, I am using OpenGL.
Can you perhaps give me a bit of example source code on this?
Thanks again!

##### 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 on other sites
Thank you! I'll definately crack open my OpenGL book and read up on that tonight.
Thanks again, I owe you a cookie.

##### 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.

Example:

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

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011388
• ### Who's Online (See full list)

There are no registered users currently online

×