# Camera Spherical Rotation with gluLookAt (SOLVED!)

This topic is 2983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to simulate the rotation of a camera in a sphere around any selected object of my screen or the (0,0,0). My input consists of movement units towards Y axis and towards X axis(as usual). I've tried using Matrices, the Spherical coordinates, or even two independent cyclical movements. The result I keep getting is: i)Correct rotation around Y axis(mouse movement towards X) ii)As soon as the mouse pointer passes 90 (or -90) degrees the model performs a rotation aroun its axis(Y axis relevant to its initial position) and reverses. I'm sure I'm missing something in rotation theory but I'm really stuck. Why is this happening and what can I do to bypass this and achieve my goal? Thank you. [Edited by - pontifikas on December 26, 2009 12:48:17 AM]

##### Share on other sites
You need to apply one rotation about the correct axis but you need to rotate the other around a modified axis.

Assuming y is up, x is right

If you want to look up you rotate about the x axis, then when you want to look from side to side you rotate about the y axis. I thinkt he problem is your second rotation rotates about an original axis.

Imagine if you have a vector pointing forward, you look 45 degrees to the right first, now if you look up/down (x axis rotation) then that forward vector will draw out a cone shape around the x axis instead of going straight up.

The solution to this is to also rotate the second axis your rotating about then do the rotation around it. Its been a while since I made my camera so not totally sure whats what but maybe this will help:

Quaternion r1, r2;		r1.CreateFromAxisAngle(m_Right.x, m_Right.y, m_Right.z, yDiff);		r2.CreateFromAxisAngle(0, 1, 0, xDiff);		m_Rotation = (r2*r1)*m_Rotation;

m_Rotation is the current camera rotation, yDiff is the angle up/down I want to look and xDiff is left/right

m_Right is the "right" vector of the camera (rather than just the x axis)

##### Share on other sites
I don't think that this is the case. You see the problem occurs even when no rotation around Y takes place. If I rotate only around X, as long as my angle is among -90 and 90 everything looks fine. As soon as I go beyond these values I get a reverse. See the following video

http://yfrog.us/0xvideo31561sz

##### Share on other sites
Ahh I see what you mean. Have you tried using quaternions? I beleive they will solve the problem.

I used to get the same when using matrices but with quaternions I can rotate all the way round without it flipping.

##### Share on other sites
Unfortunately use of Quaternions did not solve the problem.
Maybe it should be expected since I only perform rotations along two axis, so both matrix representation and Euler angles should produce no problems.

There must be something else I'm missing.

Help :(

##### Share on other sites
Hmm thats strange, I thought it was a problem with using matrix represenation. This is my camera "set angles by mouse" xDiff/yDiff are how many pixels the mouse move. Maybe it'll help althought its quite old.

void Camera::SetAnglesByMouse(float xDiff, float yDiff)	{		//if no movement return		if(!xDiff&&!yDiff)		{			return;		}		yDiff = yDiff*m_sensitivity;		if(m_inverted)		{			yDiff = -yDiff;		}				xDiff = -xDiff*m_sensitivity;		Quaternion r1, r2;		r1.CreateFromAxisAngle(m_Right.x, m_Right.y, m_Right.z, yDiff);		r2.CreateFromAxisAngle(0, 1, 0, xDiff);		m_Rotation = (r2*r1)*m_Rotation;		SyncAxis();		m_TransformNeedsUpdate = true;	}

m_Rotation is a quaternion storing the orientation of the camera. SyncAxis() just updates the 3 vectors forward/right/up

Could you post how your currently doing this? I would think maybe your not updating the up vector or something. What do you use for rendering?

##### Share on other sites
Blimey!!! It was the up vector after all!! I had to update it as well which I didnt. Everything is OK now. Thank you Nanoha!