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 / ART53.SCR < prev    next >
Encoding:
Text File  |  1989-06-08  |  11.0 KB  |  211 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=3090>
  8. <bgcolor=-1>
  9. <background=0> 
  10. <link_color=000>
  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=3090 b=-1 c=-1>
  18.  
  19.  
  20.                       -- - -- -------------------------- 
  21. <f1><c000>               Math in games <f0>
  22.                       ----------------------- -- -- - --
  23.  
  24.           dirty tricks used since the Stone Age by game programmers
  25.                compiled by Saulot/ Nokturnal/ Chosneck Magazine
  26.  
  27.  
  28. NUMBERS
  29. ------------------
  30. Integers
  31. When  storing  objects  coordinates or values of other kind as integers we lack
  32. precision.  The following trick is handy when, during sprite animation, we want
  33. to  move given sprite less than one pixel per frame or move more than one pixel
  34. per  frame.  Do NOT use pauses in your code! All of this can be done by scaling
  35. the  number  system  you  are  using. For example you can divide the horizontal
  36. position by 100 before drawing the sprite on the screen. This forces the sprite
  37. to  be  drawn once per two frames. In this manner you can achieve wide range of
  38. velocities.
  39.  
  40. Fixed point math
  41. Dividing  of  the  integer by scaling ratio is rather CPU hungry operation. The
  42. same thing can be done in another way. If scaling ratio is the power of 2, then
  43. dividing process can be achieved by simple bit shifting operations. Fixed point
  44. numbers  are  simple  integers  which  represent both integer part and fraction
  45. separated  with binary point (exactly like decimal point, but in binary system)
  46. on the earlier specified, constant position. When programmers are talking about
  47. fast  integer  graphics,  they  often  have  in  mind fixed point numbers. Most
  48. frequently  used  powers of 2 are 256 and 65536, but others are also used. They
  49. can be very efficient on many systems. Naming convention of fixed point numbers
  50. covers  the  amount  of  bits for integer and fraction part separated by binary
  51. point:
  52. 12.4 - 12 bits of integer part
  53.        4 bits of fraction part
  54. This  gives  us  the  range (unsigned) of 0-4095.9375 with step 0.0625 (1/16 or
  55. 2^(-4)).
  56.  
  57. Addition  and  substraction  is  quite  simple.  We can operate on them like on
  58. integers  (they  only have to be compatible, that means that they must have the
  59. same  scaling  ratio).  If  we want to multiply the fixedpoint numbers, we must
  60. perform a logical shift to the right before any action.
  61.  
  62. As  a  good  introduction to fixed point math you should read the article about
  63. fast interpolation for m68k processors by Dynacore /TSCC.
  64.  
  65. Integers - flexible ranges
  66. One of major disadvantages when using the fixed point numbers is that they lack
  67. precision  in  wide  range  of numbers: they cannot hold very small or very big
  68. numbers  and  with very small numbers they are simply inaccurate. To get around
  69. this the floating point numbers are used. The floating point number consists of
  70. signed  exponent  which  describes the shift of the whole range and mantissa (a
  71. signed value).
  72. We can transform the floating point number to fixed point number by shifting of
  73. the  mantissa  by  a  amount  of  bits  specified  in exponent (to the left for
  74. positive  exponent  and  to  the  right  for  negative one). The floating point
  75. numbers  are supported in hardware by external, math coprocessors like MC68881,
  76. MC68882.  The  Motorola  68040  and 68060 (not all versions) possess their own,
  77. built-in,  internal  coprocessors.  The  use  of coprocessors on Atari's (maybe
  78. excluding  those  equipped with 68060) should be cautious, because their aren't
  79. lightning  fast and under some circumstances can slow the things down. But they
  80. are  invaluable  for  table's  precalculations  and they are very accurate. The
  81. programming  languages like C/C++ support float (32 bits), double (64 bits) and
  82. long  double  (80  bits).  The  machines  without coprocessors can also use the
  83. floating  point numbers, but all the operations are handled by software and not
  84. hardware, so it slows down everything considerably.
  85.  
  86. DISCRETENESS
  87. ---------------------------
  88. Discreteness  is  a  way of  talking  about  values  in computer games which in
  89. most cases  are integers. The  time  isn't  continuous,  it's divided on frames
  90. 1/50 per second  for  PAL  systems  (1/60  for  NTSC).  Sprite positions aren't
  91. continuous  too,  they   are   displayed   on  the  pixel  boundary.  Using  of
  92. float/fixed point numbers for storing  object  coordinates  means that they are
  93. stored with greater accuracy than  they   are  displayed on screen. When we are
  94. scrolling the whole screen, we have  to  stop  it's  movement, if this movement
  95. turns out to be too small (for example  less  than  1/8  pixel  per  frame)  to
  96. avoid flickering. To take into account   spatial discreetnes we should stop the
  97. screen scrolling rather in half of   pixel,   rather  than  on  the  edge. When
  98. the scrolling will start from the beginning, it will have the chance to move in
  99. both directions (in one or second way).
  100.  
  101. Temporal discreetness - game frames
  102. The conception of game frames is fundamental for traditional games programming.
  103. Of  course  there is possibility of writing asynchronous driven games, but most
  104. games depend on master clock's ticks occuring fixed amount of times per second.
  105. Each  tick,  the  game  executes  one cycle of thinking. Each object can move a
  106. little,  some  decisions  can  be made, the screen can be refreshed. Operations
  107. which  you  were  used  to  do  normally  in  one  go  have  to be performed in
  108. step-by-step  manner.  How  it  will be when you will "miss the frame" and game
  109. will  take  much more than 1 frame of game cycle? (in most cases it is te fault
  110. of  overloaded  graphics  routines). How to catch-up and avoid the game slowing
  111. down?  There  are  two  methods:  launching  the  game engine several times and
  112. scaling of game speed.
  113.  
  114. The  first  method  is  quite simple - you measure the amount of missed frames,
  115. then  launch  the  game code adequate amount of frames to catch things up. This
  116. works  fine,  because game routines are much quickier than routines responsible
  117. for  display. If game code is very complex or game works on a slow machine this
  118. method won't work. Solution like this is used succesfully on consoles.
  119.  
  120. Next  method  (it works on slower machines) is based on measuring how long each
  121. frame will take and multiplying the speed of each game by adequate number. This
  122. method  is  used  on  PCs,  that's  because they vary between themselves in CPU
  123. speed.
  124.  
  125. In  modern  games,  the  game  frame isn't the same as dispaying frame. Game is
  126. going  on  in  a  thread  separated  from  displaying  code  and in this manner
  127. shouldn't  be  affected  by  displaying  code, slowed down by too big amount of
  128. objects  or  too complex scene. To track down the eventual problems a different
  129. approach  is  needed,  similar  to  launching game engine several times, but it
  130. requires  a  lot of additional thought. Shared variables, those written by game
  131. code  and  read  by  part  responsible for display should be double buffered to
  132. wipe-out problems with synchronisation.
  133.  
  134. GEOMETRY
  135. --------
  136. Classical  geometry  or  Euclidean geometry is based on right triangle and it's
  137. angles.  In  following  examples the theta will describe the main angle and phi
  138. any other angles. There are two vertices called A and B. There are three sides:
  139. 2 x adjacent, and the longest side - hypotenuse.
  140.  
  141.          *
  142.          |/  *
  143.          | phi    *
  144.          |            *
  145.          |                 *
  146.          |.) 90ן        theta / *
  147.          +------------------------*
  148.  
  149.          theta + phi + 90ן = 180ן
  150.          phi = 90ן - theta
  151.  
  152. That means that one angle can be calculated from the other. We can be specified
  153. by the lenght of one or two sides. That leads us to two systems of coordinates:
  154. polar and carthesian. (about them later)
  155.  
  156. TRIGONOMETRY - those sinuses and cosinuses
  157. What does it mean "trigonometric functions" ? Simply speaking they are ratio of
  158. right-triangle  sides  near/  opposite to specified angle. Calculating of those
  159. functions  is  quite  CPU time consuming task (it's stupid dividing after all),
  160. then  the  best solution is to precalculate and store all the values in look-up
  161. table.  Those  functions  are  becoming  handy when translating between classic
  162. carthesian and polar coordinates.
  163.  
  164. Distance calculation
  165. Pythagorean  theorem tells that square of hypotenuse is equal to sum of squares
  166. of  legs  of  right  triangle.  It  is  very  useful in computer games, because
  167. accurate calculation of distance between two points is possible.
  168.  
  169. Let's  say  that we have object A at (x1,y2) and object B at (x2,y2). According
  170. to    Pythagorean    theorem,  the  distance  between  A  and  B  is  equal  to
  171. ((x2-x1)²+(y2-y1)²). If you know how to calculate square roots then this method
  172. is  well  suited  for you. Specifying of the angle between two points is a real
  173. nightmare,  because there are four possibilities depending in which quarter the
  174. adequate angle is located. If you know the angle thetha (e.g. you calculated it
  175. earlier),  you can calculate the distance between two points by dividing of the
  176. adjacent  side  (near  thetha  angle) by cos(thetha)(if thetha<45ן) or dividing
  177. lenght  of  right  triangle's  leg (opposite to thetha angle) by sin(thetha)(if
  178. thetha>45ן).
  179.  
  180. Systems of coordinates
  181. Concept  of  space  in  computer  games  requires  numeric  measure for marking
  182. position and directions. There are many solutions, but few of them are used.
  183.  
  184. Carthesian coordinates
  185. It's  most  frequently  used  system.  Carthesian coordinates consist of values
  186. horizontal  (X), vertical (Y) or for 3D games depth (Z axis). Between each axis
  187. there  is  an  90ן  angle.  Coordinates can be represented as a vector [X,Y,Z],
  188. which is displayed horizontally while using it to multiplying matrixes.
  189.  
  190. Polar coordinates
  191. Polar  coordinates  consist  of angle and scalar value. They can be useful when
  192. storing the velocities of objects in 2D games (in sport games for example). The
  193. person  can run in specified direction and change the angle by specified amount
  194. in  each frame, maintaining the speed at constant level. We can transform polar
  195. coordinates  to  carthesian system by multiplying scalar by sinus or cosinus of
  196. negated  angle  to  get proper horizontal and vertical coordinates. (angle 0 is
  197. North,  90 is EAST). Angles can be stored in different formats. Circle consists
  198. of 2*pi radians. Sailors use 360 degrees, french dictators 400 grad, and coders
  199. 256 units, because it fits into one byte.
  200.  
  201. Quaternions 3D rotations that work
  202. Quaternions  are  4  dimensional  numbers  (exactly  like complex numbers are 2
  203. dimensional).  They  have  a  set of mathematical operations tied only for them
  204. like  addition,  substraction,  multiply  etc.  They  are  very  useful with 3D
  205. rotations.  In  "Descent"  for example you can rotate around axis, no matter in
  206. what direction you face. The quaternions let you do things like that.
  207.  
  208. <link=art53b.scr>Go to NEXT PART</l>
  209. </frame>
  210. </body>
  211.