- Home /
Quaternion Rotation around Local Axis
What would be the simplest way of rotating an object around his LOCAL axes using 3 Quaternions for each axis as an absolute rotation?
I need this for an airplane rotation. I have the absolute rotations for each axis as:
Quaternion yaw;
Quaternion pitch;
Quaternion roll;
I can't do:
transform.rotation = yaw * roll * pitch;
Because that would combine the rotations according to worldspace, as discussed here
Would I have to apply the yaw rotation first, then recalculate and apply the roll rotation, then recalculate and apply the pitch rotation? What would be the best way to do this?
Thanks for your thoughts!
Answer by DaveA · Oct 08, 2011 at 12:34 AM
Why not just use eulerAngles? http://unity3d.com/support/documentation/ScriptReference/Transform-eulerAngles.html for example
the problem with eulerAngles is, that I can't define the order in which the rotation is computed. And for some special cases, I should have the plane rolled before applying the pitch..
Would there be any way to do that with eulers?
Answer by aldonaletto · Oct 08, 2011 at 10:51 AM
The big problem here is: in a real airplane, all rotations that you apply refer to the airplane local axes, but when combining rotations the combination order modify this - the first rotation refers to the world axes (since no rotation was applied yet), the second use the axes rotated by the first rotation, and only the 3rd will be totally local, since the other axes were already rotated (side note: rotations run right to left, so the rightmost quaternion applies the first rotation).
To mimic a real airplane, you should apply the rotations incrementally to the current object rotation - maybe using the old Rotate, like this:
float yawSpeed = 10f; float rollSpeed = 15f; float pitchSpeed = 12f;
void Update(){ float y = Input.GetAxis("Mouse X")*yawSpeed*Time.deltaTime; float r = Input.GetAxis("Horizontal")*rollSpeed*Time.deltaTime; float p = Input.GetAxis("Vertical")*pitchSpeed*Time.deltaTime; transform.Rotate(p, y, r); // Rotate uses Space.Self as default } This works fine when someone is handling the controls, but may fail if used by AI because the accumulation of small errors over time lead to weird results: the angles don't return to zero, or rotation from one axis "leaks" to the others.
"This works fine when someone is handling the controls, but may fail if used by AI because the accumulation of small errors over time lead to weird results: the angles don't return to zero, or rotation from one axis "leaks" to the others."
Unfortunately that's exactely the case. - I'll have an AI controlling the plane. - I'd like to Lerp the roll rotation to a maximum roll, to get a smooth behaviour.
$$anonymous$$aybe I could do it incrementally and control the rotation speeds by comparing some of the local and world orientation vectors and get the angles between them?
I'm experimenting right now, and I urgently need a wall to bang my head on.. and coffee.. anyway, would be cool if anyone has a suggestion how to achieve those special rotations :)
I suppose that pitch and roll are zero most of time, right? If that's true, you can reset them when you know that they should be zero. You can call a reset function like this:
void Reset$$anonymous$$chRoll(){ transform.rotation = new Vector3(0, transform.eulerAngles.y, 0); }Or maybe something more smooth, like this:
IEnumerator Reset$$anonymous$$chRoll(){ Quaternion newRot = new Vector3(0, transform.eulerAngles.y, 0); for (float t = 0f; t < 1f;){ t += 5*Time.deltaTime; transform.rotation = Quaternion.Lerp(transform.rotation, newRot, t); yield return 0; } }Anyway, assigning some well known value to rotation at some interval is the way to correct these cumulative errors.
Your answer
Follow this Question
Related Questions
Rotating around the Y axis then on Z axis makes my object rotate on the X axis 1 Answer
Problem with quaternion rotation zxis 0 Answers
Rotation-projection to new coordinate system 1 Answer
Unity Simulate Local Rotation 0 Answers
Creating a multiple part turret what locks onto certain axis. 4 Answers