# Correct aircraft rotation

Hi, I am new to JME so bear with me. I am trying to write a basic flight simulator. I have an aircraft spatial that I need to rotate about the pitch (lateral) axis, the roll (longitudinal) axis and the yaw (vertical) axis. The aircraft is positioned ahead of the player so that you effectively have a chase camera view. When trying to rotate the aircraft, it seems to be locked to the preset X, Y and Z axes. So, initially if I roll the aircraft, it appears to roll correctly. However, when I pitch the aircraft up to a 90 degree position and then try and roll the aircraft for example, then it appears to yaw instead. (This seems to be because the rotation in roll is set about the Z axis and when the aircraft is pitched up 90 degrees then the Z axis runs through the top of the aircraft and not longitudinally as it was when the aircraft was at zero pitch).

What I am trying to achieve is that regardless of the aircrafts orientation in space, the controls will either pitch, roll or yaw the aircraft about the actual longitudinal, lateral and vertical axes of the aircraft (not the X, Y and Z) axes of the space it is in.

I have a flight model that returns a pitch angle, yaw angle, and roll angle relative to an absolute zero (ie straight and level and flying into the screen). So, each cycle I am trying to calculate the orientation of the aircraft based on these angles. (I am not trying to increment the angles each cycle when calculating the rotations because the flight model takes care of that and I just need to orientate the model correctly and place it on the screen).

I have tried reading as much as I can on the forums and the documentation, but I cant find anything that specifically deals with this. I am aware of things such as the order of the rotations, and angles in radians etc.

Initially I created a quaterion from all three angles and then applied that rotation to the spatial. This gave the result above. I then tried creating three separate quaterions for each axis of rotation and then multiplied them together using mult. This gave the same result. I have since tried to fromAngleNormalAxis to try and establish axes relative to the aircraft and then create quaterions based on these axes and then multiply them to get the overall rotation but to no avail!

I am sure that this is a problem that many people deal with, and I am sure that there is a simple solution. Please forgive my lack of correct terminology, or perhaps lack of understanding, but any help would be appreciated. At this stage I have not posted any code as I think it is a conceptual problem rather than a coding problem, however if you need me to post code, then I can. (our internet here in Papua New Guinea is pretty bad, so forgive any delays in responding!).

I think this is because you are using anglesâ€¦ but I canâ€™t see your code so I canâ€™t say for sure. If you are always composing plane rotation from three angles then itâ€™s never going to work out right.

Rotations have to be built up. Like if you want to adjust pitch by 5 degrees then you need to multiply a 5 degree pitch rotation by the current rotation. You canâ€™t just add pitch += 5 and then recompose or you will get the problem you state.

If you accumulate rotation then no matter what angle the plane is already facing, a 5 degree pitch up is always relative to the plan, a 5 degree yaw changes is always relative to the current rotation, etcâ€¦ â€ścurrent rotationâ€ť being the part that you are missing right now.

1 Like

Thanks for the quick response! From what you are saying I think that I have to use incremental angle changes in the three axes, and not calculate an overall aircraft orientation from the three angles (pitch, roll and yaw).

Currently, I have the angles as calculated by the flight model (anglePitch, angleYaw and angleRoll). From there I work out 3 quaterions as follows:

pitch.fromAngleAxis((float)(anglePitch),Vector3f.UNIT_X );
yaw.fromAngleAxis((float)(angleYaw),Vector3f.UNIT_Y );
roll.fromAngleAxis((float)(angleRoll),Vector3f.UNIT_Z );

I then calculate a total aircraft orientation quaterion as follows:

aircraftAngle=pitch.mult(yaw).mult(roll);

Following that in the simpleupdate method I orientate the aircraft by using:

aircraftNode.setLocalRotation(flightModel.getAircraftAngle()); (this line gets the aircraft angle quaterion from the flight model and rotates the aircraftNode acordingly)

However, if I understand your answer correctly, always working it out from the initial absolute orientation is not correct.

So, to do this correctly (and please correct me if I am wrong) I think I need to use the angle changes as they occur: So, does that mean I do the following (for example)?:

1. initial rotation is 5 degrees pitch up.
2. create quaternion with this delta rotation
3.second rotation is 5 degrees roll.
3. create quaternion with this delta rotation
4. multiply the two quaternions (in the correct order). etc etc