Question

I´m trying to rotate a Vector3D like this:

Vector3D i = new Vector3D(1, 1, 0);
i.Normalize();

Matrix3D m = Matrix3D.Identity;
Quaternion rot = GetShortestRotationBetweenVectors(i, new Vector3D(1, 0, 0));
m.Rotate(rot);

Vector3D j = new Vector3D(0, 1, 0);
Vector3D jRotated = m.Transform(j);
// j should be equal to i

public static Quaternion GetShortestRotationBetweenVectors(Vector3D vector1, Vector3D vector2)
{
vector1.Normalize();
vector2.Normalize();
float angle = (float)Math.Acos(Vector3D.DotProduct(vector1, vector2));
Vector3D axis = Vector3D.CrossProduct(vector2, vector1);

// Check to see if the angle is very small, in which case, the cross product becomes unstable,
// so set the axis to a default.  It doesn't matter much what this axis is, as the rotation angle 
// will be near zero anyway.
if (angle < 0.001f)
{
    axis = new Vector3D(0.0f, 0.0f, 1.0f);
}

if (axis.Length < .001f)
{
    return Quaternion.Identity;
}

axis.Normalize();
Quaternion rot = new Quaternion(axis, angle);

return rot;
}

I get the rotation matrix to pass from i = (0.77 ,0.77, 0) to (1,0,0) => 45º. So (0, 1, 0) rotates 45º, and the result should be (0.77, 0.77, 0). But the result is almost the same original vector (0,1,0), so no transform has been done.

How can I rotate vectors? I have a vector (x, y, z), and this vector should rotate to (1, 0, 0). For this operation, suppose we have to rotate 30º. So how do I rotate all the vectors I have this 30º?

Was it helpful?

Solution

At last I found the problem.

float angle = (float)Math.Acos(Vector3D.DotProduct(vector1, vector2));

gives the angle in radians.

Quaternion rot = new Quaternion(axis, angle);

expect the angle in degrees

So the solution is easy:

float angle = (float)(Math.Acos(Vector3D.DotProduct(vector1, vector2)) * (180 / Math.PI));

This means there is a bug in Avateering-XNA (Microsoft Official Software).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top