Quaternion from two vectors

Hello, i’d like to store the rotation from the vector (1,0,0) to (x,0,z)

what is the best way to get the quaternion that describes this rotation?

THXXX!!!

Can you tell us why you want to do this? It seems like a strange requirement on its own as there are other ways to do the half a dozen things I guessed you might be doing.

lets say I have two translations vectors :
(0,0,0) -> (10,0,0)
(10,0,0) -> (10,0,10)

i would like to rotate those translations to have for X axis my new x axis (x,0,z)

it’d like to rotate all the vectors so that their xAxis becomes my new vector (x,0,z)

in order to do that I guess i’d have to get the angle between the xAxis and the new vectror (x,0,z) then appkly a rotation to all the translation vectors around the origin (Y axis)

To answer original question (not looking at alternatives):

To get the quaternion between 2 vectors, you would cross product the vectors, which would give you the axis to rotate around. Then use vector1.angleBetween (vector2); to give you the angle (make sure you pass in normalized vectors to this).

And to generate the quaternion, use: new Quaternion ().fromAngleAxis (angle, crossProdVec);

2 Likes

Ok, and why do you want to do that?

More specifically, can you describe what you are actually trying to do at a higher level? I know you have painted your floor in a particular order and are standing in a corner now wondering how to get out… but maybe there was a different way to paint the floor in the first place. :slight_smile:

oh ok guys here is what I am doing :

I am making a skill generator,

where a skill is described by a list of animations to apply to parts of an object.
what I call an animation, is a list of transformations
and transformations can be combined.

For the moment I am working on translation transformation, a translation is described by two points, a start and an end.

and an animation just a list of those transformations.
example:

you guys any idea to rotate all the translations accordingly?
here is what ive done :

angle = Cursor.sub(spatialVec).normalize().anglebetween(Vector3f.UNIT_Y)
Quternion q = new Quaternion()
q.fromAngleAxis(angle , Vector3f.UNIT_Y)

then i’d just q.mult(translation vectors)

Why not just place the animated objects inside a node and rotate/etc the node.

You can then do all your work with the much simpler non-rotated vectors and everything is magically taken care of for you.

yes, that’s what I am doing, sorry, I did a mistake, I meant i’d rotate the parent node.

this works but I’m facing a little problem, only the angles under 180 work well, the others are reverted :confused:
anglebetween seems to only return positive values ? :confused:

oww i get it, thanks wezrule,
i had to get the axis of rotation by making the cross product, it’d give me the axis of rotation (positive or negative :))