title: How to prevent floating point errors in camera vectors
body: <p>On a <a href="https://stackoverflow.com/questions/5287667/how-to-calculate-look-at-point-to-move-the-camera-with-the-mouse-in-opengl-glut">different question</a>, the user that answered (Ricky) me also said the following (which I still find a little confusing):</p>
  
  <blockquote>
    <p>When I say they must remain
    perpendicular, I only mean that if you
    rotate vectors independently by the
    same transformation over and over,
    floating point errors may creep in and
    they could diverge from being
    perpendicular. Dot(x,y), dot(y,z) and
    dot(x,z) should be very close to zero.
    If not, you need to do some cross
    products and re-perpendicularize them.
    It's as simple as overwriting
    z=cross(x,y), then y=cross(z,x).</p>
  </blockquote>
  
  <p>There are 3 relevant vectors on my camera system, these vectors are floating point numbers and always normalized. Reference vector points in the direction the camera is looking at, UpVector and RightVector are self-explanatory.</p>
  
  <p>Anyone knowing how to answer, should answer of course, but Ricky, if you're there, please help me out...</p>
  
  <p>1) What exactly does it mean dot(x,y), dot(y,z) and dot(x,z)? Is this the dot product between these vectors? I suppose... But which ones (x, y, z) correspond to my Reference, UpVector and RightVector? I'm a little confused...</p>
  
  <p>2) Then, these dot products should be very close to zero, how exactly should I check this? I've seen code like this to achieve the same thing in a similar context:</p>
  
  <pre><code>const float Math::EPSILON = 1e-6f;
  
  Math::closeEnough(&lt;floating_point_variable_to_check&gt;, 0.0f);
  
  static bool closeEnough(float f1, float f2) {
      // Determines whether the two floating-point values f1 and f2 are
      // close enough together that they can be considered equal.
  
      return fabsf((f1 - f2) / ((f2 == 0.0f) ? 1.0f : f2)) &lt; EPSILON;
  }
  </code></pre>
  
  <p>I suppose it's good enough?</p>
  
  <p>3) Where I exactly should I perform all these checks and re-perpendicularize the vectors? I have 3 functions that rotate the camera, Pitch, Yaw and Roll (the last one I don't use it that much), only these functions change those unit vectors. Should I perform the checks and fixes every time I call one of these rotate functions or would that be too much? Where and when then?</p>
  
  <p>4) Last but not least, to fix them I need to overwrite the vectors with the cross product, makes sense as I want them perpendicular to each other. But isn't the quote above missing one cross product? And does the order i which I perform those fixes matter?</p>