Matrixes, quaternion, and vector math question
by CdnGater · in Torque Game Engine · 11/04/2004 (6:32 am) · 1 replies
I'm not the greatest at matrixes, quaternion, and vector math.
Anyway, I have a need to have one object slowly turn to face the same direction as a target object currently faces.
I have done this in the code below, I think. (Seems to work!)
My question is this. Is there a better way to do the same thing using matrixes, quaternion, and vector math?
Edit: Fixed spacing in code
Anyway, I have a need to have one object slowly turn to face the same direction as a target object currently faces.
I have done this in the code below, I think. (Seems to work!)
My question is this. Is there a better way to do the same thing using matrixes, quaternion, and vector math?
// default settings
F32 modThreshhold = 0.015;
F32 modAdjust = 0.01;
// get target matrix
const MatrixF& targetMat = pTargetObject->getTransform();
Point3F targetDir;
targetMat.getColumn(1,&targetDir);
// get target yaw
F32 targetYaw, targetPitch;
MathUtils::getAnglesFromVector( targetDir, targetYaw, targetPitch );
// get my matrix
const MatrixF& myMat = getTransform();
Point3F myDir;
myMat.getColumn(1,&myDir);
// get my yaw
F32 myYaw, myPitch;
MathUtils::getAnglesFromVector( myDir, myYaw, myPitch );
// find the yaw diff
F32 yawDiff = targetYaw - myYaw;
// make it between 0 and 2PI
if( yawDiff < 0.0f )
yawDiff += M_2PI;
else if( yawDiff >= M_2PI )
yawDiff -= M_2PI;
// now make sure we take the short way around the circle
if( yawDiff > M_PI )
yawDiff -= M_2PI;
else if( yawDiff < -M_PI )
yawDiff += M_2PI;
if (mFabs( yawDiff ) < modThreshhold)
{
// I am close enought to target yaw, may as well make it the same
myYaw = targetYaw;
}
else
{
// adjust my yaw by a set amount times delta time
// this way I will slowly come to the same bearing as
// my target
if (yawDiff < 0)
myYaw -= modAdjust * dt;
else if (yawDiff > 0)
myYaw += modAdjust * dt;
}
// oh yeh, need to set myself now
setPosition( myMat.getPosition(), Point3F( 0, 0, myYaw ) );Edit: Fixed spacing in code
Katrina Rose
Default Studio Name
$sgtFirst.setTransform(changeRotation($myPlayer.getTransform(),$sgtFirst)); function changeRotation(%transform,%obj) { //echo("In change rotation"); %rotation1 = GetWords(%transform,3,6); %rotation2 = "0 0 1 3.14"; //1 being y axis, 3.14 being 180 degrees. %v1 = "0 0 0" SPC %rotation1; %v2 = "0 0 0" SPC %rotation2; %v3 = MatrixMultiply(%v1,%v2); %newtrans = getWords(%obj.getTransform(),0,2) SPC GetWords(%v3,3,6); return %newtrans; }This code makes $sgtFirst face $myPlayer. If you change the %rotation2 you should get the results you desire.
I hope this helps,
Marrion