Author Topic: Vector Rotation  (Read 603 times)

How would I go about rotating for example the vector 1 0 -1 95 by 0 0 1 45?
I tried making my own function, but that doesn't work properly at all:
Code: [Select]
function rotateVector( %a, %b )
{
%alh = getWords( %a, 0, 2 );
%bet = getWords( %b, 0, 2 );
%gam = vectorAdd( %alh, %bet );

%ga = getWord( %gam, 0 );
%gb = getWord( %gam, 1 );
%gc = getWord( %gam, 2 );

if ( %ga > 1 ) %ga -= 2;
if ( %gb > 1 ) %gb -= 2;
if ( %gc > 1 ) %gc -= 2;

if ( %ga < 1 ) %ga += 2;
if ( %gb < 1 ) %gb += 2;
if ( %gc < 1 ) %gc += 2;

return %ga SPC %gb SPC %gc SPC getWord( %a, 0, 3 );
}
« Last Edit: December 11, 2011, 12:05:54 AM by Port »

Do you need it in axis form? You can do it with euler angles like so:
Code: [Select]
function rotateVector(%a,%b)
{
%pi = 3.141592653589793;
%x = getWord(%a,0);
%y = getWord(%a,1);
%z = getWord(%a,2);
%rotX = getWord(%b,0) * %pi / 180;
%rotY = getWord(%b,1) * %pi / 180;
%rotZ = getWord(%b,2) * %pi / 180;

%x = %x * mCos(%rotZ) - %y * mSin(%rotZ);
%y = %x * mSin(%rotZ) + %y * mCos(%rotZ);
%y = %y * mCos(%rotX) - %z * mSin(%rotX);
%z = %y * mSin(%rotX) + %z * mCos(%rotX);
%z = %z * mCos(%rotY) - %x * mSin(%rotY);
%x = %z * mSin(%rotY) + %x * mCos(%rotY);

return %x SPC %y SPC %z;
}
If you need the output in axis form, you can convert it between euler and axis angles.

Do you need it in axis form? You can do it with euler angles like so:
Code: [Select]

If you need the output in axis form, you can convert it between euler and axis angles.

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));
}

function rotateEulerVector(%a,%b)
{
%pi = 3.141592653589793;
%x = getWord(%a,0);
%y = getWord(%a,1);
%z = getWord(%a,2);
%rotX = getWord(%b,0) * %pi / 180;
%rotY = getWord(%b,1) * %pi / 180;
%rotZ = getWord(%b,2) * %pi / 180;

%x = %x * mCos(%rotZ) - %y * mSin(%rotZ);
%y = %x * mSin(%rotZ) + %y * mCos(%rotZ);
%y = %y * mCos(%rotX) - %z * mSin(%rotX);
%z = %y * mSin(%rotX) + %z * mCos(%rotX);
%z = %z * mCos(%rotY) - %x * mSin(%rotY);
%x = %z * mSin(%rotY) + %x * mCos(%rotY);

return %x SPC %y SPC %z;
}

function rotateAxisVector(%a,%b)
{
return eulerToAxis(rotateEulerVector(axisToEuler(%a), axisToEuler(%b)));
}

Okay, thanks. I'll try that shortly.