Advertisement

Camera Distortion

Started by February 10, 2003 10:46 PM
0 comments, last by Bacardi34 22 years ago
I have a camera system for a RTS all setup, but the problem is when the camera is pitched up or down, and then rotated the view is skewed, like a fish eye lens. I have a feeling its because of the way i am keeping the camera level when rotating with a pitch, but i am unsure how to correct the situation. Here is the code for the pitch and rotation:
  
void CMyCamera::PitchDown( float fTime )
{
		// Vector for rotation in Rads

	float		frotation;
	D3DXMATRIX	matRotate;

	D3DXMatrixIdentity( &matRotate );
	frotation = m_fRotateSpeed * fTime;
	if( m_fPitch > ( D3DX_PI / 2 ) || m_fPitch < ( -D3DX_PI / 2 ) )
		return;
	D3DXMatrixRotationX( &matRotate, frotation );

	// Now multiply the matrices together

	D3DXMatrixMultiply( &m_matPosition, &matRotate, &m_matPosition );
}


void CMyCamera::PitchUp( float fTime )
{
	// Vector for rotation in Rads

	float		frotation;
	D3DXMATRIX	matRotate;

	D3DXMatrixIdentity( &matRotate );
	frotation = -m_fRotateSpeed * fTime;
	m_fPitch += frotation;
	if( m_fPitch > ( D3DX_PI / 2 ) || m_fPitch < ( -D3DX_PI / 2 ) )
		return;
	D3DXMatrixRotationX( &matRotate, frotation );

	// Now multiply the matrices together

	D3DXMatrixMultiply( &m_matPosition, &matRotate, &m_matPosition );
}
void CMyCamera::TurnLeft( float fTime )
{
	// Vector for rotation in Rads

	float		frotation;
	D3DXMATRIX	matRotate;

	D3DXMatrixIdentity( &matRotate );
	frotation = -m_fRotateSpeed * fTime;
	D3DXMatrixRotationY( &matRotate, frotation );

	// Now multiply the matrices together

	D3DXMatrixMultiply( &m_matPosition, &matRotate, &m_matPosition );
}


void CMyCamera::TurnRight( float fTime )
{
	// Vector for rotation in Rads

	float		frotation;
	D3DXMATRIX	matRotate;

	D3DXMatrixIdentity( &matRotate );
	frotation = m_fRotateSpeed * fTime;
	D3DXMatrixRotationY( &matRotate, frotation );

	// Now multiply the matrices together

	D3DXMatrixMultiply( &m_matPosition, &matRotate, &m_matPosition );
}


void CMyCamera::SetViewMat()
{
// Called after all camera movement has been completed.

	// Make sure the camera is always level on the x axis

	m_matPosition._12 = 0;
	D3DXMatrixInverse( &m_matView, NULL, &m_matPosition );
}

  
I know that calling m_matPosition._12 = 0; in SetViewMat is the problem, but it is the only way i can get the camera to stay level when rotating. I have tried setting matRotate._12 = 0; in the rotate functions after the rotation matrix has been created, but that does not keep the camera level. So really what i need is a suggestion on how to achieve that. Any ideas?
I''m not well versed in DirectX (usually use OpenGL), so I don''t know the exact matrix notation (e.g., how are the matrix components numbered---where is m._12, and are matrices column- or row-wise?). But I can say a few things.

You are correct about the cause of the problem being the m._12 = 0 in SetMatView. If it was not 0 to begin with, then what you''re doing is creating a non-orthogonal rotation portion of the view matrix. Basically, the "up" direction is not perpendicular to the view plane, for example. This leads to the distortion. To remove the distortion you need to make sure that view matrix remains orthogonal.

I would suggest you rethink what you want your camera to do. It seems like you''re trying to get it to do something that is a bit unnatural, which led you to "tweak" the math. If you "pitch" the camera from horizontal, its "Z" axis almost by definition will no longer be horizontal.

I would recommend that you look at Nate Robbins tutorial on camera projection. It shows OpenGL API calls, and their effect on the view. Its a VERY nice tutorial. Even though it is not DirectX, you can see exactly what the effect of changing the "up" and "look" directions do to the view. (In the demo, the camera looks at the "center" point and the look direction = "center" point - "eye" point.)

http://www.xmission.com/~nate/tutors.html

I would also suggest that you consider renaming your m_matPosition variable to something like m_matViewMatrix. Reason being the word "position" implies a point location in space, while in fact that matrix contains both a point location and an orientation/rotation. "ViewMatrix" implies both location and orientation.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

This topic is closed to new replies.

Advertisement