I know this question has been asked alot, but I haven't seen any answers that satisfy my needs.
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.
I'm currently writing a camera using Quaternions. I am attempting to turn my camera to a certain point, but my results are...strange.
I get my direction vector via
dir = viewPos-camPos;
dir.normalize();
I convert it to euler via this
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));
I then convert to quaternion via this
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);
And lastly to AxisAngle via this
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;
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)
Ideas?
EDIT 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;
EDIT 2 I've found out abit more, turns out what I thought to be Pitch and Roll is more Like... Pitch on that 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;