0

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++.

6
  • If you use an appropriate maths library, there should be a function to create a matrix from an axis and angle. Just multiply this matrix with the vector. If not, Wikipedia has the formulae. Commented Apr 12, 2014 at 8:36
  • I'm afraid that the required mathematics cannot be explained to a 5-year old child. Even though it is particularly beautiful mathematics, it is far from trivial. If you are prepared to try to tackle it anyway, check out this wikipedia article: en.wikipedia.org/wiki/Quaternions_and_spatial_rotation Commented Apr 12, 2014 at 8:55
  • This question appears to be off-topic because it cannot be answered the way it is posed. Commented Apr 12, 2014 at 8:56
  • I tried to use some of the matrixes that wikipedia and other sites suggested but no one seems to work. I must be doing something wrong but I can't figure out what. Commented Apr 12, 2014 at 16:25
  • @cmaster btw, when I asked to be explained like if it was a 5-year old child, I meant not to simply post the answer without explaining anything. Sorry, my mistake. Commented Apr 12, 2014 at 16:34

1 Answer 1

1

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:

basic

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
Sign up to request clarification or add additional context in comments.

1 Comment

what programing language it is? Could you rewrite it to c# . I suppose there is vector scale, and vector add / substract function , as well as dot and cross products are used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.