# Camera rolling

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

## Recommended Posts

I have a camera floating in 3D space. I'm trying to implement a relatively simple system where the user moves the mouse and the camera rotates in that direction, and they can push the keyboard to move the camera's position, so allowing free view of whatever's in said 3D space.

Now I've got a small problem. The user can obviously only move the mouse in two dimensions- x and y, and the code only rotates around the x and y axis. To keep the rotations relative I also rotate all three axis by every rotation.

This works fine in the short term- the camera moves exactly as expected. However, once the user starts to rotate the camera often, the camera starts to slowly roll. You can move the mouse in a circle and exaggerate the effect to produce purely rolling. I wish to eliminate this issue. I've used quaternions to represent each rotation in a sequence. I attach some hopefully easy C++ code in case I have made a simple mistake.

 Math::AbsolutePoint previous; previous.x = 0; previous.y = 0; Math::Vector CameraXAxis = Math::Vector(1, 0, 0); Math::Vector CameraYAxis = Math::Vector(0, 1, 0); Math::Vector CameraZAxis = Math::Vector(0, 0, 1); auto RotateAxis = [&](Math::Quaternion q) { CameraXAxis = CameraXAxis * q; CameraYAxis = CameraYAxis * q; CameraZAxis = CameraZAxis * q; Scene3D->camera.LookingAt = CameraZAxis; Scene3D->camera.Up = CameraYAxis; }; window->OnKeyDown = [&](Wide::OS::Key k) { switch(k) { case OS::Key::DownArrow: Scene3D->camera.Position -= CameraYAxis; return; case OS::Key::UpArrow: Scene3D->camera.Position += CameraYAxis; return; case OS::Key::LeftArrow: Scene3D->camera.Position -= CameraXAxis; return; case OS::Key::RightArrow: Scene3D->camera.Position += CameraXAxis; return; } }; window->OnMouseScroll = [&](int scroll, Math::AbsolutePoint position) { if (scroll > 0) Scene3D->camera.Position.z -= 1; if (scroll < 0) Scene3D->camera.Position.z += 1; }; window->OnMouseMove = [&](Math::AbsolutePoint position) { if (previous.x == 0) { previous = position; return; } Math::Quaternion addrotation; if (previous.x > position.x) { addrotation = Math::RotateAxis(3, CameraYAxis); } else if (previous.x < position.x) { addrotation = Math::RotateAxis(-3, CameraYAxis); } if (previous.y > position.y) { addrotation = Math::RotateAxis(3, CameraXAxis); } else if (previous.y < position.y) { addrotation = Math::RotateAxis(-3, CameraXAxis); } RotateAxis(addrotation); previous = position; }; 

Any suggestions?

##### Share on other sites
I don't see any orthonormalization in your code. I'm suspecting floating point deviation is skewing your axes, try breaking into the code when you see the problem in the debugger, and check to see if your camera's local axii are orthonormal to each other.

##### Share on other sites
Also, you typically want to rotate around WORLD Y axis rather than camera.

##### Share on other sites
Thanks for the tips guys. I decided to shelve this camera style for now but will be revisiting it in the future.