4

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;

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
ZZT
  • 71
  • 1
  • 6
  • Re Edit 2 - probably confusion about order in which angles are applied. E.g. [Wikipedia Euler Angles / Tait-Bryan / aircraft convention](https://en.wikipedia.org/wiki/Euler_angles): First Yaw, then Pitch, finally Roll. (If applying matrices, might have to do in reverse order: Roll/Pitch/Yaw.) Note that these are *intrinsic* rotations; each rotation is within the coordinate system created by the previous rotation. Consider an airplane flying level in +Y, with Z as up axis; yaw swivels airplane around Up axis, then pitch raises/drops nose, then roll is around the plane's current orientation. – ToolmakerSteve Feb 02 '18 at 00:45
  • The sentence *"converting a vector to a euler angle set ..."* makes me suspect that `UP` here is the *world's* Up Vector. If so, the linked formula for `angle_B` is used incorrectly; that formula requires knowing the *local* Up associated with the direction vector. If you don't have that, you can't calculate "Bank" (Roll). Or to put it another way, lacking "local Up", simply set (angle_B) Bank=0 as the least harmful value based on available info. For example, I use this when manipulating a camera + look-at (aim) point, and want it to "stay stable" so ground doesn't appear to roll. – ToolmakerSteve Feb 02 '18 at 02:12

0 Answers0