2
\$\begingroup\$

I have the following variables:

  • Point of interest which is the position(x,y) in pixels of the place to focus.
  • Screen width,height which are the dimensions of the window.
  • Zoom level which sets the zoom level of the camera.

And this is the code I have so far.

void Zoom(int pointOfInterestX,int pointOfInterstY,int screenWidth,
   int screenHeight,int zoomLevel)
{   
glScalef(1,1,1);
glTranslatef( (pointOfInterestX/2) - (screenWidth/2), (pointOfInterestY/2) - (screenHeight/2),0);

glScalef(zoomLevel,zoomLevel,1);
}

And I want to do zoom in/out but keep the point of interest in the middle of the screen. but so far all of my attempts have failed and I would like to ask for some help.

\$\endgroup\$
7
  • \$\begingroup\$ Do you have zooming and centering working separately otherwise? Seems like you're asking people to write your code for you. \$\endgroup\$
    – House
    Commented Apr 12, 2012 at 16:46
  • \$\begingroup\$ Yes i have tried look at the edited post.Centering works but not when zooming in.. \$\endgroup\$ Commented Apr 12, 2012 at 17:06
  • \$\begingroup\$ Do you zoom back to "normal" before translating? \$\endgroup\$
    – House
    Commented Apr 12, 2012 at 18:20
  • \$\begingroup\$ Yes.I have even tried to multiply each result axis by zoom level but nothing ;( \$\endgroup\$ Commented Apr 12, 2012 at 18:36
  • 1
    \$\begingroup\$ What are you using for a camera? That's likely where you'd want to do the zoom. \$\endgroup\$
    – House
    Commented Apr 12, 2012 at 18:40

1 Answer 1

3
\$\begingroup\$

So you want zoom just like in google maps? If you hover over city and zoom there cursor stays top of that city. This give smooth transition.

        public static final void zoom(float amount, float toX, float toY) {
            float oldZ = camera.zoom;
            camera.zoom += amount;
            // If zooming still has effect then we need translate map towards cursor
            if (camera.zoom < minZoom) {
                    camera.zoom = minZoom;
            } else if (camera.zoom > maxZoom) {
                    camera.zoom = maxZoom;
            } else {
                    float a = (toX - (float) HALFSCREENSIZEX)
                                    * (oldZ - camera.zoom);
                    float b = (-toY + (float) HALFSCREENSIZEY)
                                    * (oldZ - camera.zoom);
                    camera.translate(a, b, 0);
             //check boundaries if you have any.
            }
    }

To make this work with matrix stack you might have to peek there http://code.google.com/p/libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/graphics/OrthographicCamera.java

Zooming is just multiplier for viewport size there. I have done this with matrix stack too but its much cleaner to use camera class.

\$\endgroup\$
2
  • \$\begingroup\$ Sorry for the late reply but i've solved the problem like a month ago :D \$\endgroup\$ Commented Jun 20, 2012 at 17:46
  • \$\begingroup\$ @user1010005 how? \$\endgroup\$
    – elect
    Commented Aug 25, 2014 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .