title: Trigonometry of a 3D "free" camera
body: <p>I'm sorry if this question was asked before, I did search, and I did not find an answer.</p>
  
  <p>My problem is, that I'd like to make movement on all 3 axes with the X and Y rotation of the camera being relevant.</p>
  
  <p>This is what I did:</p>
  
  <pre><code>private static void fly(int addX, int addY){ //parameters are the direction change relative to the current rotation
  
      float angleX = rotation.x + addX;   //angle is basically the direction, into which we will be moving(when moving forward this is always the same as our actual rotation, therefore addX and addY would be 0, 0)
      float angleY = rotation.y + addY;
  
  
      float speed = (moveSpeed * 0.0002f) * delta;
      float hypotenuse = speed; //the length that is SUPPOSED TO BE moved overall on all 3 axes
  
  
      /* Y-Z side*/
      //Hypotenuse, Adjacent and Opposite side lengths of a triangle on the Y-Z side
      //The point where the Hypotenuse and the Adjacent meet is where the player currently is.
      //OppYZ is the opposite of this triangle, which is the ammount that should be moved on the Y axis.
      //the Adjacent is not used, don't get confused by it. I just put it there, so it looks nicer.
      float HypYZ = speed;
      float AdjYZ = (float) (HypYZ * Math.cos(Math.toRadians(angleX))); //adjacent is on the Z axis
      float OppYZ = (float) (HypYZ * Math.sin(Math.toRadians(angleX))); //opposite is on the Y axis
  
      /* X-Z side*/
      //Side lengths of a triangle on the Y-Z side
      //The point where the Hypotenuse and the Adjacent meet is where the player currently is.
      float HypXZ = speed;
      float AdjXZ = (float) (HypXZ * Math.cos(Math.toRadians(angleY)));  //on X
      float OppXZ = (float) (HypXZ * Math.sin(Math.toRadians(angleY)));  //on Z
  
      position.x += AdjXZ;
      position.y += OppYZ;
      position.z += OppXZ;
  }
  </code></pre>
  
  <p>I only implement this method when moving forwards(parameters: 0, 90) or backwards(params: 180, 270), since movement can't happen on the Y axis while going sideways, since you don't rotate on the Z axis. ( the method for going sideways(strafing) works just fine, so I won't add that.)</p>
  
  <p><strong>the problem is that when I look 90 degrees up or -90 down and then move forward I should be moving only on the Y axis(vertically) but for some reason I also move forwards(which means on the Z axis, as the X axis is the strafing).</strong></p>
  
  <p><em>I do realize that movement speed this way is not constant. If you have a solution for that, I'd gladly accept it as well.</em></p>