## [Solved] quaternion between 2 vectors

### [Solved] quaternion between 2 vectors

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!
Last edited by TheWonderBear on Tue Oct 14, 2008 9:29 am, edited 1 time in total.
TheWonderBear
Gnoblar

Posts: 21
Kudos: 0
Joined: 11 Feb 2008
Location: Brisbane, Australia

You can generate an angle-axis 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;

Code: Select all
``Vector3 axis;Radian angle;axis.crossProduct(v2, v1);axis.normalise();angle = Ogre::Math::ACos(veca.dotProduct(vecB));Quaternion qRot(angle, axis);``

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.
Yesterday returneth not. Tomorrow perhaps cometh not. Today is thine. Misuse it not.
rusty
Kobold

Posts: 33
Kudos: 0
Joined: 09 Sep 2005
Location: Frankfurt, Germany

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.
TheWonderBear
Gnoblar

Posts: 21
Kudos: 0
Joined: 11 Feb 2008
Location: Brisbane, Australia

No worries. I think you would have gotten the solution yourself eventually since you started to consider the dot-product. 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.
rusty
Kobold

Posts: 33
Kudos: 0
Joined: 09 Sep 2005
Location: Frankfurt, Germany

You can also just do:
Code: Select all
``Quaternion q = v0.getRotationTo(v1);``

nullsquared
Old One

Posts: 3245
Kudos: 9
Joined: 24 Apr 2007
Location: NY, NY, USA

nullsquared wrote:You can also just do:
Code: Select all
``Quaternion q = v0.getRotationTo(v1);``

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.

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 ready-made 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.
rusty
Kobold

Posts: 33
Kudos: 0
Joined: 09 Sep 2005
Location: Frankfurt, Germany

rusty wrote:Or have I gotten it all completely wrong?

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.

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 well-named 'getRotationTo') code?

nullsquared
Old One

Posts: 3245
Kudos: 9
Joined: 24 Apr 2007
Location: NY, NY, USA

nullsquared wrote:
rusty wrote:Or have I gotten it all completely wrong?

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.

Not everybody who drives a car is wanting to be a mechanic.

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 well-named 'getRotationTo') code?

Everybody who is programming with Ogre, wants to be a programmer for various reasons.

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.
rusty
Kobold

Posts: 33
Kudos: 0
Joined: 09 Sep 2005
Location: Frankfurt, Germany