title: Zoom toward mouse (eg. Google maps)
body: <p>I've written a home-brew view_port class for a 2D strategy game. The panning (with arrow keys) and zooming (with mouse wheel) work fine, but I'd like the view to also <em>home</em> towards wherever the cursor is placed, as in <a href="http://www.codinghorror.com/blog/2007/05/zoomable-interfaces.html" rel="noreferrer">Google Maps or Supreme Commander</a></p>
  
  <p>I'll spare you the specifics of how the zoom is implemented and even what language I'm using: this is all irrelevant. What's important is the zoom function, which modifies the rectangle structure (x,y,w,h) that represents the view. So far the code looks like this:</p>
  
  <pre><code>void zoom(float delta, float mouse_x, float mouse_y)
  {
      zoom += delta;
      view.w = window.w/zoom;
      view.h = window.h/zoom;  
      // view.x = ???
      // view.y = ???
  }
  </code></pre>
  
  <p>Before somebody suggests it, the following will <strong>not</strong> work:</p>
  
  <pre><code>view.x = mouse_x - view.w/2;
  view.y = mouse_y - view.h/2;
  </code></pre>
  
  <p>This picture illustrates why, as I attempt to zoom towards the smiley face:</p>
  
  <p><img src="https://i.stack.imgur.com/Vm7F2.png" alt="http://oi43.tinypic.com/16m21au.jpg"></p>
  
  <p>As you can see when the object underneath the mouse is placed in the centre of the screen it stops being under the mouse, so we stop zooming towards it!</p>
  
  <p>If you've got a head for maths (you'll need one) any help on this would be most appreciated!</p>