Converting mouse coords to 3D

Started by
5 comments, last by d1820 20 years, 5 months ago
I have mouse coords stored in a database for where certain objects go. these coords are in pixels. how can i convert these to 3D coords so when i load an object in directX i can place that object in the coorect position. I have looked at the ray picking example, and i can not see where the 3D coords are being produced. for ex my mouse coords are 12, 12 which would put me at top left of window. when i try and use the ray picking and return back the destRay i am sitting in the middle of the screen. any code example would be the greatest help. but at this frustrating point i wil take any insight at all thanks
Advertisement
You can unproject two vectors from your mouse coords, one at (x,y,0.0) and one at (x,y,1.0). Then you can use those to get the point and the direction of your ray, which you can pass into D3DXIntersectPlane or whatever it''s called to get back the 3d coordinates on some plane (for instance the plane at 0,0,0 with normal 0,1,0).
In the unproject, i have tried that but i get values back of all 0.0, i think that might be due to the fact i dont have a viewport that i am using.. here is the code that i have tried:

try 1
g_D3DDevice.GetTransform D3DTS_PROJECTION, tempProj

''reset teh back buffer size if a resize of teh form occured
Set pBackBuffer = g_D3DDevice.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
pBackBuffer.GetDesc m_d3dsdBackBuffer

''v.x = (((2 * x) / m_d3dsdBackBuffer.Width) - 1) / tempProj.m11
''v.y = -(((2 * y) / m_d3dsdBackBuffer.Height) - 1) / tempProj.m22

v.z = 1#

''// Get the inverse view matrix
g_D3DDevice.GetTransform D3DTS_VIEW, tempview
D3DXMatrixInverse m, vbNull, tempview




''// Transform the screen space pick ray into 3D space
vPickRayDir.x = v.x * m.m11 + v.y * m.m21 + v.z * m.m31
vPickRayDir.y = v.x * m.m12 + v.y * m.m22 + v.z * m.m32
vPickRayDir.z = v.x * m.m13 + v.y * m.m23 + v.z * m.m33
D3DXVec3Normalize vPickRayDir, vPickRayDir
vPickRayOrig.x = m.m41
vPickRayOrig.y = m.m42
vPickRayOrig.z = m.m43

D3DXMatrixInverse invWorldMatrix, vbNull, gmesh(a).g_matworld
D3DXVec3TransformCoord vLocalRayOrig, vPickRayOrig, invWorldMatrix
D3DXVec3TransformNormal vLocalRayDir, vPickRayDir, invWorldMatrix

g_D3DX.Intersect gmesh(a).g_Mesh, vLocalRayOrig, vLocalRayDir, bHit, dwFace, fBary1, fBary2, fDist, fcount


try 2
''Compute the vector of the pick ray in screen space
g_D3DDevice.GetTransform D3DTS_PROJECTION, tempProj

''reset teh back buffer size if a resize of teh form occured
Set pBackBuffer = g_D3DDevice.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
pBackBuffer.GetDesc m_d3dsdBackBuffer

v.x = (((2 * x) / m_d3dsdBackBuffer.Width) - 1) / tempProj.m11
v.y = -(((2 * y) / m_d3dsdBackBuffer.Height) - 1) / tempProj.m22

v.z = 1#

''// Get the inverse view matrix
g_D3DDevice.GetTransform D3DTS_VIEW, tempview
D3DXMatrixInverse m, vbNull, tempview

Dim nd As D3DVECTOR
Dim fd As D3DVECTOR
Dim vo As D3DVECTOR
Dim vp As D3DVIEWPORT8

Call D3DXVec3Unproject(nd, vec3(v.x, v.y, 0), vp, matProj, matView, matWorld)
Call D3DXVec3Unproject(fd, vec3(v.x, v.y, 1), vp, matProj, matView, matWorld)



results from try 2
?nd.x
0
?nd.y
0
?nd.z
0
?fd.x
0
?fd.y
0
?fd.z
0

?v.x
-0.4093461
?v.y
0.4077281


any other insight to this problem would be great, and much needed

thank you
You''re passing in matProj into your intersect call, but where are you getting it? I see a call to load tempProj with the projection matrix. You should just need to load up the view, world, and projection matrices from the matrices you have set, load up the current viewport, and call unproject. I don''t think you need to invert you view matrix either.
code is:

g_D3DDevice.GetTransform D3DTS_PROJECTION, tempProj
g_D3DDevice.GetTransform D3DTS_WORLD, tempWorld

''reset teh back buffer size if a resize of teh form occured
Set pBackBuffer = g_D3DDevice.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
pBackBuffer.GetDesc m_d3dsdBackBuffer

v.x = (((2 * x) / m_d3dsdBackBuffer.Width) - 1) / tempProj.m11
v.y = -(((2 * y) / m_d3dsdBackBuffer.Height) - 1) / tempProj.m22

v.z = 1#

''// Get the inverse view matrix
g_D3DDevice.GetTransform D3DTS_VIEW, tempview


Dim nd As D3DVECTOR
Dim fd As D3DVECTOR
Dim vp As D3DVIEWPORT8
g_D3DDevice.GetViewport vp
Call D3DXVec3Unproject(nd, vec3(v.x, v.y, 0), vp, tempProj, tempview, tempWorld)
Call D3DXVec3Unproject(fd, vec3(v.x, v.y, 1), vp, tempProj, tempview, tempWorld)


ok i get the following values for the unproject calls

?nd.x
-0.8297222
?nd.y
40.66496
?nd.z
-117.9404
?fd.x
-829.6364
?fd.y
704.8849
?fd.z
1939.384

now that i have these what do i do with them? how do i get my mesh object to the point that has been clicked on the screen from the above data.

thanks for all you help..
vLocalRayOrig = nd;
vLocalRayDir = fd - nd;

g_D3DX.Intersect gmesh(a).g_Mesh, vLocalRayOrig, vLocalRayDir, bHit, dwFace, fBary1, fBary2, fDist, fcount

... and you should have what you want.
I am having the same problem. I have a 3d game area and a mouse coord in 2d with 0,0 being the top left and 1023, 767 being the bottom right. I think my viewport perspective is PI/4 and I am having a heck of a time. For example, my camera always centers on my character (think diablo ish) and when I click on the screen I want to turn this coordinate into a place to move in the z axis. I have my camera at x=0, y= 750, z = -750 and never rotat it so it makes a 45 degree intersection with my z plane. I tried using some geometric functions but because of the projection plane they always come up wrong.

This topic is closed to new replies.

Advertisement