title: Directional Vector to Euler/Otherwise
body: <p>I know this question has been asked alot, but I haven't seen any answers that satisfy my needs.</p>
  
  <p>My problem rests in the fact i've gotten very used to developing in an env where converting a vector to a euler angle set was simple Vec.ToAngle, and now I need to replicate that.</p>
  
  <p>I'm currently writing a camera using Quaternions.
  I am attempting to turn my camera to a certain point, but my results are...strange.</p>
  
  <p>I get my direction vector via</p>
  
  <pre><code>dir = viewPos-camPos;
  dir.normalize();
  </code></pre>
  
  <p>I convert it to euler via <a href="https://stackoverflow.com/questions/21622956/how-to-convert-direction-vector-to-euler-angles?answertab=votes#tab-top">this</a></p>
  
  <pre><code>float angle_H = (float)Math.atan2(dir.y,dir.x);
  float angle_P = (float)Math.asin(dir.z);
  
  Vector3f W0 = new Vector3f(-dir.y,dir.x,0);
  Vector3f U0 = mul(W0,dir);
  
  float angle_B = (float)Math.atan2(Vector3f.dot(W0,UP),Vector3f.dot(U0,UP));
  </code></pre>
  
  <p>I then convert to quaternion via <a href="http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm" rel="nofollow noreferrer">this</a></p>
  
  <pre><code>double c1 = Math.cos(angle_H/2);
  double c2 = Math.cos(angle_P/2);
  double c3 = Math.cos(angle_B/2);
  double s1 = Math.sin(angle_H/2);
  double s2 = Math.sin(angle_P/2);
  double s3 = Math.sin(angle_B/2);
  
  viewQuat.w = (float)(c1*c2*c3 - s1*s2*s3);
  viewQuat.x = (float)(s1*s2*c3 + c1*c2*s3);
  viewQuat.y = (float)(s1*c2*c3 + c1*s2*s3);
  viewQuat.z = (float)(c1*s2*c3 - s1*c2*s3);
  </code></pre>
  
  <p>And lastly to AxisAngle via <a href="http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm" rel="nofollow noreferrer">this</a></p>
  
  <pre><code>float w = viewQuat.w;
  float w2 = (float)Math.sqrt(1-w*w);
  
  float angle = (float)(2f*Math.acos(w));
  float x = viewQuat.x/w2;
  float y = viewQuat.y/w2;
  float z = viewQuat.z/w2;
  </code></pre>
  
  <p>The reason I'm flip flopping around between angle-formats is mainly because:
  I wan't to use quaternions in the background
  Euler is a physically tangible angle-format
  Axis-Angle is required for lwjgl (OpenGL for java)</p>
  
  <p>Ideas?</p>
  
  <p><b><em>EDIT</em></b>
  Turns out my math was correct, however the angles did need tweaking.
  Pitch, Yaw, and Roll where all mixed up, I think it was Pitch was Roll, and Roll was Yaw, in addition to having to Pitch = -Pitch+90; and Roll = -Roll-90;</p>
  
  <p><b>EDIT 2</b>
  I've found out abit more, turns out what I thought to be Pitch and Roll is more Like... Pitch on <b>that</b> axis.
  My "Pitch" controls rotation on the Y-Z axis, and my "Roll" controls the X-Z axis.
  So if i look Right to +Y Pitch and roll work great, If I look right to +X the rolls switch linearly as I approach looking to +X;</p>