Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Let’s say we’re making a first-person camera. The basic idea is it should yaw to look left & right, and pitch to look up & down. So we write a bit of code like this (using Unity as an example):

  1. void Update() {
  2. float speed = lookSpeed * Time.deltaTime;
  3. // Yaw around the y axis using the player's horizontal input.
  4. transform.Rotate(0f, Input.GetAxis("Horizontal") * speed, 0f);
  5. // Pitch around the x axis using the player's vertical input.
  6. transform.Rotate(-Input.GetAxis("Vertical") * speed, 0f, 0f);
  7. }

or maybe

  1. // Construct a quaternion or a matrix representing incremental camera rotation.
  2. Quaternion rotation = Quaternion.Euler(
  3. -Input.GetAxis("Vertical") * speed,
  4. Input.GetAxis("Horizontal") * speed,
  5. 0);
  6. // Fold this change into the camera's current rotation.
  7. transform.rotation *= rotation;

And it mostly works, but over time the view starts to get crooked. The camera seems to be turning on its roll axis (z) even though we only told it to rotate on the x and y!

Animated example of a first-person camera tilting sideways

This can also happen if we’re trying to manipulate an object in front of the camera - say it’s a globe we want to turn to look around:

Animated example of a globe tilting sideways

The same problem - after a while the North pole starts to wander away to the left or right. We’re giving input on two axes but we’re getting this confusing rotation on a third. And it happens whether we apply all our rotations around the object’s local axes or the world’s global axes.

In many engines you’ll also see this in the inspector - rotate the object in the world, and suddenly numbers change on an axis we didn’t even touch!

Animated example showing manipulating an object next to a readout of its rotation angles. The z angle changes even though the user didn't manipulate that axis.

So, is this an engine bug? How do we tell the program we don’t want it adding extra rotation?

Does it have something to do with Euler angles? Should I use Quaternions or Rotation Matrices or Basis Vectors instead?

Answers: 15
Votes: 5

Created by -  

AndreFCruz Trusted
nodejs html5 css
Character count: 33

Etiam semper lacus eu dolor dictum, a dictum odio laoreet. Praesent luctus hendrerit dapibus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum dapibus leo id neque consequat placerat. Edited

Etiam semper lacus eu dolor dictum, a dictum odio laoreet. Praesent luctus hendrerit dapibus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum dapibus leo id neque consequat placerat. Edited

Etiam semper lacus eu dolor dictum, a dictum odio laoreet. Praesent luctus hendrerit dapibus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum dapibus leo id neque consequat placerat. Edited

Related Questions
Success card title

Some quick example text to build on the card title and make up the bulk of the card's content.