# Convert from D3DTS_VIEW to Position-Vector?

## Recommended Posts

Hello, after a while reading in this forum I decided to register an account and this is my first question! I have a little Cameraclass, where I can move forward and backward (z) and rotate about y ("left and right"), so I can reach every point in my world on the x/z-Axis. When I start at (0,0,0), how can I check my actual position-vector in my world? Is there a helper-function from DirectX, where I can set my Viewmatrix as a parameter and get back my position-vector? I would be glad if someone could help me. Thank you!

##### Share on other sites
sirob    1181
You can use D3DXMatrixDecompose, but for a View matrix you'd need to negeate the result.

You can also grab the position from the matrix yourself, like so:
m._41 = -x;m._42 = -y;m._43 = -z;

(negated because this is a View matrix).

Hope this helps.

##### Share on other sites
Thanks for the answer, sadly only the z-value changes. I post some code, it would be nice, if you or someone else could make a quick view on it and tell me possible errors:

void CCamera::MoveCamera(float x, float y,float z){// only the z-value changes, x and y are always 0.0f	D3DXMatrixTranslation(&m_matMove, x, y, z);}void CCamera::SetRotateY(float yPos){	m_fyRot -= yPos;	D3DXMatrixRotationY(&m_matDrehY, m_fyRot);	return;}void CCamera::Move(){		m_matResult = m_matDrehY * m_matDrehX * m_matMove; 	m_pgraphic->GetDeviceObj()->SetTransform(D3DTS_VIEW, &m_matResult);}void CCamera::GetCameraPos(float &x, float &y, float &z){	D3DXMATRIX matView;	m_pgraphic->GetDeviceObj()->GetTransform(D3DTS_VIEW, &matView);	x = matView._41; // always 0.0f	y = matView._42; // always 0.0f	z = matView._43;}

In my "MoveCamera"-function only the z-values are changed by me, x and y are always 0. But when I rotate about the y-Axis and then go back- or forward, the x- and z-values are changed in real and I want to know this position.

I hope it's understandable what I want, my english is not so perfect.

Thank you!

##### Share on other sites
BornToCode    1185
What Sirob tells you to do is correct. Because the ViewMatrix is teh Inverse of the World Matrix and since you need the position of the camera withing the ViewMatrix then you have to negate the last row.
Example.

D3DXVECTOR3 getPositionFromViewMatrix(D3DXMATRIXA16 ViewMatrix)
{
return D3DXVECTOR3(-ViewMatrix._41,-ViewMatrix._42,-ViewMatrix._43);
}

##### Share on other sites
I understand, but when I call "GetCameraPos" the x (and y)-values stay at a value of 0.0.

I think the rotation remains unconsidered.

At programstart I stand in the middle of a box on 0,0,0 and view forward.

Then I rotate for example 45 degrees to the left, so I view to the left forward corner and then go a little bit forward. When I call "GetCameraPos" now, I expect to get values like (-2,0,2). But I get (0,0,2).

##### Share on other sites
I found a solution that works for me. The problem was the unconsidered y-rotation.

For those, who find this thread via search-function and want a solution for this problem too, here is the line you have to add right after sirobs code in the 2nd post:

x = -(matView._31 *z);

Thanks to sirob for giving me the right impulse (+)!