I previously created a helper file that contains subroutines that you can use to correctly rotate an aircraft object. Now that I am somewhat familiar with how three.js rotates objects, I wonder if I use three.js to perform these calculations?
I have created a demo using 2 objects, airobj (the aircraft object) and airaxe (an axis used to position airobj) and . Both use a rotation order of “YXZ”. The airobj is linked to airaxe.
I position airaxe using the following:
airaxe.rotation.z = Mod360(360 - ACBank) * DegRad; // Bank
airaxe.rotation.x = ACPtch * DegRad; // Pitch
airaxe.rotation.y = -ACHead * DegRad; // Heading
where Mod360 is my subroutine that limits values to degrees and DegRad converts degrees to radians.
The airaxe object acts as a “base” from which to rotate airobj. Starting from this initial rotation of airaxe, I can Bank, Pitch or Yaw the airobj and it will behave perfectly. However, once I have done one of these actions, the others will no longer work correctly. So what I need is something that will update the rotation of airaxe.
Since you can get the world rotation of airobj, is there a simply way to transfer that rotation to airaxe?
I have looked online and it appears that getWorldRotation has been replaced by getWorldQuaternion. And it appears that you might have been able to use something as simple as airobj.getWorldQuaternion(airaxe) to transfer rotation from one object to another. (But this particular command generates an error - “e.setFromRtationMatrix is not a function”) After transferring the values to airaxe, I would set the airobj rotations to zero.
Does this approach make sense?
If so, what commands should I use?
Or is there an even simpler method that I am completely overlooking?
SOLVED?
Through trial and error, I think I have found a solution as shown in this demo.
I added the following variable to the beginning of the program:
var quaternion = new THREE.Quaternion();
Here is the subroutine for rotating the aircraft:
function rotePlane() {
// Rotate airobj (child) by changes
airobj.rotation.z = -ACBDif*DegRad; // Change in Bank
airobj.rotation.x = PPPDif*DegRad; // Change in Pitch
airobj.rotation.y = -YawDif*DegRad; // Change in Yaw
// Get world quaternion for child and save to parent
airobj.getWorldQuaternion(quaternion);
airaxe.setRotationFromQuaternion(quaternion);
// Zero out airobj rotations (so don't double up)
airobj.rotation.z = 0;
airobj.rotation.x = 0;
airobj.rotation.y = 0;
// Load new values (for display or use in program)
ACBank = Mod360(-airaxe.rotation.z*RadDeg);
ACPtch = airaxe.rotation.x*RadDeg;
ACHead = Mod360(-airaxe.rotation.y*RadDeg);
}
Is this the best way to achieve this result?
This definitely simplifies my task. I can forget about all those Napier formulae and trying to make them work in 360 degree rotation error-free. (Computing yaw was giving me special problems.)
I worried that using the three.js routines might add to the computing time. However, the three.js routines would be called whenever I rotate an object. Even using two objects, I am probably saving time by eliminating the need to use my computations.
If this is okay, I will revise my rotation and flight simulation demos.