# Projecting a UnitQuaternion on a 2D plane

I have the orientation of a IMU device as a `UnitQuaternion`. What is the best way to get it’s projection on the XY-plane? (a `Vector2`)?

I am not sure I understand your question. What do you mean by projecting a unit quaternion to a plane ? (I don’t know anything about IMU devices.)

I get the heading of my IMU device in space as a Quaternion [x, y, z, w]. If it’s sitting flat on the floor, it should point forward along the XY-plane. (I’m considering the floor to be the XY-plane, and positive Z to be up).

Although I get a value in 3D space, I’m only interested in the heading in the XY-plane. If I disregard the “twisting” (My mental model of a quaternion is a 3D vector, which is additionally “twisted” about its axis (roll?)), and only think about the quaternion as a 3-dimensional vector, pointing somewhere in 3D space, I should be able to project that vector onto my 2D XY-plane.

Alternatively, what I might really be after is the “yaw” component of the original quaternion represented as Euler angles. Unfortunately I didn’t find a method to convert a `Quaternion` to Euler angles. (I did find the other way http://nalgebra.org/rustdoc/nalgebra/geometry/type.UnitQuaternionBase.html#method.from_euler_angles)

Forgive me for not necessarily using the correct terms. Unfortunately geometry is not my strongest suit.

You can see the xyz part of the quaternion as the rotation axis scaled by the rotation angle (or, more precisely by the cosinus of half the angle). So projecting this axis on the XY plane will probably not give you any insight regarding the heading of the plane. Instead, I think you could project the vector `q * v` to the XY plane (in the word global coordinate system), where `q` is your quaternion, and `v` is a vector (in the plane’s local coordinate system) passing through the tail and front of the plane.

A conversion from quaternion to Euler angles might be something we could consider adding to nalgebra though.

Of course
Because I knew my quaternion `q` represents an object (the IMU) in space, my geometric intuition of it was more like a (absolute) `Vector3`. Since it’s a (relative) rotation, I need to have some (absolute) “forward” vector `v` to rotate.

I did the following, and it seems to work:

``````    let v = Vector3::new(1.0, 0.0, 0.0); //forward
let v_rot = q * v; //rotate v by q
let v_proj = Vector2::new(v_rot.x, v_rot.y);
let theta = v_proj.y.atan2(v_proj.x);
``````

Thank you!

Hi,
I am also trying something similar where I need to plot the quaternion in a 2D plane.
From your code, you get x and y from the new quaternion but I don’t get how will you map to the xy plane, just like that it will work? What the theta calculation for?

Thanks.

Hi @jxgeoffrey,
I believe the `theta` calculation is done because @marikka actually just needed the heading of the object in the XY plane. So here `theta` would represent the angle (wrt. the X axis) of the “forward” vector on the XY plane. Though note that the latest version of `nalgebra` can compute the `yaw` component of the unit quaternion using .to_euler_angles() if your needs are similar to those described by @marikka at the end of his second post.

Thanks. I figured it out. I had to use Z-axis as my forward vector to get it plotted on a 2D plane.