Generic Quaternion Camera Class

Started by
1 comment, last by Zakwayda 16 years, 8 months ago
Guys, I'm pulling out my hair cuase this should be easy, but I can't figure out why in the world my camera class is not working, here's the code for the rotation:

void Camera::Rotate(float angle,float x,float y,float z)
{
	Quaternionf temp, quat_view, result;

	temp.x = x * sin(angle/2);
	temp.y = y * sin(angle/2);
	temp.z = z * sin(angle/2);
	temp.w = cos(angle/2);

	quat_view.x = m_LookAt.X();
	quat_view.y = m_LookAt.Y();
	quat_view.z = m_LookAt.Z();
	quat_view.w = 0;

	Quaternionf preConj = temp * quat_view;
	temp.Conjugate();

	result = preConj * temp;

	m_LookAt.X( result.x );
	m_LookAt.Y( result.y );
	m_LookAt.Z( result.z );
}


Here's how my game initializes the camera:

	m_Camera.Position(50.0f,600.0f,-300.0f);
	m_Camera.LookAt(50.0f,0.0f,0.0f);
	m_Camera.Up(0.0f,1.0f,0.0f);
	m_Camera.Far(3000.0f);
	m_Camera.Near(1.0f);
	m_Camera.Aspect(3.14f/4.0f);
	m_Camera.FOV(800.0f/600.0f);


And lastly, I'm just trying to get the rotation working, so during update I'm calling this;

m_Camera.Rotate(0.1f,0.0f,1.0f,0.0f);

It just jiggles the camera, but no real movement??? Any ideas??? Thanks in advance Jeff.
Advertisement
Are you sure the quaternion multiplication and conjugation functions are implemented correctly, and that the Quaternionf.Conjugate() method alters the quaternion in place instead of returning a value?
Is m_lookAt the target point for the camera (as the code suggests), or is it a view direction? If it's the former, then rotating it as you're doing is almost certainly not going to give you the results you're looking for.

Also, my usual mini-rant: The use of quaternions here is not only pointless, but actually serves to obfuscate what would otherwise be a fairly straightforward operation (in fact, eliminating the quaternions would be a good first step toward figuring out what the real problem is). Furthermore, the whole 'rotate the look-at point/vector' approach to camera control is equally pointless and obfuscated (IMO, at least). Whatever camera control scheme you're after (FPS, 6DOF, orbital, etc.), there is almost certainly a better way to go about it.

This topic is closed to new replies.

Advertisement