home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / CHOSNECK / CHOS2.ZIP / CHOSNECK.2ND / STUFF / DATAS.ZIP / ART47.SCR < prev    next >
Encoding:
Text File  |  2003-12-08  |  7.9 KB  |  245 lines

  1. <head>
  2. <title="...forever...">
  3. <font=monaco10.fnt>
  4. <font=newy36.fnt>
  5. <font=time24.fnt>
  6. <image=back.raw w=256 h=256 t=-1>
  7. <buf=3647>
  8. <bgcolor=-1>
  9. <background=0> 
  10. <link_color=253>
  11. <module=console.mod>
  12. <pal=back.pal>
  13. colors:
  14. 251 - black
  15. </head>
  16. <body>
  17. <frame x=0 y=0 w=640 h=3647 b=-1 c=-1>
  18.  
  19.  
  20. - -- - ------------------------------------------------------------------------    
  21. <f1><c000>                3D Rotations <f0>
  22. <f1><c000>       in Mathematics & Demos <f0>
  23. --------------------------------------------------------------------- ---- -- -
  24.  
  25. Strange title, isn't it? :-) I  just  finished  my exploration of matrices & 3D
  26. (yeah, after 3/4 year of using it  :-))  so  why  not to write some lines about
  27. it...
  28.  
  29.  
  30. Everyone knows it
  31. =================
  32.  
  33. We want to rotate a point in 2D space (at first) :
  34.  
  35. new x := x*cos(phi) - y*sin(phi)
  36. new y := x*sin(phi) + y*cos(phi)
  37.  
  38. That means if we have a point  in Carthesian Coordinate System (CCS), our point
  39. with coordinates (x,y)  will  be  anticlockwise  rotated  around  (0,0) in this
  40. system. Graphically:
  41.  
  42.       ^ +y
  43.       |
  44.       |
  45.       |   . <- rotated point with new (x,y)
  46.       |  /
  47.       | / _. <- point (x,y)
  48.       |/--
  49. --------------> +x
  50.       |(0,0)
  51.       |
  52.       |
  53.       |
  54.       |
  55.       |
  56.       |
  57.  
  58. Clear. But! As you probably know,  computer  screen  doesn't look like this. We
  59. can  do  two  things  if  we   want  correct  rotations:  "convert"  carthesian
  60. coordinates to computer ones or derive new rotation formulas.
  61.  
  62.  
  63.  
  64. 1. Carthesian vs Computer Coordinates
  65. =====================================
  66.  
  67. Our situation looks like this:
  68.  
  69.  
  70. (0,0)
  71.   -----------> +x
  72.   |\--_. <- point (x,y)
  73.   | \
  74.   |  \
  75.   |   \. <- rotated point with new (x,y)
  76.   |
  77.   |
  78.   |
  79.   |
  80.   v +y
  81.  
  82.  
  83.  
  84. We see two differences: y-direction and anti/clock- wise rotation.
  85.  
  86.  
  87.  
  88. In 2D, both differences can be solved by a simple equation y := (yres-1) - y
  89.  
  90. In 3D, it isn't so easy. Remember  we're rotating around 3 axes, firstly around
  91. Z (gama angle), then Y (beta) and finally X (alpha)
  92.  
  93.  
  94.  
  95.      ( +cosG +sinG 0 )       ( +cosB 0 +sinB )       ( 1 +cosA +sinA )
  96. MZ = ( -sinG +cosG 0 )  MY = (   0   1   0   )  MX = ( 0 -sinA +cosA )
  97.      (   0     0   1 )       ( -sinB 0 +cosB )       ( 0   0     0   )
  98.  
  99.  
  100.  
  101. So, we derive our new point by new [P] = [x y z] * MZ*MY*MX, right?
  102.  
  103.  
  104.  
  105. In my short  coder's  life  I've  seen  as  many  rotation  matrices as sources
  106. released by coders =) After multiplying you will get:
  107.  
  108.  
  109.  
  110.     ( +cosBcosC +cosAsinC-sinAsinBcosC +sinAsinC+cosAsinBcosC )
  111.     ( -cosBsinC +cosAcosC+sinAsinBsinC +sinAcosC-cosAsinBsinC )
  112.     ( -sinB     -sinAcosB              +cosAcosB              )
  113.  
  114.  
  115.  
  116. Believe me, this is the  one  and  only  correct  solution :-) BUT. This matrix
  117. works in CCS, but not in computer  space.  So,  what's wrong? Let's look at our
  118. two problems:
  119.  
  120.  
  121. a) y-direction: here there aren't any changes, we use y := (yres-1) - y
  122.  
  123. b) if you run your 3D engine now,  you'll get the feeling everything is alright
  124. (as I did for about 3/4 year ;-))  But  there's that clockwise problem - if our
  125. engine has to be correct (that means it respects a lefthand rule since we're in
  126. lefthanded CCS = z-axis goes away from us)
  127.  
  128. a rotation around y-axis has to switch to another direction:
  129.  
  130.  
  131.       ^ +z
  132.       |
  133.       |
  134.       |   /
  135.       |  /  . <- out rotated point (we're breaking the lefthand rule)
  136.       | /
  137.       |/___--. <- our point (x,y)
  138. ------*-------> +x
  139.      /|
  140.     / |
  141.    /  |
  142.   /   |
  143.  /+y  |
  144.  
  145.  
  146. So, we need to change from anticlockwise  to  clockwise rotation.. How do we do
  147. it? Simply ;) Just imagine how we define x  & y coordinates on circle in 2D. If
  148. we're going  "up"  with  our  point,  the  y-value  is  increased.  This  is an
  149. anticlockwise rotation. And we  want  a  clockwise  one.  And  its as simple as
  150. negating the y coordinate! And  y  coordinate  =  r*sin(phi) -> every sin(beta)
  151. becomes -sin(beta). Remember "beta" is an angle we're rotating around y-axis.
  152.  
  153.  
  154. Our matrix becomes:
  155.  
  156.  
  157.     ( +cosBcosC +cosAsinC+sinAsinBcosC +sinAsinC-cosAsinBcosC )
  158.     ( -cosBsinC +cosAcosC-sinAsinBsinC +sinAcosC+cosAsinBsinC )
  159.     ( +sinB     -sinAcosB              +cosAcosB              )
  160.  
  161.  
  162. And our rotation will look like:
  163.  
  164.  
  165.     [rotate point using matrix above]
  166.     [calculate projected coordinates]
  167.     [center on screen: x := x + xres/2; y := (yres/2 - 1) -y ]
  168.     [write pixel to screen]
  169.  
  170.  
  171. Now you've got a mathematically correct  rotation  in  2D and 3D... but there's
  172. one stupid thing and that is a  need  of one more instruction in the projection
  173. loop - you can't do for example "119 - y" in one instruction since you need the
  174. value 119 for the next use => at  least two instructions are needed, that's bad
  175. if you want to  rotate  about  1000  points...  ok,  DSP  has  a  +/- option in
  176. multiplying, but the solution above is a bit stupid ;-)
  177.  
  178. A better solution is :
  179.  
  180.  
  181. 2. Deriving New Rotation Formulas
  182. ===================================
  183.  
  184. What does this mean? When I began with 3D stuff I thought programs like Neon or
  185. 3DS use CCS instead of computer screen for calculating their y-coordinates. But
  186. this isn't true of course -  why  give  coders  some additional work? :-) So we
  187. have some 3d objects saved in a computer  space system and we want to rotate it
  188. in mathematically correct way (ie. not breaking the lefhand-rule)
  189.  
  190.  
  191. When you try to use original matrix with:
  192.  
  193. x := x + xres/2
  194. y := y + yres/2
  195.  
  196. you'll get quite strange result ;-) Where's the problem? The original matrix is
  197. "built" for CCS. And... the difference  between  CCS  and computer screen is in
  198. the y coordinate.  In  the  sign  of  y  coordinate.  And  what  affects the y-
  199. coordinate in 3D? Rotation around x & z axis.  So the only thing you have to do
  200. is negate the sin(alpha) and sin (gama) values:
  201.  
  202.  
  203.     ( +cosBcosC -cosAsinC+sinAsinBcosC +sinAsinC+cosAsinBcosC )
  204.     ( +cosBsinC +cosAcosC+sinAsinBsinC -sinAcosC+cosAsinBsinC )
  205.     ( -sinB     +sinAcosB              +cosAcosB              )
  206.  
  207.  
  208. So people, this is the one and only matrix for correct rotation in a lefthanded
  209. coordinate system ! For sure, we're in the system which looks like this:
  210.  
  211.  
  212.        |                   ^ +x          ^ +z              ^ +y
  213.        |                   |             |                 |
  214.        |    /|+z           |             |                 |
  215.        |   /               |   /| +z     |   /| +y         |   /| +x
  216.        |  /                |  /          |  /              |  /
  217.        | /                 | /           | /               | /
  218.        |/                  |/            |/                |/
  219. -------*-------> +x        *-----> +y    *-----> +x        *-----> +z
  220.       /|
  221.      / |                anticlockwise    anticlockwise    anticlockwise
  222.     /  |                rotation around  rotation around  rotation around
  223.    /   |                z-axis           y-axis           x-axis
  224.   /    |
  225.        v +y
  226.  
  227.  
  228. And here it is... you see  all  rotations  are correct, the lefthand rule stays
  229. unbroken, simply... this is what I like  ;-)  But  beware! If you use this way,
  230. your  vertices  will  be  swapped  in  comparison  with  classic  "conversion"!
  231. (clockwise->anticlockwise and vice versa) So  you  need to modify your backface
  232. culling routine (ble -> bge) and maybe  some  other things (for example, if you
  233. use realtime interpolation, right and left side  will be swapped since the next
  234. point in anticlockwise order = the  previous  point in clockwise one) So, don't
  235. be stupid and use this way from  the  start  and  you'll save yourself a lot of
  236. work ;-)
  237.  
  238.                                                                      MiKRO
  239.  
  240. -------------------------------------------------------------------------------
  241.   mikro@hysteria.sk          XE/XL/MegaSTE/Falcon060          mikro.atari.org
  242. -------------------------------------------------------------------------------
  243. </frame>
  244. </body>
  245.