- Home /
Given a quaternion, how do I modify it to zero rotations about certain axes?
I have been struggling with this problem for weeks so I finally decided to seek help. I am working on a script for controlling a first person character using the gyro. I found the iPhone script by Perry Hoberman and I have since converted it to C# and made modifications to it to suit my purpose...mostly by trial and error ;) The problem I am trying to deal with is that I need the gyro to only control the vertical rotation(x-axis) of the camera and only the horizontal rotation (y-axis) of the parent of the camera. The following is the update function code that I am using to get the quaternion for the camera rotation:
void Update () {
rotation = Input.gyro.attitude * new Quaternion(0f,0f,0.7071f,0f);
transform.localRotation = rotation;
}
I have confirmed that this works even though the w component of the rhs quaternion is strangely 0. Without the rhs quaternion, rotations are inverted where tilting the phone up causes the camera to look down and I believe the other axes were swapped.
What I now need to do is to isolate only the x-axis of the rotation quaternion for the camera and the y-axis for the parent. Converting the quaternion to euler and setting the unwanted axes to zero does not work. It does restrict rotation to the wanted axis but rotation on that axis gets limited to a 180 degree range if I remember correctly.
I am new to Unity and quaternions so if I am going about this in the wrong way please let me know and any help with getting the gyro to work as a first person controller would be greatly appreciated.
Answer by tanoshimi · Dec 16, 2013 at 07:27 AM
Quaternions don't have an x-axis of rotation- if you're working in that frame of reference you need to be using Euler angles instead. What makes you say that would limit the range of rotation to 180 degrees? It should work fine by creating a Vector3 of the eulerAngles, zeroing the z parameter, and reassigning to the transform.rotation.
I understand that quaternions don't have an x-axis and I guess what I really want is a quaternion that is equivalent to the Euler rotation without the y and z components. I use this to output the Euler values on the screen to try to figure out what is happening:
GUI.TextArea(new Rect(100,0,300,50),"X"+rotation.eulerAngles.x+" Y:"+rotation.eulerAngles.y+" Z:"+rotation.eulerAngles.z)
When my phone is facing south and to the floor, the x value is 360 and it goes down to 270 as I tilt upwards to facing forward but the confusing thing is that as I tilt further to point to the ceiling in the same direction, the value starts to go back up to 360. Now obviously if I use those Euler values to recreate a quaternion for the rotation, I would get correct rotation from facing the floor to facing forward but as I tilt to the ceiling the camera starts to tilt back to the floor. Also the range of x values changes from 0 to 90 and back to 0 if I am facing north.
Your answer
Follow this Question
Related Questions
Gyro quaternion offset 1 Answer
3D camera relatively using gyro a la N.O.V.A. 2 0 Answers
Camera Rotation using Gyroscope: Yaw and Pitch reversed 2 Answers
Quaternion partial offset, need help badly 0 Answers
"moveable" first person controller 0 Answers