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

**0**

# Finding the Quaternion Betwee Two Vectors

Started by Mr Awesome, Dec 21 2006 01:10 PM

10 replies to this topic

###
#1
Members - Reputation: **106**

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.

###
#2
Members - Reputation: **92**

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.

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.

###
#4
Members - Reputation: **869**

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.

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

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();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.

return Quaternion(v, acos(a.dot(b)));

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

###
#6
Members - Reputation: **2073**

Posted 21 December 2006 - 03:48 PM

Quote:IMO rejecting quaternions as unuseful (or, conversely, touting them as a panacea for all rotation-related ills) is not very well informed.

Original post by Raghar

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

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;

###
#7
Members - Reputation: **464**

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.

###
#8
Members - Reputation: **2073**

Posted 22 December 2006 - 07:10 AM

Quote: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:

Original post by pTymN

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.

Quote: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).

Original post by Vorpy

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.

###
#9
Members - Reputation: **869**

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.

###
#10
Crossbones+ - Reputation: **1709**

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.

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.