Skip to main content
GameDev.net gamedev.net
🔒 Locked

Camera targeting - a little problem

Started by Enjoy Jan 27, 2008 at 10:47 AM 0 replies 1.2k views
Original Post
Enjoy
Enjoy
Hi, I made a function, that makes a camera to look at a certain point. For rotation representation I am using quaternions. It works fine, but, when y coordinate of a view vector of a camera (vView.y) is positive, my camera flips upside down. Of course this happens only when I make camera look to a new direction. For example new target is (0.0f, 2.0f, 0.0f), and when camera (position) goes under a plane y = 2.0f, camera flips upside down. What my problem could be?

void CCamera::LookAt( vec3 pTarget )
{
	
	vTarget = pTarget;
	vTarget.x *= -1.0f; //I don't know, what do I need it for, but without it x coordinate flips.
	vView = vTarget - vPos;
	
	vec3 vViewProjected = vView;
	vViewProjected.y = 0.0f;
	
	vRight = vView * vViewProjected;
	vUp = vRight * vView;
	
	vView.normalize();
	vRight.normalize();
	vUp.normalize();

	camMatrix.values[0] = vRight.x;
	camMatrix.values[4] = vRight.y;
	camMatrix.values[8] = vRight.z;
	camMatrix.values[1] = vUp.x;
	camMatrix.values[5] = vUp.y;
	camMatrix.values[9] = vUp.z;
	camMatrix.values[2] = -vView.x; //Why is this reversed(x, y and z)? I don't understand, what do I need it for.
	camMatrix.values[6] = -vView.y;
	camMatrix.values[10] = -vView.z;
	qCurrent = CreateFromMatrix(camMatrix);
	
}

Thank you in advance.
ldeej
ldeej
It is going to flip because you do not have a reference frame, and without a reference frame for your camera you are basically making up your vectors on the fly (which can be done, I think Graphics Gems 2 has something on this).

The reason your camera is flipping is because in some cases the cross product of your right and forward vectors will create an up consistent with your world, other times it will create an up that actually points down.

My suggestions for this simple example is to use a global up vector, and compute your right vector from up and forward, and I would suggest you read more on reference frames to understand better how to program a more robust camera system.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.