Help with Yaw/Pitch being converted to a cameras transform...
by Matt T · in Torque 3D Professional · 01/05/2014 (5:18 pm) · 6 replies
Hello there , im a developer over at good ol` Legions. We have come up with a way to get freecams into demos through mostly hackery. Its something we plan to share over here once its done , going to add keyframe for camera/timescale etc too.
The movement is working fine...I am pretty bad with rotations and this is a dousie:
The functions are below... it appears RPYtoTransform(%roll, %pitch, %yaw) is not working correctly and the math is beyond us.
Can anyone help us out with why that function is not working ? Or another way to convert mouse pitch/yaw to a cameras transform. This has to stay in script.
Here are the echos being returned:
The movement is working fine...I am pretty bad with rotations and this is a dousie:
The functions are below... it appears RPYtoTransform(%roll, %pitch, %yaw) is not working correctly and the math is beyond us.
Can anyone help us out with why that function is not working ? Or another way to convert mouse pitch/yaw to a cameras transform. This has to stay in script.
Here are the echos being returned:
the transform I received to convert to RPY was 29.7113 185.442 228.483 1 0 0 0 the old rotation converted is 0 0 0 the old rotation converted plus current pitch/yaw is 0 29.3117 17.9891 converted back the new rotation is 1 0 0 0
About the author
Lead Developer - Legions Overdrive www.legionsoverdrive.com
#2
01/05/2014 (5:21 pm)
/////////////////////////////////////////////////////////////////////////
// MATH HELPERS////http://www.garagegames.com/community/blogs/view/21516
/////////////////////////////////////////////////////////////////////////
function RPYtoTransform(%roll, %pitch, %yaw){
//Convert roll, yaw and pitch in radians to axis angle from setTransform
%cr = mCos(%roll/2.0);
%sr = mSin(%roll/2.0);
%cy = mCos(%yaw/2.0);
%sy = mSin(%yaw/2.0);
%cp = mCos(%pitch/2.0);
%sp = mSin(%pitch/2.0);
// Convert to quaternion
%qw = %cr*%cy*%cp + %sr*%sy*%sp;
%qx = %sr*%sy*%cp + %cr*%cy*%sp;
%qy = %sr*%cy*%cp + %cr*%sy*%sp;
%qz = %cr*%sy*%cp - %cr*%cy*%sp;
%denom = mSqrt(1-%qw*%qw);
if(%denom == 0){
%out = "1 0 0 0";
} else {
%theta = 2.0*mAcos(%qw);
%x = %qx/%denom;
%y = %qy/%denom;
%z = %qz/%denom;
%out = %x SPC %y SPC %z SPC %theta;
}
return %out;
}
function TransformtoRPY(%transform){
//Input is the rotational part of the transform
echo("the transform I received to convert to RPY was" SPC %transform);
%x = getWord(%transform,3);
%y = getWord(%transform,4);
%z = getWord(%transform,5);
%theta = getWord(%transform,6);
//Convert to quaternion
%qw = mCos(%theta/2.0);
%st = mSin(%theta/2.0);
%qx = %x*%st;
%qy = %y*%st;
%qz = %z*%st;
//Convert to roll, yaw and pitch
%yaw = mAsin(2.0*%qx*%qy + 2.0*%qz*%qw);
%roll = mAtan(2.0*%qy*%qw - 2*%qx*%qz, 1.0 - 2.0*%qy*%qy - 2.0*%qz*%qz);
%pitch = mAtan(2.0*%qx*%qw - 2.0*%qy*%qz, 1.0 - 2.0*%qx*%qx - 2.0*%qz*%qz);
//Correct pitch to -pi/2 to pi/2
if(%pitch<=-mPi()/2.0){
%pitch = -(mPi() + %pitch);
}
if(%pitch>=mPi()/2.0){
%pitch = (mPi() - %pitch);
}
%out = %roll SPC %pitch SPC %yaw;
return %out;
}
#3

01/08/2014 (12:12 pm)
Bump for key-framed democam justice , ive made progress on other parts.. still stuck on this ^above though : (
#4
01/09/2014 (6:38 am)
Not sure if it'll help, but it might be easier to use a dummy object - no material, invisible. Move it and rotate it as you would a normal object then set the camera position and rotation to match the object (%cam.position = %dummy.position; %cam.rotation = %dummy.rotation;). The engine's way of handling full transform data is weird at best and this is the easiest way I've seen to bypass it. Used it in the "Adventure Prototype" script tutorial in the docs.
#5
Well incase someone comes across this needing the solution , its :
on a 1 ms schedule its very smooth camera movement. woohoo !
credit to this guy from 2005:
http://www.garagegames.com/community/forums/viewthread/35282
01/10/2014 (5:48 pm)
@Richard: Thanks , I gave it a try with no luck but ended up finding this.Well incase someone comes across this needing the solution , its :
//add desired amount of pitch and yaw to a camera/object
%oldTransform = $demoCam.getTransform();
%pos = getWords(%oldTransform, 0, 2);
%a = %pos SPC 0 SPC 0 SPC 1 SPC $yawChange;
%b = %pos SPC 1 SPC 0 SPC 0 SPC $pitchChange;
%c = %pos SPC 0 SPC 1 SPC 0 SPC 0;
%d = %pos SPC 0 SPC 1 SPC 0;
%r = matrixmultiply(%a, %b);
%r = matrixmultiply(%r, %c);
%r = matrixmultiply(%r, %d);
%newTransform = matrixmultiply(%oldTransform, %r);on a 1 ms schedule its very smooth camera movement. woohoo !
credit to this guy from 2005:
http://www.garagegames.com/community/forums/viewthread/35282
#6
02/07/2016 (1:38 am)
A bump from the grave, I just wanted to let ya know I got this working pretty well ! .. 2 years later :|
Matt T
function DCprocessMove() { %oldTransform = $demoCam.getTransform(); %pos = getWords(%oldTransform, 0, 2); %oldRotConverted = TransformtoRPY(%oldtransform); echo("the old rotation converted is" SPC %oldRotConverted); %newRot = getWord(%oldRotConverted,0) SPC getWord(%oldRotConverted,1) + $mvPitch SPC getWord(%oldRotConverted,2) + $mvYaw; echo("the old rotation converted plus current pitch/yaw is" SPC %newRot); %rotChange = RPYtoTransform(%newRot); echo("converted back the new rotation is" SPC %rotChange); if ($mvLastDirection != -1) { %forwardVector = $demoCam.getForwardVector(); %horizontalForwardVector = VectorNormalize(getWord(%forwardVector, 0) SPC getWord(%forwardVector, 1) SPC "0"); %horizontalMoveVector = rotateVector(%horizontalForwardVector, 0, 0, $mvLastDirection); %moveVector = %horizontalMoveVector; if ($mvJumpAction) %moveVector = VectorNormalize(VectorAdd(%moveVector, "0 0 1")); if ($mvCrouchAction) %moveVector = VectorNormalize(VectorAdd(%moveVector, "0 0 -1")); %offset = VectorScale(%moveVector, 0.14); %pos = VectorAdd(%pos , %offset); } $demoCam.setTransform(%pos SPC %rotChange); schedule(1,serverconnection,"DCprocessMove"); }