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