Circular rotation around an arbitrary axis

I program Starcraft 2 custom maps and get some maths programs in 3D. I'm currently trying to create and rotate a point around an arbitrary axis given by x, y and z (the xyz vector is normalized).

I tried a lot and read a lot of things on the Internet, but I just can not understand how it works correctly. My current script (you probably don't know the language, but it's nothing special) is the result of breaking for only a few hours (not working correctly):

    point CP;
fixed AXY;
point D;
point DnoZ;
point DXY_Z;
fixed AZ;
fixed LXY;
missile[Missile].Angle = (missile[Missile].Angle + missile[Missile].Acceleration) % 360.0;
missile[Missile].Acceleration += missile[Missile].AirResistance;
if (missile[Missile].Parent > -1) {
    D = missile[missile[Missile].Parent].Direction;
    DnoZ = Point(PointGetX(D),0.0);
    DXY_Z = Normalize(Point(SquareRoot(PointDot(DnoZ,DnoZ)),PointGetHeight(D)));
    AZ = MaxF(ACos(PointGetX(DXY_Z)),ASin(PointGetY(DXY_Z)))+missile[Missile].Angle;
    DnoZ = Normalize(DnoZ);
    AXY = MaxF(ACos(PointGetX(DnoZ)),ASin(PointGetY(DnoZ)));
    CP = Point(Cos(AXY+90),Sin(AXY+90));
    LXY = SquareRoot(PointDot(CP,CP));
    if (LXY > 0) {
        CP = PointMult(CP,Cos(AZ)/LXY);
        PointSetHeight(CP,Sin(AZ));
    } else {
        CP = Point3(0.0,0.0,1.0);
    }
} else {
    CP = Point(Cos(missile[Missile].Angle),Sin(missile[Missile].Angle));
}
missile[Missile].Direction = Normalize(CP);
missile[Missile].Position = PointAdd(missile[Missile].Position,PointMult(missile[Missile].Direction,missile[Missile].Distance));

I just can't figure out the math. If you could explain it in plain language, which would be the best solution, then the code would be very useful (but not quite as useful, because I plan to make more 3D materials in the future).

+11
6

http://en.wikipedia.org/wiki/Rotation_matrix. . . . theta - , uux, uy uz - x, y z

Here's the rotation matrix

, , .

+19

quaternions. , Gimbal lock.

, , ( ). , , .

:

, (a,b,c) , , . , .

- , , , ( ) . "", :

M M' = I

'. , .

, . , ai + bj + ck, . , - ( ?) .

, ( ) " ". , , , ( "", ) . 1, -1. , ( ) , , ( 1 ).

, ( !). , :

u = ai + bj + ck

"" , k ( z). , z; 2x2 x, y :

       cos(r) sin(r)   0
M  =  -sin(r) cos(r)   0
         0      0      1

, "" , u k, , ( ), . , R , u k, R ' k u, :

R' M R

, u, u :

R' M R u = R' M k = R' k = u

, u.

, Q Q ' "" u. , , , , .

, . , , . , , . "" 1843 :

[ ] http://www-gap.dcs.st-and.ac.uk/~history/Mathematicians/Hamilton.html

3D- .

q = q0 + q1*i + q2*j + q3*k :

     (q0² + q1² - q2² - q3²)      2(q1q2 - q0q3)          2(q1q3 + q0q2)

Q  =      2(q2q1 + q0q3)     (q0² - q1² + q2² - q3²)      2(q2q3 - q0q1)

          2(q3q1 - q0q2)          2(q3q2 + q0q1)     (q0² - q1² - q2² + q3²)

, Q - , .. Q Q' = I, , Q . , , 1:

(q0² + q1² - q2² - q3²)² + 4(q1q2 - q0q3)² + 4(q1q3 + q0q2)²

  = (q0² + q1² - q2² - q3²)² + 4(q1q2)² + 4(q0q3)² + 4(q1q3)² + 4(q0q2)²

  = (q0² + q1² + q2² + q3²)²

  =  1

:

  [ (q0² + q1² - q2² - q3²), 2(q1q2 - q0q3), 2(q1q3 + q0q2) ]

   * [ 2(q2q1 + q0q3), (q0² - q1² + q2² - q3²), 2(q2q3 - q0q1) ]

 = 2(q0² + q1² - q2² - q3²)(q2q1 + q0q3)

   + 2(q1q2 - q0q3)(q0² - q1² + q2² - q3²)

   + 4(q1q3 + q0q2)(q2q3 - q0q1)

 = 4(q0²q1q2 + q1²q0q3 - q2²q0q3 - q3²q2q1)

   + 4(q3²q1q2 - q1²q0q3 + q2²q0q3 - q0²q2q1)

 =  0

, det(Q) = 1, , , Q .

Q ? ? , r :

u = ai + bj + ck

, :

q = cos(r/2) + sin(r/2) * u

  = cos(r/2) + sin(r/2) ai + sin(r/2) bj + sin(r/2) ck

,

q0 = cos(r/2), q1 = sin(r/2) a, q2 = sin(r/2) b, q3 = sin(r/2) c,

, Q "" u:

Q u = u

, , .

u = 0i + 0.6j + 0.8k - , r = pi - .

:

q = cos(pi/2) + sin(pi/2) * u

  = 0 + 0i + 0.6j + 0.8k

:

        -1     0     0

Q =      0  -0.28  0.96

         0   0.96  0.28

, Q Q '= det (Q) = 1.

, :

Q u = [ 0, -0.28*0.6 + 0.96*0.8, 0.96*0.6 + 0.28*0.8 ]'

    = [ 0, 0.6, 0.8 ]'

    =  u

. u , "" Q.

, , pi ( 180 ), , Q , :

i + 0j + 0k,  or as a vector, [ 1, 0, 0 ]'

Q [ 1, 0, 0 ]' = [-1, 0, 0 ]', [1, 0, 0 ] ' pi u.

( , ), . :

[ ] http://gandalf-library.sourceforge.net/tutorial/report/node125.html

r u = ai + bj + ck [a, b, c] ', :

q0 = cos(r/2),  q1 = sin(r/2) a,  q2 = sin(r/2) b,  q3 = sin(r/2) c

:

     (q0² + q1² - q2² - q3²)      2(q1q2 - q0q3)          2(q1q3 + q0q2)

Q  =      2(q2q1 + q0q3)     (q0² - q1² + q2² - q3²)      2(q2q3 - q0q1)

          2(q3q1 - q0q2)          2(q3q2 + q0q1)     (q0² - q1² - q2² + q3²)

Q , :

Q u = u
+10

3D-, , . :

.

Point of Rotation = (X1, Y1, Z1)
Point Location    = (X1+A, Y1+B, Z1+C)

(Point Location - Point of Rotation) = (A, B, C).

Z.

    A' = A*cos ZAngle - B*sin ZAngle
    B' = A*sin ZAngle + B*cos ZAngle
    C' = C.

Y.

    C'' = C'*cos YAngle - A'*sin YAngle
    A'' = C'*sin YAngle + A'*cos YAngle
    B'' = B'   

X.

    B''' = B''*cos XAngle - C''*sin XAngle
    C''' = B''*sin XAngle + C''*cos XAngle
    A''' = A''

, .

Rotated Point = (X1+A''', Y1+B''', Z1+C''');

. , X, Y Z.

:

enter image description here

+4

, x, y z. Rx, Ry Rz x, y, z .

enter image description here

+2

. () /. , (x, y, z) (a, b, c) ⟨u, v, w⟩ tta.

rotation matrix around an arbitrary axis

The result is this point in three dimensions:

rotated point

The page contains a link to download the source code. If you want to interactively perform rotations, you can do this on this site . Try the link to rotate the sample to see what happens.

Twist and Shout!

0
source

Python implementation worked for me.

cos180=-1
sin180=0

rotmatrix=np.zeros((3,3))
rotmatrix[0][0]=cos180 + rotaxis[0]**2 * (1-cos180)
rotmatrix[0][1]=rotaxis[0]*rotaxis[1] * (1-cos180) - rotaxis[2] * sin180
rotmatrix[0][2]=rotaxis[0]*rotaxis[2] * (1-cos180) + rotaxis[1] * sin180

rotmatrix[1][0]=rotaxis[1]*rotaxis[0] * (1-cos180) + rotaxis[2] * sin180
rotmatrix[1][1]=cos180 + rotaxis[1]**2 * (1-cos180)
rotmatrix[1][2]=rotaxis[1]*rotaxis[2] * (1-cos180) - rotaxis[0] * sin180

rotmatrix[2][0]=rotaxis[2]*rotaxis[0] * (1-cos180) - rotaxis[1] * sin180
rotmatrix[2][1]=rotaxis[2]*rotaxis[1] * (1-cos180) + rotaxis[0] * sin180
rotmatrix[2][2]=cos180 + rotaxis[2] ** 2 * (1-cos180)
-2
source

Source: https://habr.com/ru/post/1534136/


All Articles