Rotation of a object using ".transform()"
by andrisalim · in General Discussion · 06/19/2011 (7:04 am) · 3 replies
Hi guys,
I've been doing a test to set a random impulse on a cube to make it fly to the air and then try to rotate it randomly through any axis, can be all of x, y, z axis when the cube is in the air.
The applyImpulse part is done, but when comes to rotating it in the air or maybe on the ground before I apply the impulse, I encounter problems. The direction can be random for rotating.
I found some codes from https://www.garagegames.com/community/forums/viewthread/104031/1 about rotating.
So I tried this, however this function is based on the desired vector from an rotating object to the target object as you can see from the thread and it is not a random ones. And then I tried another solution to the function above, also provided in the above thread.
It seems that this one is much more accurate to the rotating solution, but I am confused about the "vDirection" parameter. It says it is from the y-column of the matrix but how do I define it?
I tried to use "1 1 1" for the "vDirection" and it comes out only lifting the cube upwards without any rotation.
And then finally, I tried a solution I got from a book where it says I can use arbitary rotation method to rotate about the center of the object by creating matrix.
There is some samples so I tried this one:
Is there any other solution that can randomly or specifically rotate an object through any axis x, y and z (for example, in my case it is a cube whether it is on the ground or in the air)? One thing I'm sure is that I need to use transform, but the calculation within it is still quite confusing me. I need some helps..
Rgds,
Andri
I've been doing a test to set a random impulse on a cube to make it fly to the air and then try to rotate it randomly through any axis, can be all of x, y, z axis when the cube is in the air.
The applyImpulse part is done, but when comes to rotating it in the air or maybe on the ground before I apply the impulse, I encounter problems. The direction can be random for rotating.
I found some codes from https://www.garagegames.com/community/forums/viewthread/104031/1 about rotating.
function securityCam::LookAt2(%thisDB, %targetObj, %rotateObj)
{
%targetObjPos = %targetObj.getPosition();
%rotateObjPos = %rotateObj.getPosition();
%desiredVec = VectorNormalize(VectorSub(%targetObjPos, %rotateObjPos));
%currentAngle = getWord(%rotateObj.getTransform(), 6);
%newTransform = %rotateObjPos SPC %desiredVec SPC %currentAngle;
%rotateObj.setTransform(%newtransform);
%thisDB.schedule(200, "LookAt2", %targetObj, %rotateObj);
}So I tried this, however this function is based on the desired vector from an rotating object to the target object as you can see from the thread and it is not a random ones. And then I tried another solution to the function above, also provided in the above thread.
function directionToAxisAngle(%vDirection)
{
// orion elenzil
// we are going to compose a 3x3 rotation matrix
// which orients the Y-axis of an incoming identity transform to the %vDirection vector.
// it's as simple as just setting the y-column of the matrix to be %vDirection
// and then determining the x- and z-columns as mutually perpendicular.
// it's well worth the time to hand-work some multiplications of this matrix against various
// incoming vectors to get a feel for the whole matrix thing.
%vUp = "0 0 1"; // let's keep it simple for now. fight gimble-lock later.
%vMatrixX = VectorCross(%vDirection, %vUp); // the x-column of our 3x3 matrix. perpendicular to both %vDirection and %vUp.
%vMatrixY = %vDirection; // the y-column of our 3x3 matrix. we're just saying "be what i want".
%vMatrixZ = VectorCross(%vMatrixX, %vMatrixY); // the z-column of our 3x3 matrix. perpendicular to X and Y.
// note that the order of the components matters in a cross-product,
// and the proper order varies from 3D system to system,
// but there's only two choices, so if you find your object pointing
// 180-degrees in the wrong direction, try switching the order of
// the arguments in the two cross-products.
%matrix = %vMatrixX SPC %vMatrixY SPC %vMatrixZ;
%transform = "0 0 0" SPC rotationMatrixToAxisAngle(%matrix);
return %transform;
}
function rotationMatrixToAxisAngle(%m)
{
// orion elenzil
// %m is a string representing a rotation matrix who's columns are: x[n] = m[n], y[n] = m[3 + n], z[n] = m[6 + n].
// this is code untested, and the math comes from
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle
// this will fail if the matrix contains scaling, shearing, or is non-affine.
%m00 = getWord(%m, 0);
%m01 = getWord(%m, 1);
%m02 = getWord(%m, 2);
%m10 = getWord(%m, 3);
%m11 = getWord(%m, 4);
%m12 = getWord(%m, 5);
%m20 = getWord(%m, 6);
%m21 = getWord(%m, 7);
%m22 = getWord(%m, 8);
// some systems use row-matrices and some use column-matrices.
// i'm not sure which one was chosen by euclideanspace.com,
// so it's possible that the above code should be:
// %m00 = getWord(%m, 0);
// %m10 = getWord(%m, 1);
// %m20 = getWord(%m, 2);
// %m01 = getWord(%m, 3);
// %m11 = getWord(%m, 4);
// %m21 = getWord(%m, 5);
// %m01 = getWord(%m, 6);
// %m11 = getWord(%m, 7);
// %m21 = getWord(%m, 8);
%theta = mAcos((%m00 + %m11 + %m22 - 1) / 2);
%epsilon = 0.0001;
if (mAbs(%theta) < %epsilon)
{
// singularity at zero
return "0 0 0 0 0 1 0";
}
else if (mAbs(3.14159265359 - %theta) < %epsilon)
{
// ditto at 180, but i leave it to you to grok the whole largest-diagonal thing at that site.
return "0 0 0 0 0 1 0";
}
%m21_minus_m12 = %m21 - %m12;
%m02_minus_m20 = %m02 - %m20;
%m10_minus_m01 = %m10 - %m01;
%denominator = mSqrt(
(%m21_minus_m12 * %m21_minus_m12) +
(%m02_minus_m20 * %m02_minus_m20) +
(%m10_minus_m01 * %m10_minus_m01)
);
%x = %m21_minus_m12 / %denominator;
%y = %m02_minus_m20 / %denominator;
%z = %m10_minus_m01 / %denominator;
%axisAngle %x SPC %y SPC %z SPC %theta;
return %axisAngle;
}It seems that this one is much more accurate to the rotating solution, but I am confused about the "vDirection" parameter. It says it is from the y-column of the matrix but how do I define it?
I tried to use "1 1 1" for the "vDirection" and it comes out only lifting the cube upwards without any rotation.
And then finally, I tried a solution I got from a book where it says I can use arbitary rotation method to rotate about the center of the object by creating matrix.
There is some samples so I tried this one:
function rotateByMatrix()
{
MatrixF matrixA;
matrixA.identity();
}I tried this code only as a declaration of MatrixF, but there is script error when I tried to compile. I have look over the documentation on how it is applied to declare a MatrixF, but nothing came to my eye.Is there any other solution that can randomly or specifically rotate an object through any axis x, y and z (for example, in my case it is a cube whether it is on the ground or in the air)? One thing I'm sure is that I need to use transform, but the calculation within it is still quite confusing me. I need some helps..
Rgds,
Andri
About the author
Torque is new and interesting to me. I am keen in learning and sharpening my knowledge of Torque, especially Torque 3D which I am currently working with. :) Cheers, Andri Salim
andrisalim