# Steady zoom on center in LWJGL (Modelview)

I am having a problem in LWJGL with zooming in and out. I am using glScaled(zoom, zoom, 1) before glTranslated. There are 2 problems: 1. The rate of zoom speeds up a lot when zooming out (lower zoom value). 2. It zooms in on the bottom left corner of the screen rather than the center. Eventually, I would like to have the zoom focused on the mouse position.

I have tried to fix these problems by make it glScaled(zoom^12, zoom^12, 1) so that the greater the zoom value, the faster it will zoom in order to balance out the faster zoom at lower zoom values. To compensate for the zoom focused on the bottom left, I have tried to subtract (zoom+1)^10 + 2^10 from the X and Y of each sprite. This results in a curved zoom path, first to the left and then to the right. It is a 2D game.

## 1 Answer

These should be considered as two separate problems:

The center-point problem

glScale always acts on the center of your coordinate system. You can do scaling at a different point by translating that point to the center temporarily. Just do this sequence:

glTranslated(centerX, centerY, centerZ);
glScaled(zoom, zoom, 1);
glTranslated(-centerX, -centerY, -centerZ);


(I might have the order reversed.)

The speed problem

Suppose you are adjusting your zoom variable at a rate of 1.0 per second. Suppose your zoom level is 2.0, and the user is zooming out. Then it takes 1 second to go from 2.0 to 1.0, and 1 second to go from 1.0 to 0.0 — at which point things are infinitely small.

What you probably want is for the visible zoom speed to be proportional to the current zoom value. There are two ways to do this:

• When you modify your zoom variable, use the current value as a multiplier: zoom in with zoom *= 1 + SPEED and zoom out with zoom /= 1 + SPEED.

• Compute the scale factor as an exponential function of the zoom variable.

double scale = Math.exp(SPEED*zoom);
glScaled(scale, scale, 1);


These two methods are completely equivalent except for needing slightly different values for SPEED. (If you want to learn the math behind this, looking up compound interest and continuous compounding would be a place to start.)

• Thanks. I totally knew that first thing I was just derping. – l5p4ngl312 Dec 1 '11 at 1:45