# Camera Rotation (Y rotation)

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

## Recommended Posts

Hey, Quick question, I have my direction vector. I can get my up/down rotation working fine by just taking the cross between my direction vector and my up vector (up is 0,1,0). In order to get left/right rotating working, I don't need my world up vector, I need my camera space up vector. Any idea how I can calculate with only my direction vector known? I can obtain my right vector, but does that help? Thanks! Jeff.

##### Share on other sites
Once you have the forward (direction) vector and right vector, cross those to find the final camera up vector.

##### Share on other sites
agi_shi,

See, the problem is, I use a cross product of my world space up (0,1,0) and my direction vector to get the right vector. So if I do a cross of my Dir & Right, I get my world up (0,1,0). I don't want my world up, I want the perpendicular vector to where I'm looking.

Any other ideas?
Thanks!
Jeff.

##### Share on other sites
Quote:
 So if I do a cross of my Dir & Right, I get my world up (0,1,0). I don't want my world up, I want the perpendicular vector to where I'm looking.
If you take the cross product of your view (direction) and 'right' vectors, you'll get a vector perpendicular to where you're looking, not the world 'up' vector (try it and see!).

##### Share on other sites
take your camera look at vector cross with the world up to get your right vector. Then use your camera look at and cross with you newly found right vector to get your camera up vector. This newly up vector is not necessary the same as the world up.

##### Share on other sites
OK,

I did that, and you guys are correct. I take the cross of my camera LookAt and my newly found right vector, I get the camera's up vector. Great! HOWEVER!...

when I do a rotation using the newly created up vector, it still appears my camera is rotating about (0,1,0). I can see this because when I look almost down, I see the object spin around in a circle... just as if you were to look down at the floor and spin around the (0,1,0) axis (try it, though you might get dizzy). I am trying to simulate a free cam. Hmmm, here's my code, do you guys see any wrong logic?

My Movement Function:
/*----------------------------------------------------------------------------    ROTATE    Rotates using a delta x/y.  Good for mouse controls.*/void Camera::Rotate(float deltaX, float deltaY)    {    Vector3f axis, dir, camUp;    // get my direction vector    dir = m_LookAt - m_Pos;    // get my right vector    axis = CrossProduct(dir,m_Up);    Vec3fNormalize(axis);    // rotate y degrees about my right axis    Rotate(deltaY, axis);    // get the new camera up    camUp = CrossProduct(dir,axis);    Vec3fNormalize(camUp);    // rotate x degrees about my new camera up    Rotate(deltaX, camUp);    }

Here's my generic Rotate Function:
/*----------------------------------------------------------------------------    ROTATE    Rotates the angle about an axis.*/void Camera::Rotate(float angle, Vector3f &axis)    {    Quaternionf qRotation, qView, qNewView;    qRotation.RotationAxisToQuaternion(angle, axis);    qView.x = m_LookAt._x - m_Pos._x;    qView.y = m_LookAt._y - m_Pos._y;    qView.z = m_LookAt._z - m_Pos._z;    qView.w = 0;    qNewView = ((qRotation * qView) * qRotation.Conjugate());    m_LookAt._x = m_Pos._x + qNewView.x;    m_LookAt._y = m_Pos._y + qNewView.y;    m_LookAt._z = m_Pos._z + qNewView.z;    Vector3f right = CrossProduct(m_Up,(m_LookAt - m_Pos));    Vec3fNormalize(right);    m_Right._x = right._x;    m_Right._y = right._y;    m_Right._z = right._z;    }

Any thoughts? It should be working, but I can't for the life of me figure out why it keeps rotating about (0,1,0) or appears to be doing that. No matter where I'm looking, it should rotate left and right, not about the world up axis.

Thanks
Jeff.

##### Share on other sites
It would help to see how you actually use the rotations. Isolated wrappers aren't telling us if you simply apply your transformations "wrong". All this work will be useless if you just collect angles somewhere and then apply a bunch of rotations "from scratch". Especially if the order is "wrong" on top of it.