title: Convert yaw, pitch AND roll to x,y,z vector in world coordinates
body: <p>I'm working on some simple 3d graphics in OpenGL (java LWGJL), and I'm trying to figure out how to convert yaw, pitch and roll to the x, y and z components of my movement Vector. I know how to do this with just pitch and yaw (as explained <a href="https://stackoverflow.com/a/1568687/1530508">here</a>), but I haven't found anything the explains how to integrate roll into this formula. </p>
  
  <p>I am aware that yaw and pitch are all that is needed to define a vector in 3d space, but I also need roll in this instance. I have keys bound to different movements relative to the camera in a basic WASD configuration (<kbd>A</kbd> is local left, <kbd>W</kbd> is local forward, <kbd>SPACE</kbd> is local up), so the roll affects how the camera moves (eg pressing <kbd>D</kbd> with a roll of pi/2 (the default) moves the camera right (in world coords), but pressing <kbd>D</kbd> with a roll of pi moves the camera up in world coords)).</p>
  
  <p>Here's the code I have so far:</p>
  
  <pre class="lang-java prettyprint-override"><code>//b = back
  //f = forward
  //r = right
  //l = left
  //u = up
  //d = down
  
      private void move()
      {
          double dX = 0, dY = 0, dZ = 0;
  
  
          if (f ^ b)
          {
              dZ += cos(yaw) * cos(pitch) * (b ? 1 : -1);
              dX += sin(yaw) * cos(pitch) * (b ? 1 : -1);
              dY += -sin(pitch) * (b ? 1 : -1);
          }
  
          if (l ^ r)
          {
              dZ += sin(yaw) * sin(roll) * (l ? 1 : -1);
              dX += cos(yaw) * - sin(roll) * (l ? 1 : -1);
              dY += cos(roll) * (l ? 1 : -1);
          }
  
          if (u ^ d) //this part is particularly screwed up
          {
              dZ += sin(pitch) * sin(roll) * (u ? 1 : -1);
              dX += cos(roll) * (u ? 1 : -1);
              dY += cos(pitch) * sin(roll) * (u ? 1 : -1);
          }
  
  
  
          motion.x = (float) dX;
          motion.y = (float) dY;
          motion.z = (float) dZ;
  
          if (motion.length() != 0)
          {
              motion.normalise();
              motion.scale(2);
          }
  
          x += motion.x;
          y += motion.y;
          z += motion.z;
  </code></pre>
  
  <p>This works for a few rotations, but for many it produces incorrect results.</p>
  
  <p>So the question is:</p>
  
  <p><strong>How do I modify my code such that it successfully calculates the x, y, and z components of my motion vector based upon my desired direction (what key is pressed), accounting for my yaw, pitch, <em>AND</em> roll?</strong></p>
  
  <p>I'm fine with using raw trig (as I am attempting to do), a solution involving matrices, or pretty much anything.</p>
  
  <p>EDIT:</p>
  
  <p>Please don't answer by just linking to the Wikipedia article on Euler Angles. I've read it and I don't have a strong enough background in math to understand how to apply it to my situation.</p>
  
  <p>EDIT #2:</p>
  
  <p>I'm only using Euler angles to store my orientation in between re-orienting the camera. For the actual camera manipulations, I use rotational matrices. If needed, I can drop the euler angles and just use a matrix. All that matters is that I can convert from my orientation to a vector.</p>
  
  <p>EDIT #3:</p>
  
  <p>Found a solution by multiplying my forward vector by my rotation matrix as described in the 
  comments:</p>
  
  <pre class="lang-java prettyprint-override"><code>//b = back
  //f = forward
  //r = right
  //l = left
  //u = up
  //d = down
  
  private Vector3f motion;
  
  protected void calcMotion()
  {
      //1 for positive motion along the axis, -1 for negative motion, 0 for no motion
      motion.x = r&amp;&amp;!l ? -1 : l ? 1 : 0; 
      motion.y = u&amp;&amp;!d ? 1 : d ? -1 : 0;
      motion.z = f&amp;&amp;!b ? 1 : b ? -1 : 0;
  
      if (motion.length() == 0)
      {
          return;         
      }
  
      motion.normalise();
  
      //transform.getRotation() returns a Matrix3f containing the current orientation
      Matrix3f.transform(transform.getRotation(), motion, motion);
  }
  </code></pre>
  
  <p>Still having trouble with <a href="https://stackoverflow.com/q/18995844/1530508">this</a>, though.</p>