•      Sign In
• Create Account

## Finding the Quaternion Betwee Two Vectors

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Mr Awesome  Members

106
Like
0Likes
Like

Posted 21 December 2006 - 01:10 PM

I have two 3d vectors, and I would like to find the quaternion such that v0 * q yields v1. I tried taking the cross product and the inverse cosine of the dot product and constructing a quaternion based on the axis and angle of the rotation, but the result was not even a unit quaternion. Here is the relevant code; perhaps someone can find the error or point me in the right direction.
Quaternion::Quaternion(const Vector& axis, float angle)
{
if (abs(angle) < 1e-6)
{
w = 1.0;
x = y = z = 0.0;
return;
}

angle *= 0.5;
float sinAngle = sin(angle);

w = cos(angle);
x = axis.x * sinAngle;
y = axis.y * sinAngle;
z = axis.z * sinAngle;
}

float Vector::dot(const Vector& vector) const
{
return x * vector.x + y * vector.y + z * vector.z;
}

Vector Vector::cross(const Vector& vector) const
{
return Vector(y * vector.z - z * vector.y, z * vector.x - x * vector.z, x * vector.y - y * vector.x);
}

Quaternion Vector::rotationTo(const Vector& vector) const
{
if (*this == vector)
return Quaternion::IDENTITY;
else
{
Vector a = this->normalize();
Vector b = vector.normalize();

return Quaternion(a.cross(b), acos(a.dot(b)));
}
}


### #2Raghar  Members

92
Like
0Likes
Like

Posted 21 December 2006 - 01:26 PM

When I had these situations I tried to find a third vector "vc" that was able to find the second vector.

So vb = va + vc
??? = (0, 1, 2) + (0, 3, 6)
(0, 4, 8) = (0, 1, 2) + (0, 3, 6)
Considering vc = vb - va
finding vc was straightforward.

Now two questions. Are both vectors normalized? Would additional normalization of elements yield the desired result?

You should know I side with Diana, when someone talks about that four element monstrosity.

### #3Zipster  Members

1723
Like
0Likes
Like

Posted 21 December 2006 - 01:28 PM

Your quaternion construction looks correct. Did you convert the vector v0 to a quaternion first, and are you performing the rotation using q*v0*conj(q)?

### #4Vorpy  Members

869
Like
0Likes
Like

Posted 21 December 2006 - 01:54 PM

The magnitude of a cross product of two vectors is equal to the product of the magnitudes of the vectors times the sine of the angle between them. Unless the normalized vectors were perpendicular to each other, the cross product will have a magnitude of less than one.

Instead of normalizing the two vectors and then finding the cross product, you should just calculate and then normalize the cross product.
Vector v = (this->cross(vector)).normalize();return Quaternion(v, acos(a.dot(b)));
There are actually infinitely many rotations that will rotate the first vector to be in the direction of the second vector, but this method does find the rotation that is in some sense the shortest, and probably the one which you are thinking of. To get the longer rotations you can multiply this quaternion by one that uses the original vector as the direction of the axis, with an arbitrary angle of rotation.

[Edited by - Vorpy on December 21, 2006 8:54:00 PM]

### #5Mr Awesome  Members

106
Like
0Likes
Like

Posted 21 December 2006 - 02:38 PM

Thanks a lot Vorpy, that solved it.

### #6scgames  Members

2082
Like
2Likes
Like

Posted 21 December 2006 - 03:48 PM

Quote:
 Original post by RagharYou should know I side with Diana, when someone talks about that four element monstrosity.
IMO rejecting quaternions as unuseful (or, conversely, touting them as a panacea for all rotation-related ills) is not very well informed.

Also, don't believe everything you read online about quaternions: much of it is confused, misguided, or simply incorrect.

@The OP: There's actually a very nice algorithm for finding the quaternion that rotates one vector onto another that has a couple of advantages over the 'standard' method, namely that it works with vectors of arbitrary length, and that it handles uniformly (and robustly) the case where the vectors are aligned and nearly parallel.

Here's the algorithm in pseudocode:
quaternion q;vector3 c = cross(v1,v2);q.v = c;if ( vectors are known to be unit length ) {    q.w = 1 + dot(v1,v2);} else {    q.w = sqrt(v1.length_squared() * v2.length_squared()) + dot(v1,v2);}q.normalize();return q;

### #7pTymN  Members

464
Like
0Likes
Like

Posted 22 December 2006 - 06:49 AM

Interestingly, it does not necessarily mean anything to say that you have a quaternion that rotates from one vector to another, because you are forgetting to say anything about the orienation of that new vector. Think of it like this, you have a plus sign shape of 5 points that you rotate with the generated quaternion. Is the new + oriented how you expected? There are actually an infinite number of quaternions that satisfy the constraint of rotating a vector to a new vector. There is only one quaternion that does that but also satisfies the additional constraint. Think about it in terms of rotating a coordinate frame, and it might make more sense. Sorry I couldn't explain it better.

### #8scgames  Members

2082
Like
0Likes
Like

Posted 22 December 2006 - 07:10 AM

Quote:
 Original post by pTymNInterestingly, it does not necessarily mean anything to say that you have a quaternion that rotates from one vector to another, because you are forgetting to say anything about the orienation of that new vector.
Just to clarify/defend my previous post, I didn't address the non-uniqueness of the rotation since it had already been addressed by Vorpy earlier:
Quote:
 Original post by VorpyThere are actually infinitely many rotations that will rotate the first vector to be in the direction of the second vector, but this method does find the rotation that is in some sense the shortest, and probably the one which you are thinking of.
It seemed clear that the topic of the thread was the 'shortest arc' problem, so I didn't restate it in my post (although perhaps I should have).

### #9Vorpy  Members

869
Like
0Likes
Like

Posted 22 December 2006 - 10:55 AM

That other way of finding the quaternion is pretty neat. I spent a while just trying to prove to myself that the w coordinate comes out the same in either method. I think the best part about it is how it eliminates the trig functions, which do look almost redundant in the axis/angle method but require some tricky manipulations to eliminate.

### #10Zipster  Members

1723
Like
0Likes
Like

Posted 22 December 2006 - 11:29 AM

Whoops I guess I skimmed over the code a bit too quickly [grin]

But yeah, those half-angles just scream "trig identity", and since the dot product and cross product already contain trig functions you might as well find some clever way to manipulate them.

### #11scgames  Members

2082
Like
0Likes
Like

Posted 22 December 2006 - 01:07 PM

Quote:
 Original post by VorpyThat other way of finding the quaternion is pretty neat.
Yeah, it's pretty slick.

I originally picked it up from Martin Baker's site, here.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.