1

I created two objects in Unity3D, representing an upperarm (parent) and a lowerarm (child), joined with a character joint. The rotation of these objects is driven by 2 quaternions which i gather from an IMU located on the upperarm and lowerarm. I calibrate both quaternions. My objective is to visualize an elbow flexion/extension.

Q in the name indicates it is a Quaternion. QImu indicates it is the Quaternion from the IMU. Qstart indicates it is the initial starting orientation of the IMU.

// upperarm script attached to upperarm object
if (Input.GetKeyDown(KeyCode.Space))
        {
            QstartUpperArm = QimuUpperArm;
        }

        transform.localRotation = QstartUpperArm * Quaternion.Inverse(QimuUpperArm);
// lowerarm script attached to lowerarm object
if (Input.GetKeyDown(KeyCode.Space))
        {
            QstartLowerArm = QimuLowerArm;
        }

        QimuLowerArm = QstartLowerArm * Quaternion.Inverse(QimuLowerArm);
transform.localrotation = Quaternion.Inverse(Qstart2 * Quaternion.Inverse(Qimu2)) * Qimu1new;

The Upperarm object is rotating exactly as expected. The lowerarm object however has one the y-axis reversed. All other axis are fine. How can i change the sign of this one axis using the Quaternion?

5
  • Rotate about a perpendicular axis by 180 degrees. Unfortunately you can't negate just one axis without using negative scale or changing the handedness of the whole system. Commented Jul 15, 2019 at 12:04
  • Can you explain a little more what you mean by "the y-axis is reversed"? Are you saying it's rotating the wrong direction around the upper arm's local y axis? We might need images or a video to get a visual of what's happening here. Commented Jul 15, 2019 at 16:57
  • This is kind of a shot in the dark without seeing exactly what's going on... Try this: Quaternion reversedY = Quaternion.Inverse(Qstart2 * Quaternion.Inverse(Qimu2)) * Qimu1new; transform.localrotation = Quaternion.Euler(Vector3.Scale(reversedY, new Vector3(1f, -1f, 1f) ) ); Commented Jul 15, 2019 at 17:25
  • @pvand let us know if that works Commented Jul 16, 2019 at 13:57
  • I found something online which had a similar issue, the solution is: Vector3 myEulerAngles = QimuLowerArm.eulerAngles; Quaternion invertedRotation = Quaternion.Euler(myEulerAngles.x, -myEulerAngles.y, myEulerAngles.z); transform.localRotation = invertedRotation; So i guess your soluiton is kind of similar. Commented Jul 16, 2019 at 20:14

1 Answer 1

1

You shouldn't do this directly in a Quaternion. It sounds like you are confusing Quaternion (which has 4 components x,y,z,w) with the Euler space representation in x,y,z.

You could try using something like

var invertedRotation = Quaternion.Euler(Vector3.Scale(QimuLowerArm.eulerAngles, Vector3.up * -1));

Depending on which is the rotation you have to invert (didn't clearly see this in your code).

  • eulerAngles returns an Euler representation of the Quaternion

  • Vector3.Scale multiplies to Vector3 component wise. In this case by Vector3.up * -1 == new Vector3(0, -1, 0);

  • Quaternion.Euler generates a Quaternion for a rotation on given Euler angles. One has to note here that the rotations are applied in the order x then z then y but since you are inverting the latter it should be fine.


An alternative afaik (not so sure about) is to

  • negate the according axis component
  • negate the w component

I still wouldn't do this directly in the quaternion because the order matters a lot but do e.g.

var invertedRotation = new Quaternion (QimuLowerArm.x, - QimuLowerArm.y, QimuLowerArm.z, -QimuLowerArm.w);
Sign up to request clarification or add additional context in comments.

2 Comments

The first solution kind of works, whenever i hold the upperarm in the same rotation. However if i start to rotate the upperarm, the lowerarm start to rotate over the axis i just reversed. In other words i start to flex when i rotate my upperarm-object sideways...
this willl cause gimbal lock, very dangerous

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.