PDA

View Full Version : [SOLVED]Rotate point around vector?


Alka
10-24-2014, 18:45
I have 2 points origins(P1 and P2) and a third point(P3) that is at distance N from P2. Now what i want to make is to rotate P3 around P2 at N distance by M angles. Made a lot of researches and searches but didn't understand so much because of my lack of maths ;s if anyone around knows an answer then thank you. :bacon:

Some useful info? http://ami.ektf.hu/uploads/papers/finalpdf/AMI_40_from175to186.pdf

Dr. Greg House
10-24-2014, 19:44
Are we sticking to the example which is 2d, or do you need a 3d solution?

EDIT:
Oh dear, I didn't see the z-axis in the graph, yet wondered why the circle was so skewed.

You're going to have to define a rotation matrix, subtract p2 from p3, rotate it by multiplying it with the matrix, then readd p2.
And yes, the pdf is what you need.
Any specific issues we might help you with?

Alka
10-25-2014, 01:53
EDIT: Solution found, there was a mistake, where rotation[0][3] / [1][3] / [2][3] was initially 0 because vector supposed to start from (0,0,0). Replaced those with last column from rotation matrix
http://inside.mines.edu/fs_home/gmurray/ArbitraryAxisRotation/ArbitraryAxisRotation14x.png
where a,b,c = P1 and now works as intended. Also angle conversion wasn't needed.
I'll leave code here in case someone will eventually need this somewhere? Thank you for your suggestion Greg :wink:



float newp[3];
float rotation[4][4];
SetupMatrix(180.0, vec, rotation, back);
MultiplyMatrix(nright, rotation, newp);

//----------------------------------

stock void MultiplyMatrix(float input[3], float rotation[4][4], float output[3])
{
float input2[4];
input2[0] = input[0];
input2[1] = input[1];
input2[2] = input[2];
input2[3] = 1.0;

float output2[4];
for(int i = 0 ; i < 4 ; i++)
{
for(int j = 0 ; j < 4 ; j++)
{
output2[i] += rotation[i][j] * input2[j];
}
}

output[0] = output2[0];
output[1] = output2[1];
output[2] = output2[2];
}

stock void SetupMatrix(float angle, float vector[3], float rotation[4][4], float point[3])
{
float L = (vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
//angle = angle * M_PI / 180.0;
float u2 = vector[0] * vector[0];
float v2 = vector[1] * vector[1];
float w2 = vector[2] * vector[2];

PrintToChatAll("Debug(): L = %.1f", L);

rotation[0][0] = (u2 + (v2 + w2) * Cosine(angle)) / L;
rotation[0][1] = (vector[0] * vector[1] * (1 - Cosine(angle)) - vector[2] * SquareRoot(L) * Sine(angle)) / L;
rotation[0][2] = (vector[0] * vector[2] * (1 - Cosine(angle)) + vector[1] * SquareRoot(L) * Sine(angle)) / L;
//rotation[0][3] = 0.0;
rotation[0][3] = ((point[0] * (v2 + w2) - vector[0] * (point[1] * vector[1] + point[2] * vector[2])) * (1 - Cosine(angle)) + (point[1] * vector[2] - point[2] * vector[1]) * SquareRoot(L) * Sine(angle)) / L;

rotation[1][0] = (vector[0] * vector[1] * (1 - Cosine(angle)) + vector[2] * SquareRoot(L) * Sine(angle)) / L;
rotation[1][1] = (v2 + (u2 + w2) * Cosine(angle)) / L;
rotation[1][2] = (vector[1] * vector[2] * (1 - Cosine(angle)) - vector[0] * SquareRoot(L) * Sine(angle)) / L;
//rotation[1][3] = 0.0;
rotation[1][3] = ((point[1] * (u2 + w2) - vector[1] * (point[0] * vector[0] + point[2] * vector[2])) * (1 - Cosine(angle)) + (point[2] * vector[0] - point[0] * vector[2]) * SquareRoot(L) * Sine(angle)) / L;

rotation[2][0] = (vector[0] * vector[2] * (1 - Cosine(angle)) - vector[1] * SquareRoot(L) * Sine(angle)) / L;
rotation[2][1] = (vector[1] * vector[2] * (1 - Cosine(angle)) + vector[0] * SquareRoot(L) * Sine(angle)) / L;
rotation[2][2] = (w2 + (u2 + v2) * Cosine(angle)) / L;
//rotation[2][3] = 0.0;
rotation[2][3] = ((point[2] * (u2 + v2) - vector[2] * (point[0] * vector[0] + point[1] * vector[1])) * (1 - Cosine(angle)) + (point[0] * vector[1] - point[1] * vector[0]) * SquareRoot(L) * Sine(angle)) / L;

rotation[3][0] = 0.0;
rotation[3][1] = 0.0;
rotation[3][2] = 0.0;
rotation[3][3] = 1.0;
}