Sign in to follow this  

Using D3DXVec3Unproject??

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

Who is watching the other topic
http://www.gamedev.net/community/forums/topic.asp?topic_id=580765&PageSize=25&WhichPage=3

know I'm in trouble, to convert the mouse position on screen to the world

I tried as follows:
D3DXVECTOR3 mouseLocation;
D3DXVECTOR3 screenPoint;
D3DVIEWPORT9 viewport;
D3DXMATRIX matProjection;
D3DXMATRIX matView;
D3DXMATRIX matWorld;

ScreenToClient( wndHandle, &absCursor );
screenPoint.x = absCursor.x;
screenPoint.y = absCursor.y;
screenPoint.z = 0.0f;
//m_mousePoint = 0;

dxManager->getD3DDevice()->GetViewport(&viewport);
dxManager->getD3DDevice()->GetTransform( D3DTS_PROJECTION, &matProjection );
dxManager->getD3DDevice()->GetTransform( D3DTS_VIEW, &matView );
dxManager->getD3DDevice()->GetTransform( D3DTS_WORLD, &matWorld );

//transform the mouse clicked location
D3DXVec3Unproject( &mouseLocation, &screenPoint, &viewport, &matProjection, &matView, &matWorld );



in my HUD shows the following:
mouseLocation.X values were between -0.6 to 0.6
mouseLocation.Y values were between 3.4 to 2.6
mouseLocation.Z the fixed value of 8.0

these values is correct?
I do not use viewport in my project, I have to set some parameters for it? or this way is correct?

Thanks

Share this post


Link to post
Share on other sites
Do you have a pure device? If so, the GetTransform and GetViewport calls will fail and your matrices and viewport are uninitialized.


Note, you can't get a usable 3d point from one call to D3DXVec3Unproject. Every pixel on the screen is a 3d line. The common approach is to call D3DXVec3Unproject two times with differing z values.

You'll get two points of the aforementioned 3d line. You can use them to do intersection calculations.

Share this post


Link to post
Share on other sites
What do you mean by pure device?

I did as you said, my reasoning is correct?
// Get mouse coordinates and make them relative to the client area window.
// hWnd is a handler to the main window returned from the CreateWindow()
// function.
POINT p = {0, 0};
GetCursorPos(&p);
ScreenToClient(wndHandle, &p);

// Get matrices and the viewport.
D3DXMATRIX pm; // projection matrix
D3DXMATRIX vm; // view matrix
D3DXMATRIX wm; // world matrix
D3DVIEWPORT9 vp; // viewport
dxManager->getD3DDevice()->GetTransform(D3DTS_PROJECTION, &pm);
dxManager->getD3DDevice()->GetTransform(D3DTS_VIEW, &vm);
D3DXMatrixIdentity(&wm); // make the world matrix an identity matrix
dxManager->getD3DDevice()->GetViewport(&vp);

// Call the D3DXVec3Unproject function twice to determine two points
// in our world space. Having two points we will be able to find
// a line in the world space. One point is for the z-near plane
// (which is usually z=0.0f) and another point is for z-far plane
// (which is usually z=1.0f). We grab the z-planes from the viewport.
D3DXVECTOR3 p1;
D3DXVec3Unproject(&p1, &D3DXVECTOR3(p.x, p.y, vp.MinZ), &vp, &pm, &vm, &wm);
D3DXVECTOR3 p2;
D3DXVec3Unproject(&p2, &D3DXVECTOR3(p.x, p.y, vp.MaxZ), &vp, &pm, &vm, &wm);

// We calculate an intersection of the line found previously
// with a plane z=0.0f. Equations of a line in 3-D space can
// be found on the following websites:
// http://mathforum.org/dr.math/faq/formulas/faq.ag3.html#threelines
// http://mathforum.org/library/drmath/view/65721.html
// Here, wx, wy, and wz represent coordinates in the world space.
float wz = 0.0f;
float wx = ((wz-p1.z)*(p2.x-p1.x))/(p2.z-p1.z) + p1.x;
float wy = ((wz-p1.z)*(p2.y-p1.y))/(p2.z-p1.z) + p1.y;

// w is the point in the world space corresponding to the p point
// in the screen coordinates.
D3DXVECTOR3 w(wx, wy, wz);

Share this post


Link to post
Share on other sites
VitaliBR, take a look at my post in your "Move bones by code" thread. I worked out some code that should work for you perhaps with a little modification.

@Endurion: "you can't get a usable 3d point from one call to D3DXVec3Unproject"

You can get a usable 3d point. Just set the z-value of the projected point to the required depth in screen space (some value between 0 and 1) before the Unproject call.

[Edited by - Buckeye on September 1, 2010 7:27:26 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Buckeye
You can get a usable 3d point. Just set the z-value of the projected point to the required depth in screen space (some value between 0 and 1) before the Unproject call.


Yes, but you have no idea where in 3d space that point will show up; just that's it between the near and far plane. It's a valid point but it's pretty useless alone.

Share this post


Link to post
Share on other sites
Quote:
Endurion: you have no idea where in 3d space that point will show up

Not so. Given an object that's always a fixed distance from the camera, and having precalculated that object's screen depth, you can spot the mouse cursor in the plane of the object.

For instance, in a 3D "sidescroller" (which is what VitaliBR is working on), the player is often centered on the screen and the camera is a fixed distance away, so conversion of the mouse cursor as a point in the player's plane can be done just that way.

Share this post


Link to post
Share on other sites

This topic is 2664 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this