Finding the Quaternion Betwee Two Vectors

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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)));
}
}


Share on other sites
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.

Share on other sites
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)?

Share on other sites
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]

Share on other sites
Thanks a lot Vorpy, that solved it.

Share on other sites
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;

Share on other sites
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.

Share on other sites
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).

Share on other sites
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.

Share on other sites
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.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 14
• 9
• 11
• 15