D3DXVec3Unproject Problems

Started by
29 comments, last by Kram 16 years ago
I have just made some debug output for some help:

Mouse X: 460
Mouse Y: 386
projectVector.z: 0.971619
Mouse X: 460
Mouse Y: 386
projectVector.z: 0.974242
Mouse X: 460
Mouse Y: 386
projectVector.z: 0.972081


Which gets outputted right after:

D3DXVECTOR3 projectVector;D3DXVec3Project(&projectVector, m_centerVector ,&viewport, &matProjection, &matView, &matWorld );screenPoint.x = m_mousePoint->x;screenPoint.y = m_mousePoint->y;screenPoint.z = projectVector.z;


as posted earlier.

So as you can see, the mouse doesn't move, but the D3DXVec3Project is creating a different Z value each time...
Advertisement
Usually, math functions are pretty constant. So, if the output is changing, the inputs must be changing between calls. Check the inputs to the Unproject and Project calls.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
Usually, math functions are pretty constant. So, if the output is changing, the inputs must be changing between calls. Check the inputs to the Unproject and Project calls.


Ok will do, I had a thought last night while in bed thinking about it, and should I be storing the screen space Z value on the object rather than calculating it each time the render call is being made?

Considering that I dont want to move the Z value at any time (except for when the user scrolls the mouse wheel), should I just calculate the Z-Offset when I initiate the object, and then re-calculate it only when it changes?
The only time you need to change the z-value is when (or if) any of the viewport, projection or view matrices change.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
The only time you need to change the z-value is when (or if) any of the viewport, projection or view matrices change.


Ok, well good news on that front, the only matrix that get affected by the m_zOffset member is D3DTS_WORLD.

So obviously, if the Z-index changes so frequently on mouse clicks, the world transformation that applies the translation on the x, y and z axis, will be affected.
I have done some debugging and have found out the there is only 1 input that is changing between mouse clicks, the D3DTS_WORLD transformation.

I have included another version of the SetupLocalMousePoints method, and the output from the method is below, and as you can see the _41 and _42 properties of the D3DTS_WORLD change...

Can someone please explain the issues here and how one would normally go about fixing this issue?

Thanks you very much

void GameEntity::SetupLocalMousePoints(IDirect3DDevice9* p_d3dDevice){	if (m_mousePoint)	{		D3DXVECTOR3 screenPoint;		D3DVIEWPORT9 viewport;		D3DXMATRIX matProjection;		D3DXMATRIX matView;		D3DXMATRIX matWorld;		D3DXVECTOR3 screenVector;		p_d3dDevice->GetViewport(&viewport);		p_d3dDevice->GetTransform( D3DTS_PROJECTION, &matProjection );		p_d3dDevice->GetTransform( D3DTS_VIEW, &matView );		p_d3dDevice->GetTransform( D3DTS_WORLD, &matWorld );		screenPoint.x = m_mousePoint->x;		screenPoint.y = m_mousePoint->y;		screenPoint.z = m_zOffset;				std::ostringstream outputStr;		outputStr << "SetupLocalMousePoints Debug****************************************\n";		outputStr << "Mouse X: " << m_mousePoint->x << "\n";		outputStr << "Mouse Y: " << m_mousePoint->y << "\n";		outputStr << "m_zOffset: " << m_zOffset << "\n";		outputStr << "\nviewport details\n";		outputStr << "Height: " << viewport.Height << "\n";		outputStr << "MaxZ: " << viewport.MaxZ << "\n";		outputStr << "MinZ: " << viewport.MinZ << "\n";		outputStr << "Width: " << viewport.Width << "\n";		outputStr << "x: " << viewport.X << "\n";		outputStr << "y: " << viewport.Y << "\n";		outputStr << "\nmatProjection details\n";		outputStr << "11: " << matProjection._11 << " 12: " << matProjection._12 << " 13: " << matProjection._13 <<" 14: " << matProjection._14 << "\n";		outputStr << "21: " << matProjection._21 << " 22: " << matProjection._22 << " 23: " << matProjection._23 <<" 24: " << matProjection._24 << "\n";		outputStr << "31: " << matProjection._31 << " 32: " << matProjection._32 << " 33: " << matProjection._33 <<" 34: " << matProjection._34 << "\n";		outputStr << "41: " << matProjection._41 << " 42: " << matProjection._42 << " 43: " << matProjection._43 <<" 44: " << matProjection._44 << "\n";		outputStr << "\nmatView details\n";		outputStr << "11: " << matView._11 << " 12: " << matView._12 << " 13: " << matView._13 <<" 14: " << matView._14 << "\n";		outputStr << "21: " << matView._21 << " 22: " << matView._22 << " 23: " << matView._23 <<" 24: " << matView._24 << "\n";		outputStr << "31: " << matView._31 << " 32: " << matView._32 << " 33: " << matView._33 <<" 34: " << matView._34 << "\n";		outputStr << "41: " << matView._41 << " 42: " << matView._42 << " 43: " << matView._43 <<" 44: " << matView._44 << "\n";		outputStr << "\nmatWorld details\n";		outputStr << "11: " << matWorld._11 << " 12: " << matWorld._12 << " 13: " << matWorld._13 <<" 14: " << matWorld._14 << "\n";		outputStr << "21: " << matWorld._21 << " 22: " << matWorld._22 << " 23: " << matWorld._23 <<" 24: " << matWorld._24 << "\n";		outputStr << "31: " << matWorld._31 << " 32: " << matWorld._32 << " 33: " << matWorld._33 <<" 34: " << matWorld._34 << "\n";		outputStr << "41: " << matWorld._41 << " 42: " << matWorld._42 << " 43: " << matWorld._43 <<" 44: " << matWorld._44 << "\n";				//transform the mouse clicked location		D3DXVec3Unproject( &screenVector, &screenPoint, &viewport, &matProjection, &matView, &matWorld );		m_xOffset = screenVector.x;		m_yOffset = screenVector.y;		outputStr << "screenVector.x: " << screenVector.x << "\n";		outputStr << "screenVector.y: " << screenVector.y << "\n";		OutputDebugStringA(outputStr.str().c_str());		m_mousePoint = 0;	}}


SetupLocalMousePoints Debug****************************************
Mouse X: 407
Mouse Y: 167
m_zOffset: 0.971619

viewport details
Height: 480
MaxZ: 1
MinZ: 0
Width: 640
x: 0
y: 0

matProjection details
11: 1.81066 12: 0 13: 0 14: 0
21: 0 22: 2.41421 23: 0 24: 0
31: 0 32: 0 33: 1.0101 34: 1
41: 0 42: 0 43: -1.0101 44: 0

matView details
11: -1 12: 0 13: 0 14: 0
21: 0 22: 0.952424 23: -0.304776 24: 0
31: -0 32: -0.304776 33: -0.952424 34: 0
41: -0 42: -4.76837e-007 43: 26.2488 44: 1

matWorld details
11: 1 12: 0 13: 0 14: 0
21: 0 22: 1 23: 0 24: 0
31: 0 32: 0 33: 1 34: 0
41: 0 42: 0 43: 0.971619 44: 1
screenVector.x: -3.94132
screenVector.y: 3.14972

SetupLocalMousePoints Debug****************************************
Mouse X: 407
Mouse Y: 167
m_zOffset: 0.971619

viewport details
Height: 480
MaxZ: 1
MinZ: 0
Width: 640
x: 0
y: 0

matProjection details
11: 1.81066 12: 0 13: 0 14: 0
21: 0 22: 2.41421 23: 0 24: 0
31: 0 32: 0 33: 1.0101 34: 1
41: 0 42: 0 43: -1.0101 44: 0

matView details
11: -1 12: 0 13: 0 14: 0
21: 0 22: 0.952424 23: -0.304776 24: 0
31: -0 32: -0.304776 33: -0.952424 34: 0
41: -0 42: -4.76837e-007 43: 26.2488 44: 1

matWorld details
11: 1 12: 0 13: 0 14: 0
21: 0 22: 1 23: 0 24: 0
31: 0 32: 0 33: 1 34: 0
41: -3.94132 42: 3.14972 43: 0.971619 44: 1
screenVector.x: -6.2582e-006
screenVector.y: 2.07303e-005
The world matrix defaults to an identity matrix when you start.

What you're seeing in the world transform appears to be the translation matrix from your last render.

Because you set the world matrix to translate an object, you need to do one of two things:

1. Reset the world matrix to an identity matrix each frame before you render anything.

- or -

2. After you set the world matrix to translate your cube and render it, restore the world matrix.

Something like:
dev->GetTransform(&oldWorld,D3DTS_WORLD);...set the world transform and render something...dev->SetTransform(&oldWorld,D3DTS_WORLD);

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
The world matrix defaults to an identity matrix when you start.

What you're seeing in the world transform appears to be the translation matrix from your last render.

Because you set the world matrix to translate an object, you need to do one of two things:

1. Reset the world matrix to an identity matrix each frame before you render anything.

- or -

2. After you set the world matrix to translate your cube and render it, restore the world matrix.

Something like:
dev->GetTransform(&oldWorld,D3DTS_WORLD);...set the world transform and render something...dev->SetTransform(&oldWorld,D3DTS_WORLD);


I see the issue, thanks for the help.

Is there a better way to do this per object in my 3D world?

You say, "Because you set the world matrix to translate an object", should I be using a different matrix here?

Thanks again!
Quote:You say, "Because you set the world matrix to translate an object", should I be using a different matrix here?

Sorry about the phrasing. I could as well have said "Because one set's the world matrix.."

No, setting the world matrix is the thing to do.

I (as a personal preference) consider saving and resetting the world matrix for each render of an object to be good programming practice.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
Sorry about the phrasing. I could as well have said "Because one set's the world matrix.."

No, setting the world matrix is the thing to do.

I (as a personal preference) consider saving and resetting the world matrix for each render of an object to be good programming practice.


Thats good news :) Thanks for the help, hopefully I can solve this issue tonight when I get home and finally put it to rest!

This topic is closed to new replies.

Advertisement