I'm in a project where I need to rotate the vector v1 with v2 as axis for the rotation, but I'm having some problems with vectors and rotations. There are a lot of explanations about that, but I couldn't find one that solved my problem. Could you explain how to do that as if I was a five year old child?
What I tried to do was to multiply a quaternion-derived matrix with my "v1" by doing so:
angle = (Convert.ToDouble(Console.ReadLine()) * Math.PI / 180);
cosA = Math.Cos(angle);
sinA = Math.Sin(angle);
oneMinusCosA = 1 - cosA;
matrix[0, 0] = gravityVector[0] * gravityVector[0] * oneMinusCosA + cosA;
matrix[0, 1] = gravityVector[0] * gravityVector[1] * oneMinusCosA + gravityVector[2] * sinA;
matrix[0, 2] = gravityVector[0] * gravityVector[2] * oneMinusCosA - gravityVector[1] * sinA;
matrix[1, 0] = gravityVector[1] * gravityVector[1] * oneMinusCosA - gravityVector[2] * sinA;
matrix[1, 1] = gravityVector[1] * gravityVector[1] * oneMinusCosA + cosA;
matrix[1, 2] = gravityVector[1] * gravityVector[2] * oneMinusCosA - gravityVector[0] * sinA;
matrix[1, 0] = gravityVector[2] * gravityVector[0] * oneMinusCosA - gravityVector[1] * sinA;
matrix[1, 1] = gravityVector[2] * gravityVector[1] * oneMinusCosA - gravityVector[0] * sinA;
matrix[1, 2] = gravityVector[2] * gravityVector[2] * oneMinusCosA + cosA;
result = new double[3];
for (int i = 0; i < 3; i++)
{
result[i] = objectRotation[i] * matrix[i, 0] + objectRotation[i] * matrix[i, 1] + objectRotation[i] * matrix[i, 2];
Console.WriteLine(result[i]);
}
But I'm getting the wrong results. Can someone help me understand why or help me get to another solution?
Examples:
v1(1,0,0) v2(0,1,0) angle=90 -> vf(0,0,-1)
v1(sqrt2/2 , sqrt2/2 , 0) v2(-sqrt2/2 , sqrt2/2 , 0) angle = 90 -> vf(0,0,-1)
*OBS: I need that to apply for every vector and every angle, not just for 90 degrees (or pi/2 radians, doesn't matter) nor for those vectors that I used as example.
*OBS(2): I would be pleased if you could post the code in C# (preferred) or C++.
answering my own question, you can do the rotation by applying this formula:
vf = v1 * cosA + (v1 . v2) * v2 * (1 - cosA) + (v2 x v1) * sinA
And here is how to get to the formula:
f perpendicular to a, v1 and v2
*where p is the projection of v1 onto v2
p = (v1 . v2) * v2
a = v1 - p = v1 -(v1 . v2) * v2
f = (v2 x v1)/|(v2 x v1)| * |a|
a'= a * cos(θ) + f * sin(θ)
vf = a'+ p