#### Archived

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

# Anyone know about 2d coord->3d coord or Ray.?

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

## Recommended Posts

Hi I am feeling very pain to find a ray with 2d screen coord(x,y). Actually I want to implement picking object routine which is in D3DRM by myself in D3D IM. But my intention is stopped at first stage which is finding Ray(line from screen toward 3d-z direction) with screen x,y. Here is a my code. But it's not working correctly. when I call this function and draw Ray into 3d space, the apperance is quite different from my expectation. It's absolutely something wrong. Is there anyone to fix this code ?. //=============== Code for Ray picking ==================== void RayPick(LPDIRECT3DDEVICE7 pDev, float fovy, // fovy in projection float fNearZ, // distance to Near Clip plane float fFarZ, // distance to Far Clip plane int x, // 2D Screen Coord X int y, // 2D Screen Coord Y D3DVECTOR &p1, // Output:Start vector of Ray. D3DVECTOR &p2) // Output:End vector of Ray. { float fAspect, w, h; RECT rect; GetClientRect(m_hwndMain, &rect); w = (float)(rect.right-rect.left); h = (float)(rect.bottom-rect.top); fAspect = w/h; float dx,dy, t; D3DMATRIX invMatrix,viewMatrix; t = tanf( fFov * 0.5f ) dx = t * ( (float)x / (w * 0.5f) - 1.0f) / fAspect; dy = t * ( 1.0f- (float)y / (h * 0.5f) ); pDDev->GetTransform(D3DTRANSFORMSTATE_VIEW, &viewMatrix); D3DUtil_MatrixInvert(invMatrix,viewMatrix); p1 = D3DVECTOR( dx*fNearZ, dy*fNearZ, fNearZ ); p2 = D3DVECTOR( dx*fFarZ, dy*fFarZ, fFarZ ); // here, final results(p1,p2) is being returned.. D3DUtil_VectorMatrixMultiply( p1, p1, invMatrix); D3DUtil_VectorMatrixMultiply( p2, p2, invMatrix); } Edited by - willy9532 on 4/28/00 3:19:55 PM

##### Share on other sites
See the message from April 26th on this message board entitled:

"Direct3D Screen Coordinates / Projection"

I think this is what you are looking for.

Corey

##### Share on other sites
That message("Direct3D Screen Coordinates / Projection") is about converting 3d coord to 2d cood (3d->2d).

But what I want to know is 2D->3D.

That is 2d point to 3D line... actually..

Can anyone help me?

thanks

• 15
• 13
• 35
• 39