Search code examples
graphicszoomingdirectx-9scrollwheel

Math for zoom to cursor on mouse wheel scrolling


Have to implement a Directx9 project that involves zoom towards the cursor like Google maps with the mouse scroll wheel (similar to this implementation by Phrogz).

Need the math and the variables required for the same.


Solution

  • Solved this problem using below steps

    • Decide per scroll movement, call it Z-SHIFT, in Z-direction towards the target point such that the camera should travel to target in fixed scrolls(SCROLL_COUNT)

    • Calculate the distance to travel in X and Y directions, say DIST_X and DIST_Y

    • Movement per scroll in X-direction and Y-direction will be calculated as

      • X-SHIFT = DIST_X/SCROLL_COUNT

      • Y-SHIFT = DIST_Y/SCROLL_COUNT

      • Z-SHIFT = Pre decided suitable value

    We have mathematical equation to guide the coordinates of the camera per scroll which when placed in the code provides the required zoom to cursor effect.