﻿ eulerToAxis / axisToEuler

### Author Topic: eulerToAxis / axisToEuler  (Read 26992 times)

Here is a nifty set of functions for use in converting between Euler and axis angles.

The basic usage for eulerToAxis() is this:

Code: [Select]
`%obj.setTransform(%position SPC eulerToAxis(%x SPC %y SPC %z));`
Note: Using eulerToMatrix() in the example above produces incorrect results.

The basic usage for axisToEuler() is this:

Code: [Select]
`%eulerRot = axisToEuler(getWords(%obj.getTransform(),3,6));`
Note: Do not use axisToEuler() with %obj.rotation, as it produces incorrect results.

Now for the code:

Code: [Select]
`function eulerToAxis(%euler){ %euler = VectorScale(%euler,\$pi / 180); %matrix = MatrixCreateFromEuler(%euler); return getWords(%matrix,3,6);}function axisToEuler(%axis){ %angleOver2 = getWord(%axis,3) * 0.5; %angleOver2 = -%angleOver2; %sinThetaOver2 = mSin(%angleOver2); %cosThetaOver2 = mCos(%angleOver2); %q0 = %cosThetaOver2; %q1 = getWord(%axis,0) * %sinThetaOver2; %q2 = getWord(%axis,1) * %sinThetaOver2; %q3 = getWord(%axis,2) * %sinThetaOver2; %q0q0 = %q0 * %q0; %q1q2 = %q1 * %q2; %q0q3 = %q0 * %q3; %q1q3 = %q1 * %q3; %q0q2 = %q0 * %q2; %q2q2 = %q2 * %q2; %q2q3 = %q2 * %q3; %q0q1 = %q0 * %q1; %q3q3 = %q3 * %q3; %m13 = 2.0 * (%q1q3 - %q0q2); %m21 = 2.0 * (%q1q2 - %q0q3); %m22 = 2.0 * %q0q0 - 1.0 + 2.0 * %q2q2; %m23 = 2.0 * (%q2q3 + %q0q1); %m33 = 2.0 * %q0q0 - 1.0 + 2.0 * %q3q3; return mRadToDeg(mAsin(%m23)) SPC mRadToDeg(mAtan(-%m13, %m33)) SPC mRadToDeg(mAtan(-%m21, %m22));}`
« Last Edit: October 28, 2015, 12:04:38 PM by Badspot »

#### Marcem

Is this used for rotation based on angles and stuff?

Rotation in Blockland (Torque) uses axis angles instead of 0 to 360-degree Euler angles for rotation.

If you want to rotate an object 90 degrees on the Z axis, you'd use this:

Code: [Select]
`%obj.setTransform(%position SPC eulerToAxis("0 0 90");`
Now, if you have an object and you want to find out what it's rotation is in "normal" (Euler) degrees, you'd use this:

Code: [Select]
`%eulerRot = axisToEuler(getWords(%obj.getTransform(),3,6));`
So, these two functions are very useful when dealing with physical objects in Blockland (Torque).
« Last Edit: December 28, 2007, 07:23:14 PM by Trader »

#### Marcem

Thats awesome. I may need to use this.

#### Randy

Thanks, I've been looking for quatToEuler for a while.

Me as well.  No problem.
« Last Edit: December 28, 2007, 07:23:55 PM by Trader »

#### exidyne

Same thing, smushed to three lines:

Code: [Select]
`function mEulerToQuat(%euler) {   %euler = VectorScale(%euler, 0.017453);   %matrix = MatrixCreateFromEuler(%euler);   return getWords(%matrix, 3, 6);}`

Yeah, I'm not taking credit for writing these functions.  The first is an edit of a function posted by Badspot on the GarageGames forums, the second is an edit of a function posted by another GarageGames forum member.  Anyway, change made and I took off a couple lines on the second function as well.
« Last Edit: December 28, 2007, 07:24:24 PM by Trader »

Not too long ago at a bar during IGC, the topic of quaternions came up and I was informed by my enebriated peers that what I was explaining as a quaternion was actually an Axis Angle and a quaternion was something entirely more complex like a double ended 4 dimensional cone with a spherical base (you can see this by how scary the quaternion wiki page looks compared to the axis angle page).

Ah, I see.  I changed my posts above to reflect your correction.  Thanks for the sticky.

#### Loopyla1

I cant understand a WORD of coding. I shouldn't be here.

#### BobAndRob

Useful on many occasions, thanks Trader.
« Last Edit: August 27, 2009, 07:04:13 PM by BobAndRob »

#### Loopyla1

loopyla1, if you think you shouldn't be here, than DO NOT COME to post that you should not be posting, i know  you are new, but posting random _ to get your post count up is not the way to have people like you, it is a way to get yourself banned and annoy people.
Go away.

Not too long ago at a bar during IGC, the topic of quaternions came up and I was informed by my enebriated peers that what I was explaining as a quaternion was actually an Axis Angle and a quaternion was something entirely more complex like a double ended 4 dimensional cone with a spherical base (you can see this by how scary the quaternion wiki page looks compared to the axis angle page).