Converting 2D mouse coordinates to 3D world coordinates

Started by
5 comments, last by dracoon 21 years, 3 months ago
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
Advertisement
Take a look at my screen shot:

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);

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...
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!
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
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]
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

This topic is closed to new replies.

Advertisement