Quaternion to Vector3f conversion

Is there a way to convert a Quaternion to a Vector3f? I found these two methods on a couple Unity forum posts, but they aren’t working for me.

// always points in the Z+ direction, no matter the rotation.
Vector3f rot = spatial.getWorldRotation().mult(Vector3f.UNIT_Z);
// doesn't seem to effect anything...
float[] angles = new float[3];
spatial.getWorldRotation().toAngles(angles);
Vector3f rot = new Vector3f(angles[0], angles[1], angles[2]).mult(Vector3f.UNIT_Z);
1 Like

Correct me if I’m wrong, but I think getRotationColumn(2) works to do that.

Vector3f rot = quat.getRotationColumn(2);

Hmm, that doesn’t seem to work either…

When you say convert a Quaternion to a Vector3f there are a few different things you could mean. A rotation could be represented as a Quaternion, an axis (vector3f) and an angle (double), or as euler angles (that you could pack into a vector3f if you were so inclined). So it’s worth getting clear what exactly you want. Euler angles are instinctively understandable to humans (unlike Quaternions) but a bit of a pain to actually work with so I’d suggest sticking with Quaternions unless you have a good reason not to.

What exactly are you trying to achieve? Are you trying to rotate a vector by a rotation specified as a quaternion?

float[] angles = new float[3];
spatial.getWorldRotation().toAngles(angles);
Vector3f rot = new Vector3f(angles[0], angles[1], angles[2]).mult(Vector3f.UNIT_Z);

This shows why euler angles are a bit of a pain, this does not (as I suspect you imagine) rotate UNIT_Z by that rotation; it just multiply the angles by (0,0,1), coordinate by coordinate; so setting the X-angle and Y angle to zero, and retaining the Z angle.

(Seems the angles are actually Tait-Bryan angles, which are similar to Euler angles)

I second that. It’s my most asked question on the forum: “What are you really trying to do?”

Context is everything.

What exactly are you trying to achieve? Are you trying to rotate a vector by a rotation specified as a quaternion?

Yes. I need to rotate the local muzzle location of a gun spatial by its global rotation.

(yeah, maybe I should’ve said that before…)

Vector3f worldOffset = gun.getWorldRotation().mult(localMuzzleOffset);

Edit: and perhaps useful, the 3D math tutorial: Keynote

3 Likes