«««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « » « SCENE ROTATE ALGORITHM » « » « 10.01.1998 » « » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « How to rotate 2D vector ? » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» V(x,y) = Vector to rotate Alfa = RotAngle X' = XCOSALFA - YSINALFA Y' = XSINALFA + YCOSALFA «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « Or we want to make rotate vector 2D matrix » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Rv = Rm * V Rm = Rotate matrix V = Vector to rotate (x,y) Rv = Rotated vector coords (x',y') Rm V Rv +- -+ +- -+ +- -+ | COSALFA -SINALFA | * | X | = | XCOSALFA+(-YSINALFA) XSINALFA+YCOSALFA | | SINALFA COSALFA | | Y | +- -+ +- -+ +- -+ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ X' Y' «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « How to rotate 3D vector ? » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» V(x,y,z) = Vector to rotate Alfa,Beta,Gamma = RotAngles ZX Rotate: X' = XCOSBETA - ZSINBETA Y' = Y Z' = XSINBETA + ZCOSBETA YZ Rotate: X' = X Y' = YCOSALFA - ZSINALFA Z' = YSINALFA + ZCOSALFA XY Rotate: X' = XCOSGAMMA - YSINGAMMA Y' = XSINGAMMA + YCOSGAMMA Z' = Z «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « Or we want to make rotate vector 3D matrixes » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Rm = Rotate matrix V(x,y,z) = Vector to rotate Alfa,Beta,Gamma = RotAngles ZX Rotate: RmZX +- -+ | COSALFA 0 -SINALFA | | 0 1 0 | | SINALFA 0 COSALFA | +- -+ YZ Rotate: RmYZ +- -+ | 1 0 0 | | 0 COSBETA -SINBETA | | 0 SINBETA COSBETA | +- -+ XY Rotate: RmXY +- -+ | COSGAMMA -SINGAMMA 0 | | SINGAMMA COSGAMMA 0 | | 0 0 1 | +- -+ And now, if we want to rotate vector on (for example) XY field we must multiple RmXY matrix with vector coords... V' = V * RmXY RmXY V +- -+ +- -+ | COSGAMMA -SINGAMMA 0 | | X | +- -+ | SINGAMMA COSGAMMA 0 | * | Y | =| XCOSGAMMA-YSINGAMMA XSINGAMMA+YCOSGAMMA Z | | 0 0 1 | | Z | +- -+ +- -+ +- -+ ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ ^ X' Y' Z' V'(x',y',z') If we want to rotate vector on two fields we must multiple vector coords with each field... V' = V * RmXY * RmZX - rotates vector on XY and ZX field. «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « LightWave compatibility » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» LightWave supports three angles: 1. Heading (h) 2. Pitch (p) 3. Bank (b) h = ZX field rotate angle p = YZ field rotate angle b = XY field rotate angle H = Heading rotate matrix P = Pitch rotate matrix B = Bank rotate matrix ZX Rotate: +- -+ | COS(h) 0 -SIN(h) | H = | 0 1 0 | | SIN(h) 0 COS(h) | +- -+ YZ Rotate: +- -+ | 1 0 0 | P = | 0 COS(p) -SIN(p) | | 0 SIN(p) COS(p) | +- -+ XY Rotate: +- -+ | COS(b) -SIN(b) 0 | B = | SIN(b) COS(b) 0 | | 0 0 1 | +- -+ But we must to remember: At first we multiple vector with B matrix after with P and at the end with H «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « Optimization » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» M = Rotate matrix H = Heading rotate matrix P = Pitch rotate matrix B = Bank rotate matrix M = B * P * H B P H +- -+ +- -+ +- -+ | COS(b) -SIN(b) 0 | | 1 0 0 | | COS(h) 0 -SIN(h) | M = | SIN(b) COS(b) 0 | * | 0 COS(p) -SIN(p) | * | 0 1 0 | | 0 0 1 | | 0 SIN(p) COS(p) | | SIN(h) 0 COS(h) | +- -+ +- -+ +- -+ «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» B P +- -+ +- -+ | COS(b) -SIN(b) 0 | | 1 0 0 | M1 = B * P = | SIN(b) COS(b) 0 | * | 0 COS(p) -SIN(p) | | 0 0 1 | | 0 SIN(p) COS(p) | +- -+ +- -+ +- -+ | COS(b)+0+0 0+[-SIN(b)*COS(p)]+0 0+[-SIN(b)*-SIN(p)]+0 | M1 = B * P = | SIN(b)+0+0 0+COS(b)*COS(p)+0 0+COS(b)*-SIN(p)+0 | | 0+0+0 0+0+SIN(p) 0+0+COS(p) | +- -+ +- -+ | COS(b) -SIN(b)*COS(p) SIN(b)*SIN(p) | M1 = B * P = | SIN(b) COS(b)*COS(p) -COS(b)*SIN(p)| | 0 SIN(p) COS(p) | +- -+ «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» M1 H +- -+ +- -+ | COS(b) -SIN(b)*COS(p) SIN(b)*SIN(p) | | COS(h) 0 -SIN(h) | M = M1 * H = | SIN(b) COS(b)*COS(p) -COS(b)*SIN(p) | * | 0 1 0 | | 0 SIN(p) COS(p) | | SIN(h) 0 COS(h) | +- -+ +- -+ +- -+ | COS(b)*COS(h)+0+SIN(b)*SIN(p)*SIN(h) 0+[-SIN(b)*COS(p)]+0 COS(b)*[-SIN(h)]+0+SIN(b)*SIN(p)*COS(h) | M = | SIN(b)*COS(h)+0+[-COS(b)*SIN(p)*SIN(h)] 0+COS(b)*COS(p)+0 SIN(b)*[-SIN(h)]+0+[-COS(b)*SIN(p)*COS(h)] | | 0+0+COS(p)*SIN(h) 0+SIN(p)+0 0+0+COS(p)*COS(h) | +- -+ +- -+ | COS(b)*COS(h)+SIN(b)*SIN(p)*SIN(h) -SIN(b)*COS(p) -COS(b)*SIN(h)+SIN(b)*SIN(p)*COS(h) | M = | SIN(b)*COS(h)-COS(b)*SIN(p)*SIN(h) COS(b)*COS(p) -SIN(b)*SIN(h)-COS(b)*SIN(p)*COS(h) | | COS(p)*SIN(h) SIN(p) COS(p)*COS(h) | +- -+ «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « Finished rotate matrix » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» M = Rotate matrix Sh = SIN (Heading) Sp = SIN (Pitch) Sb = SIN (Bank) Ch = COS (Heading) Cp = COS (Pitch) Cb = COS (Bank) +- -+ | Cb*Ch+Sb*Sp*Sh -Sb*Cp -Cb*Sh+Sb*Sp*Ch | M = | Sb*Ch-Cb*Sp*Sh Cb*Cp -Sb*Sh-Cb*Sp*Ch | | Cp*Sh Sp Cp*Ch | +- -+ «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « How to rotate point coords ? » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Po = Mo * P Po = Rotated coords Mo = Rotate matrix P = Point coords Mo P +- -+ +- -+ Po | A11 A12 A13 | | X | +- -+ | A21 A22 A23 | * | Y | = | A11X+A12Y+A13Z A21X+A22Y+A23Z A31X+A32Y+A33Z | | A31 A32 A33 | | Z | +- -+ +- -+ +- -+ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ X Y Z «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « How to set point in world coords ? » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» PPo = Po + Do Po = Rotated coords Do = Point position in world Po Do +- -+ +- -+ PPo | X | | PosX | +- -+ | Y | + | PosY | = | X+PosX Y+PosY Z+PosZ | | Z | | PosZ | +- -+ +- -+ +- -+ ^^^^^^ ^^^^^^ ^^^^^^ X Y Z «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « How to transpose matrix ? » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Mc = Camera coords rotate matrix Mc' = Camera coords rotate matrix (Transposed) Mc Mc' +- -+ +- -+ | A11 A12 A13 | | A11 A21 A31 | | A21 A22 A23 | Transpose | A12 A22 A32 | | A31 A32 A33 | | A13 A23 A33 | +- -+ +- -+ «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « How to set point in camera coords ? » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» PNo = ( Ppo - Dc ) PPo Dc +- -+ +- -+ PNo | X | | CamPosX | +- -+ | Y | - | CamPosY | = | X-CamPosX Y-CamPosY Z-CamPosZ | | Z | | CamPosZ | +- -+ +- -+ +- -+ ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^ X Y Z «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « How to rotate camera coords ? » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Pc = Mc' * PNo Mc' PNo +- -+ +- -+ Pc | A11 A21 A31 | | X | +- -+ | A12 A22 A32 | * | Y | = | A11X+A21Y+A31Z A12X+A22Y+A32Z A13X+A23Y+A33Z | | A13 A23 A33 | | Z | +- -+ +- -+ +- -+ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ X Y Z «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Pc = New positions of point coords +- -+ | X | | Y | = [X,Y,Z] | Z | +- -+ «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» « Optimization » «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» Po = Mo * P + Do Pc = Mc' * ( Po - Dc ) Pc = Mc' * [ ( Mo * P + Do ) - Dc ] Pc = Mc' * [ ( Mo * P ) + ( Do - Dc ) ] Pc = Mc' * ( Mo * P ) + Mc' * ( Do - Dc ) Pc = Mc' * Mo * P + Mc' * ( Do - Dc ) M = Mc' * Mo ;Calculated one time for object per frame D = Mc' * ( Do - Dc ) ;Calculated one time for object per frame Pc = M * P + D ;Calculated for each point per frame «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» INFORMER/APPENDIX Daniel Didyk ul.Bol.Chrobrego 18B/9 69110 Rzepin woj. gorzowskie POLAND e-mail: informer@opnt.optimus.wroc.pl «««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» PS: Big thanks to GAL for help !!! PS2: This is only idea how to code scenes but I don't tested it.