#### Archived

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

# Converting 2D mouse coordinates to 3D world coordinates

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

## Recommended Posts

Hi! How can I convert the 2D mouse coordinates to 3D world coordinates? I may cast a ray from the camera to the mouse coordinates and then use D3DXIntersect to get the picked mesh, that''s ok. But it returns with the mesh''s face, u, v, etc. I am making some character movement in 3D space, so I need the exact coordinates, when I click somewhere on the ground mesh, the character should go there. So any idea? Example? Source code? Thanks. Drakon

##### Share on other sites
Take a look at my screen shot:

##### Share on other sites
the pFaceIndex tells you which triangle has the hit, so find the 3 verticies for that triangle.

the do something like:

v0, v1 and v2 are verts of the triangle
u is the barycentric u coord, and v is the v coord

x = v0.x + u*(v1.x-v0.x) + v*(v2.x-v0.x);
y = v0.y + u*(v1.y-v0.y) + v*(v2.y-v0.y);
z = v0.z + u*(v1.z-v0.z) + v*(v2.z-v0.z);

##### Share on other sites
You might look into the functions project and unproject in the DX library... Basically you pass them your mouse coords and it gives you a 3d Coord which is where your mouse would appear in the 3d space... maybe a little bit of tweeking to get it so that the coord ends up on the plane of your ground...

just some thoughts...

##### Share on other sites
quote:
Original post by dracoon
Hi!

How can I convert the 2D mouse coordinates to 3D world coordinates? I may cast a ray from the camera to the mouse coordinates and then use D3DXIntersect to get the picked mesh, that''s ok.

But it returns with the mesh''s face, u, v, etc. I am making some character movement in 3D space, so I need the exact coordinates, when I click somewhere on the ground mesh, the character should go there.

So any idea? Example? Source code?

Thanks.
Drakon

I''ve had this same problem, and was unable to get it sorted out unfortunately.

If you are using DX9, then the function (i believe) D3DXIntersectTri should help you out though. Take a look, and if you manage to solve the problem let me know!

##### Share on other sites
Well, I ve tried to do this without any intersection function, but i failed. The character is walking the right direction, but i cannot calculate the right distance, because i couldnt implement the perspective view modifiers and the camera distance. But, take a look.

I will try it with D3DXIntersect or something hope it gives better luck. Still cant belive noone can bring a sample...........

[edited by - dracoon on January 19, 2003 10:52:43 AM]

##### Share on other sites
In this thread someone mentions that I need to find out the vertices that make up the face returned from the Intersect method, how would I do that, is there a "easy" way?

Regards

JohanL

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 33
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007538
• ### Who's Online (See full list)

There are no registered users currently online

×