Thanks to Tetrad (for pointing out D3DXMatrixOrthoLH ) I have figured out how to set it up in DirectX9. However, the problem I am now facing is my 0,0 mouse position is now in the middle.

What I Want To Achieve: I want my 0,0 origin to be the top left of the window (not the middle). I also want to be able to zoom in.

Thanks for any help!

edit (this seems to add the 0,0 to the bottom left)

D3DXMATRIX m_d3dts_projection;
D3DXMatrixOrthoOffCenterLH ( &m_d3dts_projection , 0.0f , console_editor.window_w , 0.0f , console_editor.window_h , 1.0f , 10.0f );
console_editor.directx_device->SetTransform ( D3DTS_PROJECTION , &m_d3dts_projection );

Old Post -- Dont Answer

I have been developing a map editor in C++ DX9 for the past couple of months, and have ran into a particular development problem.

How my map editor works: I split the screen into 16x16 cell blocks each with their own struct for rendering. When you click on the window, it will find the cell block that you clicked based on the win32 whereabouts of the mouse cursor. It then will do its magic. This seems to work well with a FOV of D3DXToRadian(float 90).


I also want my map editor to be able to zoom in and to zoom out, so when I increase or decrease the FOV, the mouse cursor and the cell block code are all correct, but wrong. Example:

Normal 90 FOV - Looks Good Normal 90 FOV

Zoomed In - Opps Zoomed In

Question: How can I add functionality of zooming in and out, but still have the mouse position be relevant to the render? I assume I have to recode how everything works, and I am okay with doing that...

  • \$\begingroup\$ Usually for these kinds of things you don't use a perspective camera and instead use an orthographic camera. So you'd pass in an orthographic size instead of an FOV. It might make some of your math easier. \$\endgroup\$ – Tetrad Apr 18 '13 at 22:20
  • \$\begingroup\$ @Tetrad Oh, Ive never done anything with D3DXMatrixOrthoLH before. Ive looked it up, but cant seem to get it working. Do you have sample source code on how to set it up? \$\endgroup\$ – User Apr 20 '13 at 2:42
  • \$\begingroup\$ Read the documentation, you'll find D3DXMatrixOrthoOffCenterLH - msdn.microsoft.com/en-us/library/windows/desktop/… \$\endgroup\$ – Maximus Minimus Apr 20 '13 at 21:48
  • \$\begingroup\$ @mh01 Thank you! Ill check it out, looks like what I need. \$\endgroup\$ – User Apr 21 '13 at 2:24
  • \$\begingroup\$ @mh01 Seems to place the 0,0 in the bottom left. See above code. \$\endgroup\$ – User Apr 21 '13 at 3:50

Take the position of the mouse and divide it by the width of the tiles (relative to the zoom level) will give you the tile it is hovering over.

For example, if your base tiles are 25 pixels wide, and you zoom in to 2x, your tiles will be 50 pixels wide. If your mouse is at 255x and 325y, divide by 50 each and you get 5x 6y (using integer division).

You may have to do some extra math if the center of your screen is 0,0 but the premise is the same.

| improve this answer | |
  • \$\begingroup\$ How can I figure out my zoom level? \$\endgroup\$ – User Apr 18 '13 at 18:23
  • \$\begingroup\$ That is a little more difficult in a 3d space. The easiest way is to just find the width of a single tile and divide your mouse position by that. I do not know all the math required for that yet. Play around with some numbers and see if you can use distance from the surface as your zoom level. for example your default view will probably be 1 but zooming in to half that distance would probably be 2x. \$\endgroup\$ – UnderscoreZero Apr 18 '13 at 19:44

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.