Thread: Getting the position of the mouse cursor

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    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?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    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!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get visible mouse cursor height
    By eXistenZ in forum Windows Programming
    Replies: 10
    Last Post: 09-05-2008, 09:46 PM
  2. Class Design question.
    By g4j31a5 in forum C++ Programming
    Replies: 10
    Last Post: 11-08-2006, 10:28 PM
  3. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  5. Mouse position in DirectX
    By Barjor in forum Game Programming
    Replies: 1
    Last Post: 03-09-2003, 09:13 PM