Aiming a camera at an object
by kev219 · in Torque X 2D · 11/21/2008 (4:42 am) · 2 replies
I need some help here. I found a Torque script that will do what I am looking for, but I don't know what the getWord function is doing. Any ideas what is going on here or C# code that I could look at?
// get yaw from a vector
function getYawFromVector( %vec ) {
%x = getWord(%vec, 0);
%y = getWord(%vec, 1);
%yawAng = mAtan( %x, %y );
if ( %yawAng < 0.0 ) {
%yawAng += 6.28;
}
return -%yawAng;
}
// get pitch from a vector
function getPitchFromVector( %vec ) {
%x = getWord(%vec, 0);
%y = getWord(%vec, 1);
%z = getWord(%vec, 2);
if( mAbs(%x) > mAbs(%y) ) {
%pitchAng = mAtan( mAbs(%z), mAbs(%x) );
} else {
%pitchAng = mAtan( mAbs(%z), mAbs(%y) );
}
if( %z < 0.0 ) {
%pitchAng = -%pitchAng;
}
return %pitchAng;
}
// calculate an aim vector from source to target, but with offsets
// for source and target. Offsets are relative to a flat plane rotated
// along the line of sight between source point and target point.
// Also can have additional yaw and/or pitch applied
function getAimTransform(%srcPoint, %targetPoint, %srcOffset, %targetOffset, %addYaw, %addPitch) {
%srcPos = getWords(ltrim(%srcPoint @ " 0 0 0"),0,2);
%targetPos = getWords(ltrim(%targetPoint @ " 0 0 0"),0,2);
%srcOffset = getWords(ltrim(%srcOffset @ " 0 0 0"),0,2);
%targetOffset = getWords(ltrim(%targetOffset @ " 0 0 0"),0,2);
if (%addYaw $= "") {
%addYaw = 0;
}
if (%addPitch $= "") {
%addPitch = 0;
}
// generate a transform matrix based on the flat line of sight from src to target
%offset = VectorSub(%targetPos, %srcPos);
%flatOffset = getWords(%offset, 0, 1) @ " 0";
%yaw = getYawFromVector(%flatOffset);
%facing = MatrixCreateFromEuler("0 0 " @ %yaw);
// adjust both src and target points by the offsets relative to that line of sight
%offsetVec = MatrixMulPoint(%facing, %srcOffset);
%newSrcPos = VectorAdd(%offsetVec, %srcPos);
%offsetVec = MatrixMulPoint(%facing, %targetOffset);
%newTargetPos = VectorAdd(%offsetVec, %targetPos);
// now recalc facing angles given new positions plus yaw and pitch adjustments
%noffset = VectorSub(%newTargetPos, %newSrcPos);
%nYaw = getYawFromVector(%noffset) + %addYaw;
%nPitch = getPitchFromVector(%noffset) + %addPitch;
// make transform matrix for final pitch and yaw
%newFacing = MatrixCreateFromEuler(%nPitch @ " 0 " @ %nYaw);
// generate final aim transform from adjusted sourc point and final rotation
%finalTransform = getWords(%newSrcPos,0,2) SPC GetWords(%newFacing,3,6);
return %finalTransform;
}
// get yaw from a vector
function getYawFromVector( %vec ) {
%x = getWord(%vec, 0);
%y = getWord(%vec, 1);
%yawAng = mAtan( %x, %y );
if ( %yawAng < 0.0 ) {
%yawAng += 6.28;
}
return -%yawAng;
}
// get pitch from a vector
function getPitchFromVector( %vec ) {
%x = getWord(%vec, 0);
%y = getWord(%vec, 1);
%z = getWord(%vec, 2);
if( mAbs(%x) > mAbs(%y) ) {
%pitchAng = mAtan( mAbs(%z), mAbs(%x) );
} else {
%pitchAng = mAtan( mAbs(%z), mAbs(%y) );
}
if( %z < 0.0 ) {
%pitchAng = -%pitchAng;
}
return %pitchAng;
}
// calculate an aim vector from source to target, but with offsets
// for source and target. Offsets are relative to a flat plane rotated
// along the line of sight between source point and target point.
// Also can have additional yaw and/or pitch applied
function getAimTransform(%srcPoint, %targetPoint, %srcOffset, %targetOffset, %addYaw, %addPitch) {
%srcPos = getWords(ltrim(%srcPoint @ " 0 0 0"),0,2);
%targetPos = getWords(ltrim(%targetPoint @ " 0 0 0"),0,2);
%srcOffset = getWords(ltrim(%srcOffset @ " 0 0 0"),0,2);
%targetOffset = getWords(ltrim(%targetOffset @ " 0 0 0"),0,2);
if (%addYaw $= "") {
%addYaw = 0;
}
if (%addPitch $= "") {
%addPitch = 0;
}
// generate a transform matrix based on the flat line of sight from src to target
%offset = VectorSub(%targetPos, %srcPos);
%flatOffset = getWords(%offset, 0, 1) @ " 0";
%yaw = getYawFromVector(%flatOffset);
%facing = MatrixCreateFromEuler("0 0 " @ %yaw);
// adjust both src and target points by the offsets relative to that line of sight
%offsetVec = MatrixMulPoint(%facing, %srcOffset);
%newSrcPos = VectorAdd(%offsetVec, %srcPos);
%offsetVec = MatrixMulPoint(%facing, %targetOffset);
%newTargetPos = VectorAdd(%offsetVec, %targetPos);
// now recalc facing angles given new positions plus yaw and pitch adjustments
%noffset = VectorSub(%newTargetPos, %newSrcPos);
%nYaw = getYawFromVector(%noffset) + %addYaw;
%nPitch = getPitchFromVector(%noffset) + %addPitch;
// make transform matrix for final pitch and yaw
%newFacing = MatrixCreateFromEuler(%nPitch @ " 0 " @ %nYaw);
// generate final aim transform from adjusted sourc point and final rotation
%finalTransform = getWords(%newSrcPos,0,2) SPC GetWords(%newFacing,3,6);
return %finalTransform;
}
#2
getWord appears to just get one of a vector's components.
Of course, I have never used TorqueScript, so I might have this all completely wrong, but since nobody else chimed in I thought I'd at least give it a shot.
11/27/2008 (9:04 pm)
Just from a casual glance I would assume that you could replace this script// get yaw from a vector
function getYawFromVector( %vec ) {
%x = getWord(%vec, 0);
%y = getWord(%vec, 1);
%yawAng = mAtan( %x, %y );
if ( %yawAng < 0.0 ) {
%yawAng += 6.28;
}
return -%yawAng;
}with the following C#float x = vec.X;
float y = vec.Y;
float yawAng = (float)Math.Atan( x, y );
if( yawAng < 0.0f ) {
yawAng += 6.28f;
}
return -yawAng;getWord appears to just get one of a vector's components.
Of course, I have never used TorqueScript, so I might have this all completely wrong, but since nobody else chimed in I thought I'd at least give it a shot.
Torque Owner kev219
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=14822