Jump to content
  • Advertisement
Sign in to follow this  
swiv

D3DXVec3Unproject

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

Hi there, I really hope somebody can help me as I'm stumped! Basically, I am writing a little editor program where the user can click on a grid to change tiles. It is in 3D so I need to determine where the user has clicked on the plane that that the grid lies in. So, I wrote the following function:


void getIntersectPointPlane(D3DXVECTOR3* intersectPoint, int mouseX, int mouseY, D3DXPLANE* plane)
{
	D3DXMATRIX  matWorld,  matView, matProj;
	D3DXVECTOR3 Result1, Result2;
	D3DVIEWPORT9 viewPort;

	D3DXMatrixIdentity(&matWorld);
	d3dDevice->GetTransform(D3DTS_VIEW, &matView); 
	d3dDevice->GetViewport(&viewPort); 
	d3dDevice->GetTransform(D3DTS_PROJECTION, &matProj); 

	D3DXVec3Unproject(&Result1, &D3DXVECTOR3((float)mouseX, (float)mouseY, 0.0f), &viewPort, &matProj, &matView, &matWorld);
	D3DXVec3Unproject(&Result2, &D3DXVECTOR3((float)mouseX, (float)mouseY, 1.0f), &viewPort, &matProj, &matView, &matWorld);

	D3DXPlaneIntersectLine(intersectPoint, plane ,&Result1, &Result2);
}


Basically, it's supposed to unproject the mouse coord at the camera position and at the far plane. This gives me a line which I can then use to intersect with the plane the grid lies on. This all works, but the line is slightly off, and seems more off at certain view angles. I'm actually drawing the line and it is definately not accuratly under the mouse. The result of this is that when using my editor, sometimes the wrong grid cell is selected! Something that obviously renders the editor useless! Does anybody have any idea what i may be doing wrong? If I can't figure this out the computer is going out the window! Cheers for any help!

Share this post


Link to post
Share on other sites
Advertisement
I looked over your code and didn't spot anything obviously wrong. You mention that the first point is unprojected at the camera position, but I think actually it's the near plane. Also, if you're drawing the picking line segment, if things are working correctly it should either be just a dot under the mouse or not visible at all.

Are you sure your mouse coordinates and input plane are correct? There may be an obvious mistake there that I'm missing, but your code looks right to me.

Share this post


Link to post
Share on other sites
Hi there swiv,
How are you doing?

The Problem
Near and Far vectors.

The Solution
Just make sure that the other variables such as your viewport and matrices are correct, also make sure your mouse coordinates are in screen space. Test this by drawing the coordinates to screen and check if the top left hand corner is 0.0, 0.0

EDIT: removed direction vector
It's the only thing I can think of that might be wrong.

I hope this helps a bit.

[Edited by - Armadon on September 11, 2005 11:57:11 PM]

Share this post


Link to post
Share on other sites
Quote:
D3DXVECTOR3 vecDirection = Result2 - Result1;
D3DXPlaneIntersectLine(intersectPoint, plane ,&vecDirection, &Result2);
D3DXPlaneIntersectLine() takes as arguments the start and end points of the line (segment), not a direction vector. So I don't think that's where the problem is.

Share this post


Link to post
Share on other sites
Right Jyk,I do apologize, I woke up at 5am and just started typing.. I should fix that post :) thanks for pointing that out :)
PS: grr.. to D3DXIntersect :)

Share this post


Link to post
Share on other sites
Hi swiv.
I'm not a DirectX Guru, thus I don't know if I can be of any help, but I've had a similar issue.

Do you run fullscreen or windowed mode ?
I have no offset in fullscreen but it appears in windowed mode, I guess this is a problem with the size of the window borders not being counted...

well, you may take a closer look at Armadon recommendations about Mouse position and Screen space. (Mine is in Window space and I can't find the courage to fix it).

Share this post


Link to post
Share on other sites
Hi Guys!

Thanks for all the help, it is most appreciated. I found that it worked in full screen mode which lead me to beleive the mouse coords were wrong, and they were! This is because the actual width and height of my window was wrong because of the titlebar etc.

The following code accommodates for that:


if (bWindowed)
{
DWORD frameWidth = GetSystemMetrics(SM_CXSIZEFRAME);
DWORD frameHeight = GetSystemMetrics(SM_CYSIZEFRAME);
DWORD captionHeight = GetSystemMetrics(SM_CYCAPTION);

iWidth = iWidth + (frameWidth*2);
iHeight = iHeight + (frameHeight*2) + captionHeight;
}



Now it works in perfectly in fullscreen and windowed mode.

Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!