Jump to content
  • Advertisement
Sign in to follow this  
Calneon

How to get cursor position in game co-ordinates?

This topic is 2784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

New to C++/DX10, want to get the location of the mouse. Using GetCursorPos() at the moment but that gives you pixel co-ordinates (0x0 - 1920x1200) which are useless in my game world. What's the best way of getting the mouse co-ordinates in my game world? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Calneon
What's the best way of getting the mouse co-ordinates in my game world?
It depends. Is your game 2-d or 3-d? (In either case, what you're probably looking for is 'unprojection', but note that there's no one-to-one mapping between points in screen space and points in world space in 3-d, so you'll have to introduce additional constraints in order to perform the conversion in that case.)

Share this post


Link to post
Share on other sites
If you want the position of the cursor "in 3D", look at the sample "Pick10" in the Directx SDK. As jyk said, you can't have the position in the 3D. Actually you can just have a 3D geometric line that start from the camera.

If you want the 2D position use
GetCursorPos(&pos);
GetWindowRect(hwnd, &windowRect);

and you could have the position :

X = pos.x - windowRect.x;
Y = pos.y - windowRect.y;

it's not the fastest way:
Actually, you directly have it in the LOWORD(lParam) & HIWORD(lParam) parameters of a WM_MOUSEMOVE event.

Share this post


Link to post
Share on other sites
I forgot to mention it's a 2D game, but it's set up in DX10 using a 3D viewport and camera with planes facing the camera. The co-ordinates are 0,0 in the centre of the screen (unsure how to change this). pos.x - windowRect.x; wouldn't work for this reason.

Share this post


Link to post
Share on other sites
You can check the direct X picking sample, but in your case it will be a lot simpler. The basic idea is cast a ray from the camera plane through the screen pixel the mouse is over. Then intersect the ray with your 2D game plane.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!