- Home /
Setting Quaternion , are Euler angles updated in the same update function?
I am setting a Quaternion in update function and i want to restrict it's rotation in Y-axis by accessing the Euler Angles. This seems to pe producing random results.
Will the Euler angles be updated when the Quaternion is set?
void Update()
{
transforms[(int)ZigJointId.LeftHip].rotation = Quaternion.Slerp(transforms[(int)joint].rotation, newRotationLH, Time.deltaTime * RotationDamping);
.
.
.
transforms[(int)ZigJointId.LeftHip].localEulerAngles = new vector3(transforms[(int)ZigJointId.LeftHip].localEulerAngles.x,0,transforms[(int)ZigJointId.LeftHip].localEulerAngles.y);
}
Answer by robertbu · Aug 10, 2013 at 07:33 AM
First internally Unity stores rotations as Quaternions. So...
transform.eulerAngles = new Vector3(25.0f, 33.0f, 10.0f);
...is equivalent to:
transform.rotation = Quaternion.Euler(25.0f, 33.0f, 10.0f);
Second, the Euler angle representation that you pull back out of a Quaternion is not the same one you put in. That is there are multiple Euler representations for any given physical representation. For example try this code:
transform.eulerAngles = new Vector3(180,0,0);
Debug.Log(transform.eulerAngles);
The output is (0,180,180)...the same physical rotation in a different Euler angle representation.
So reading out a Euler angle with any expectation of a specific representation is can cause problem.
Since I don't have a solid knowledge of what you are trying to accomplish, I cannot suggest a specific fix or approach.
Thanks for the clear explanation! What i am trying to do is to set a rotation and restrict the rotation in the same update call.
I have a avatar whose legs gets inverted when i take data from $$anonymous$$inect, so what i am trying to do is to restrict the rotation but when i restrict the rotations other axis are also fucked up.
I have to be able to visualize the problem to give a fix...drawings, short video, or a detailed explanation. As a guess, you could try this:
Transform t = transforms[(int)ZigJointId.LeftHip];
t.rotation = Quaternion.FromToRotation(t.forward, t.parent.forward) * t.rotation;
$$anonymous$$y second guess is:
Transform t = transforms[(int)ZigJointId.LeftHip];
Vector3 v3Aim = t.parent.forward - (Vector3.Dot(t.parent.forward, t.up) * t.up);
t.rotation = Quaternion.FromToRotation(t.forward, v3Aim) * t.rotation;
This images shows, what the problem is:
This is the code responsible for setting up the rotation of joints, i am getting the 'orientation'
from Openni SD$$anonymous$$ for kinect, intial rotations stores the rotations at the tpose of the avatar, all the joints work fine but OpenNI is not giving correct data for correct rotation of the lefthip joint:
Quaternion newRotation = transform.rotation * orientation * initialRotations[(int)joint];
if (mirror)
{
newRotation.y = -newRotation.y;
newRotation.z = -newRotation.z;
}
transforms[(int)joint].rotation = Quaternion.Slerp(transforms[(int)joint].rotation, newRotation, Time.deltaTime * RotationDamping);
$$anonymous$$y second guess above had something like this in $$anonymous$$d. It projects the forward of the parent object onto a plane of the child object and then rotates the child to the projected vector.
Answer by Owen-Reynolds · Aug 10, 2013 at 02:54 PM
From your comment, setting individual x/y/z angles in a quaternion (newRotation) doesn't work. The problem, (as robertu mentions,) is that they all get changed around.
The trick is not to mix quats and x/y/z angles. If you have the normal x/y/z angles, you can adjust them all beforehand, then use quaternion.euler(x,y,z) at the end. Or use only quaternions -- do only quaternion multiply to change things.
But, angle math is tricky. That's why we have to use quaternions, local/worldSpace, lookAt with a free axis... . There are lots of problems, with very different solutions. I think your question is really "how do I combine these rotations, but with the middle hip rotation mirrored?"
Yes, you understood it correctly, but the middle hip rotation should not be mirrored as only the y angle is rotated, otherwise it works fine. Also, the left hip is inversted sometimes and sometimes it is fine. It is the problem of the tracking algorithm. So what is want to fix the rotation when the legs gets inverted.
The official Question you asked here is odd (the answer is that Quaternions and Updates are totally unrelated.) $$anonymous$$ight help if you re-asked, mentioning "Openni $$anonymous$$innect" (?) your code snippet and the problem. $$anonymous$$ake it easier for someone who models to find.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shade Based on Normal Map World Position Angle (Updated) 1 Answer
how can i determine the angle of the face of an object relative to the world? 0 Answers
spotlights spot angle 1 Answer
Rotation around z-axis always local space and not world? Vector-axis angle projection calcuations. 0 Answers
Vector3.SignedAngle wrong direction when crossing the 0 point 1 Answer