# Getting the position of the mouse cursor

Printable View

• 12-26-2007
Mavix
Getting the position of the mouse cursor
I'm using Direct3D in C#, and I was wondering how I can get the position of the mouse cursor in the 3d space. For example, the camera is looking straight down on a map. Its position is 0,0,30, which is at the top left corner of the map. I can get the position by making my game fullscreen and using c# to get the position. But I dont want fullscreen. And if the camera moves 10 pixels to the right, then when the mouse is at the top left corner of the window, its position should be 10,0.
Anyone know how I can do this?
• 12-26-2007
CornedBee
The mouse is a 2d pointer device. To get its 3d position, you need to calculate the depth somehow. (Actually, you need to guess, not calculate.)

First, you need to get the position relative to your window. That should be easy.

Then you need to reverse the computation done to draw pixels on the screen. The theory behind this computation is that you have a camera somewhere in your 3d space (you should have the coordinates, of course), and it looks at the scene. The projection matrix then calculates the 2d position on your screen where a light ray from the 3d point in question would be on the camera image.
So you need to reverse this projection. The problem is that the projection is lossy - instead of 3 coordinates, you're left with 2. So to decide what the mouse points at, you basically need to say, well, it could point at all these points - which one does the user mean? If the user points somewhere with the mouse, it's a pretty safe assumption that he means to point at the foremost thing in his sight, the thing nearest to him.
So basically, you need to cast a ray from your camera in the direction that is equivalent to the mouse position and see which object the ray hits first. That's the object that the mouse it pointing at. The process is called root finding, and there various approaches to it. Your best bet is to consult a math reference, preferably one geared at graphics and game programming.
• 12-26-2007
VirtualAce
Direct3D has done the math for you in the D3DX library and in the .NET framework. Check out the D3DX functions and/or .NET methods and you will find what you need.
• 12-27-2007
Mavix
Woah, I have no clue what you just said, so I'll go with what Bubba said. I'm also going to be buying a book on directx in c#, which was written by the development lead for managed directx. BTW, how do you cast a ray? I dont want to do that, but I would like to know how its done.
Thanks for the help!
• 12-27-2007
dwks
I think you probably mean ray tracing and not ray casting, right?

Stick "c# ray tracing" into Google. Check out Wikipedia.
http://en.wikipedia.org/wiki/Ray_tracing
http://en.wikipedia.org/wiki/Ray_casting

http://www.c-sharpcorner.com/UploadF...ayTracing.aspx
http://www.gup.uni-linz.ac.at/~gk/Co...rafik/ray.html
http://www.devnewz.com/devnewz-3-200...C-and-NET.html

I think you might have to be more specific.
• 12-27-2007
CornedBee
No, I actually mean ray casting. Ray tracing is for drawing. Ray casting is for stuff like visibility testing, mouse targets, etc.

The math involved is pretty much the same, though.