Camera distortion using Directx Math

Started by
1 comment, last by Muzzy A 9 years, 4 months ago

I'm trying to set the camera eye position by the following code:


m_Camera.SetPosition(0.0f, 3.0f, -50);

where:
void cCamera::SetPosition(FLOAT x, FLOAT y, FLOAT z)
{
XMVECTOR position = XMVectorSet(x, y, z, 1.0f);
XMStoreFloat3(&m_Position, position);
}
void cCamera::UpdateViewMatrix()
{
/*m_Position.x = 0;
m_Position.y = 3;
m_Position.z = -50;*/

XMVECTOR eyePosition = XMLoadFloat3(&m_Position);
XMVECTOR direction = XMLoadFloat3(&m_Forward);
XMVECTOR upDirection = XMLoadFloat3(&m_Up);

XMMATRIX viewMatrix = XMMatrixLookToLH(eyePosition, direction, upDirection);
XMStoreFloat4x4(&m_ViewMatrix, viewMatrix);

}

The camera is distorted by the passed values to the function, but when I hard code the values as commented in the function UpdateViewMatrix, the distortion is removed and everything is viewed clearly.

Advertisement

The code you posted looks fine, so the problem is almost certainly elsewhere. Have you tried setting a breakpoint in UpdateViewMatrix and checked that m_position is actually what you expect? E.g., do you call SetPosition before UpdateViewMatrix? Have you verified (by actually examining values in debug mode) that m_position isn't changed elsewhere before UpdateViewMatrix is called?

N.B., if hard-coding the values you show results in the correct viewmatrix, then m_position has not been set to those values when UpdateViewMatrix is called.

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.

XMMatrixLookAtLH()

The second parameter is the "Focus Position" not the direction. So for that parameter you might want to add the direction vector to the position vector.


// This would be the correct way to do it
XMMatrixLookAt(  eyePosition, eyePosition + direction, upDirection );
 
 

EDIT:

I just realized you used "LookTo" not "LookAt". so my post probably isn't helpful.

is your forward vector valid when you get to the look at function?

This topic is closed to new replies.

Advertisement