I have a DirectX scene. I want zoom in using mouse wheel to the mouse location. I am able to zoom the view to center of screen but I need to zoom to mouse location. Please suggest how it can be done. Thanks


You need to send a ray from the mouse cursor along the camera view direction through the scene and see where it intersects (take the intersect position closest to the camera position) to know the 3D position. After that you can easily zoom in there by placing the camera closer.

This is often called unprojecting and can be done by fiddling a bit with your projection matrix and the screen coordinates of your mouse cursor.

| improve this answer | |
  • \$\begingroup\$ Thanks for the reply Roy T. I am able to get the 3D position and able to unproject but I am able to zoom to the center of screen as my camera always looks at the center of viewing area. So, my question here is how do I place the camera around the mouse cursor point so that it looks like it is zooming to mouse position. \$\endgroup\$ – Pankaj Sep 6 '11 at 13:14
  • \$\begingroup\$ First set your camera look at to the 3D position of the mouse, then move closer to that point. Of course this will make the mouse cursor position the center of the screen. Alternatively you could figure out the distance between the screen-position of the mouse and the center of the screen. Then calculate what the same distance in screen space units would be zoomed in and use a parallel ray to move the camera along, this way the 3d position of the cursor will stay in the same relative screen space area but will come closer. \$\endgroup\$ – Roy T. Sep 6 '11 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.