Archived

This topic is now archived and is closed to further replies.

How do I transform the mouse position to 3d

This topic is 6159 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

I''ve been trying to figure this out on my own, and read the DX manual to figure out, but I just can''t get it to work. You see, I''m trying to transform a mouse click into 3d space. It''s purpose is to select tiles for a map editor. The view is from above, but not totally from the top. I just can''t figure it out and if anyone has some example code please show me. Thanks a lot anyone who helps, it''ll help me make a killer game

Share this post


Link to post
Share on other sites
I am no DX expert, but I have programmed an application that gets the 2d (x,y) coordinates of a mouse and then transforms them into 3d space using a formula:

mouse_3d_x=distance*(mouse_x/(distance*mouse_z))+SCREEN_WIDTH;
mouse_3d_y=distance*(mouse_y/(distance*mouse_z))+SCREEN_HEIGHT;

the distance variable is the veiwing distance which is using at 256 for some reason. There are other 3d to 2d transformation formulas and all u have to do is check them out.

But you must convert those mouse_x and mouse_y positions into 3d
first before you can do anything.

This is how I did it when I made my application:

1. Get the 2d coordinates of the mouse x,y
2. Transform to a 3d space

if (xif (x>=SCREEN_WIDTH) mouse_x= x-(SCREEN_WIDTH/2);
if (yif (y>=SCREEN_HEIGHT) mouse_y= y-(SCREEN_HEIGHT/2);

The above should now have variables mouse_x, mouse_y in the range of (-SCREEN_WIDTH,-SCREEN_HEIGHT) to(SCREEN_WIDTH,SCREEN_HEIGHT)

3. mouse_z should be set to a value of the z axis try 0 for a
default value.

4. When you want manipulate, say, a vertex and you know the z
value is at maybe -100, change the z value of mouse_z to -100
5. Use the above formula:

mouse_3d_x=distance*(mouse_x/(distance*mouse_z))+SCREEN_WIDTH;
mouse_3d_y=distance*(mouse_y/(distance*mouse_z))+SCREEN_HEIGHT;

to find your 2d coords of where your mouse cursor should be place.

6. When you change the Z value in mouse_z to maybe -100 and try
to maybe manipulate a vertex point whose z maybe at -100 you
should find that it works.

If you do change the value in mouse_z, which u will find u have to if you wanna address a vertex in another z zone or whatever the mouse my move differently, ie when moving the mouse, since it's in another z coordinate it may move up the screen slower, that's how you know it's in 3d because objects in the distance look like they are moving slower anyway.

when you have found mouse_3d_x and mouse_3d_y you may wanna set the mouse cursor at those positions if using DirectX there must be a function that sets the position or maybe if you are using something else then place a sprite at that position that represents the mouse cursor.

I know that this may not be the best way to do and there must be far more better ways of doing it. I have used this method and it's worked fine for me. But my friends don't like this method and regard it as slow or un useful. Basically this the way that works best for me, if you don't like this idea just use this as a guideline :

1. Get mouse positions

2. Somehow transform them into 3d space or have them in 3d space
all the time and move them by a set amount each frame or loop
in accordance to how much the mouse has move since the last
frame call it dy, dx and add to your 3d mouse positions

3. transform these into 2d space using a formula that works best
to you. Try the formula that you use for your 3 application
if that's what suits you.

4. Put a sprite or icon image to represent the mouse cursor in
new positon.


I hope I have helped you or at least given u some ideas. I know sometimes my methods are abit harsh and people don't really like them. (not people here but those that I work with).

Hope it helped

Dark Star



Edited by - Dark Star on February 1, 2001 7:19:07 AM

Share this post


Link to post
Share on other sites
Thanks for your help, but I don''t understand what the distance variable is. Also, in my program the view is from the top, so when the mouse is moved up, it''s y position shouldn''t necessarily change, but it''s z position should increase. Does this effect the procedure?

Share this post


Link to post
Share on other sites
The first thing to realise is that a 2D point is equivalent to a 3D line. That is, the screen coordinates of the mouse could represent any point in 3D space along a particular line.

This line can be found by "unprojecting" the mouse coordinates. I don''t know if DirectX does this, never having used it, but the procedure is reasonably simple. I think it involves inverting the projection matrix.

Once you have your line, you then have to make some decisions about how you''re going to decide where on this line the 3D cursor is. If your level is truly 3-dimensional, there''s likely no easy solution to this. You might like to assign keys to move the mouse in this third dimension. If your level is 2D, you can almost certainly make an intelligent guess as to the position of the cursor on this line.

HTH!

Share this post


Link to post
Share on other sites