Hi, I have been wondering how would i get the quaternion between 2 vectors so that:
v1: Vector1
v2: Vector2
q: quaternion I'm looking for
v2 = q * v1
Cheers!
[Solved] quaternion between 2 vectors

 Gnoblar
 Posts: 21
 Joined: Mon Feb 11, 2008 3:25 am
 Location: Brisbane, Australia
[Solved] quaternion between 2 vectors
Last edited by TheWonderBear on Tue Oct 14, 2008 9:29 am, edited 1 time in total.

 Kobold
 Posts: 33
 Joined: Fri Sep 09, 2005 12:42 pm
 Location: Frankfurt, Germany
You can generate an angleaxis between the two vectors
To do this, you need to find out the shortest angle between the two vectors and the common axis of rotation. The axis is a vector perpindicular the plane formed by the two vectors so this means that;
1) The cosine of the shortest angle can be calculated using dot product
2) The axis can be calculated usiong the cross product
And from this, you can generate a quaternion.
The code looks a little like;
And that's all that there is too it! You mioght want to check that the ordering of the parameters for the cross product is correct though.
To do this, you need to find out the shortest angle between the two vectors and the common axis of rotation. The axis is a vector perpindicular the plane formed by the two vectors so this means that;
1) The cosine of the shortest angle can be calculated using dot product
2) The axis can be calculated usiong the cross product
And from this, you can generate a quaternion.
The code looks a little like;
Code: Select all
Vector3 axis;
Radian angle;
axis.crossProduct(v2, v1);
axis.normalise();
angle = Ogre::Math::ACos(veca.dotProduct(vecB));
Quaternion qRot(angle, axis);
Yesterday returneth not. Tomorrow perhaps cometh not. Today is thine. Misuse it not.

 Gnoblar
 Posts: 21
 Joined: Mon Feb 11, 2008 3:25 am
 Location: Brisbane, Australia
*Slaps Forehead*
Of course!!! I was thinking that this morning but for some odd reason I just thought that it would not work due to the dot product not taking in account direction. Direction comes from the cross product .
I feel silly now.
Thanks for the help. Would have spent the whole day on this.
Of course!!! I was thinking that this morning but for some odd reason I just thought that it would not work due to the dot product not taking in account direction. Direction comes from the cross product .
I feel silly now.
Thanks for the help. Would have spent the whole day on this.

 Kobold
 Posts: 33
 Joined: Fri Sep 09, 2005 12:42 pm
 Location: Frankfurt, Germany
No worries. I think you would have gotten the solution yourself eventually since you started to consider the dotproduct. You would have eventually asked yourself; "I have an angle of rotation, but WHAT does it rotate around?".
Yesterday returneth not. Tomorrow perhaps cometh not. Today is thine. Misuse it not.
 nullsquared
 Old One
 Posts: 3245
 Joined: Tue Apr 24, 2007 8:23 pm
 Location: NY, NY, USA
 x 9
You can also just do:
Code: Select all
Quaternion q = v0.getRotationTo(v1);

 Kobold
 Posts: 33
 Joined: Fri Sep 09, 2005 12:42 pm
 Location: Frankfurt, Germany
Which is also good, but you know, this is the "back to basics forum". It's great that Ogre has so many helper methods and such things, but I think it's more helpful if people can see the steps required to get to the solution.nullsquared wrote:You can also just do:Code: Select all
Quaternion q = v0.getRotationTo(v1);
We have lots of beginner 3D coders here who, while they're not lacking in their basic knowledge in vector, matrix and maybe even quaternion maths, are still lacking in applying that knowledge. There's going to come a time when many of these people won't have a ready made set of libraries to do the job for them.
I like to think that part of reason this particular forum exists is to educate and equip people with a basic understanding of application, and not just supply the readymade function to solve the problem. That sort of thing I think belongs in the "Help" forum.
Or have I gotten it all completely wrong?
Yesterday returneth not. Tomorrow perhaps cometh not. Today is thine. Misuse it not.
 nullsquared
 Old One
 Posts: 3245
 Joined: Tue Apr 24, 2007 8:23 pm
 Location: NY, NY, USA
 x 9
You don't need to know how a car engine works to drive a car. However, yes, it'll be a useful piece of knowledge if your car breaks down in the middle of nowhere.rusty wrote: Or have I gotten it all completely wrong?
Considering the 'logic' behind the solution has already been posted and understood, why would you use 5 lines of code compared to 1 line of (very wellnamed 'getRotationTo') code?

 Kobold
 Posts: 33
 Joined: Fri Sep 09, 2005 12:42 pm
 Location: Frankfurt, Germany
Not everybody who drives a car is wanting to be a mechanic.nullsquared wrote:You don't need to know how a car engine works to drive a car. However, yes, it'll be a useful piece of knowledge if your car breaks down in the middle of nowhere.rusty wrote: Or have I gotten it all completely wrong?
Everybody who is programming with Ogre, wants to be a programmer for various reasons.nullsquared wrote: Considering the 'logic' behind the solution has already been posted and understood, why would you use 5 lines of code compared to 1 line of (very wellnamed 'getRotationTo') code?
Besides, I'm not saying "don't use this helper function" but only that just offering the helper function isn't telling people what they are wanting to know.
Yesterday returneth not. Tomorrow perhaps cometh not. Today is thine. Misuse it not.