home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1990 / USERSP90.MSA / TEXT_STOS.DOC < prev    next >
Text File  |  1990-07-25  |  9KB  |  206 lines

  1.                               One over the eight
  2.  
  3.  
  4.            Phil Lawson's STOS series continues to manipulate the
  5.            humble sprite and gets it bouncing in eight directions
  6.  
  7. Last month we discovered how to bounce a sprite around a simple maze in four
  8. directions, namely up, down, left and right. This month we'll expand these
  9. concepts to cover the other four diagonal directions. If you'd rather examine
  10. my code to work it out yourself, you'll find the program and Degas picture on
  11. this cover disk.
  12.  
  13.      You should remember Table I from last month, which was used to calculate
  14. the new direction of a ball after it had struck an obstacle. If you don't have
  15. that issue, order a back copy now as we'll be using the points discussed quite
  16. often in future issues.
  17.  
  18.                      original direction
  19.               left(1)   up(2)   right(3)   down(4)
  20.          1      2        0         0          3
  21. obtained 2      4        3         0          0
  22. value    3      0        1         4          0
  23.          4      0        0         2          1
  24.  
  25. Table I: Cross referencing the obtained value with the original direction.
  26.  
  27.      These values were stored in a 4X4 array, which could be quickly accessed
  28. to set the new direction. If you recall, we defined four points around the
  29. centre of the sprite and tested the background colour under these to determine
  30. what type of obstacle was hit. This was fine because there were only four
  31. possible obstables and four directions. Now however, there are eight of each
  32. which means a slight re-think in required, not to mention a bigger array.
  33.  
  34.      Since only one of the four points could have been set at any one time, we
  35. just checked which one it was, ( one to four ) and used that as one of the
  36. array parameters. Unfortunately with eight directions there will be either two
  37. or three points set, so how do we combine these to produce one number which can
  38. be passed to the array?
  39.  
  40.      Consider the binary number 0000. Each bit represents a different power of
  41. 2, namely 8,4,2 and 1. If a bit is set we simply add its corresponding number
  42. to a total to find what the original binaray number represent in our normal
  43. denary system. For example, if the number was 1011, we'd add togeather 8,2 and
  44. 1 to obtain a total of eleven.
  45.  
  46.      If we made each of the four points correspond to one of the bits, we could
  47. check whether that point was set and if so set the appropiate bit, as shown in
  48. lines 2030 to 2060 of this months program:
  49.  
  50.  
  51.    10 REM     Demonstration of 8-way bouncing
  52.    11 REM by Phil Lawson for Atari ST User (c) 1990
  53.    12 REM *****************************************
  54.    20 MODE 0 : KEY OFF : HIDE : CURS OFF
  55.    30 UNPACK 5,PHYSIC : WAIT VBL : UNPACK 5,BACK : WAIT VBL
  56.    40 GOSUB 500
  57.    50 GOSUB 1000
  58.    60 STOP
  59.    500 REM Initialisation
  60.    501 REM **************
  61.    510 DIM b(14,8),dx(8),dy(8),sx(6),sy(6),d(6)
  62.    520 RESTORE 600 : FOR a=1 TO 8 : READ dx(a),dy(a) : NEXT a
  63.    540 RESTORE 700 : FOR a=1 TO 14 : FOR c=1 TO 8 : READ b(a,c) : NEXT c :
  64.    NEXT a
  65.    550 RESTORE 800 : FOR a=1 TO 6 : READ sx(a),sy(a) : NEXT a
  66.    560 RESTORE 820 : FOR a=1 TO 6 : READ d(a) : NEXT a
  67.    599 RETURN
  68.    600 DATA-1,0,-1,-1,0,-1,1,-1,1,0,1,1,0,1,-1,1
  69.    700 DATA 0,0,0,0,0,0,0,0
  70.    705 DATA 0,0,0,0,0,0,0,0
  71.    710 DATA 5,4,0,0,0,0,0,6
  72.    715 DATA 0,0,0,0,0,0,0,0
  73.    720 DATA 0,0,0,0,0,0,0,0
  74.    725 DATA 0,8,7,6,0,0,0,0
  75.    730 DATA 7,6,5,0,0,0,0,0
  76.    735 DATA 0,0,0,0,0,0,0,0
  77.    740 DATA 0,0,0,0,0,4,3,2
  78.    745 DATA 0,0,0,0,0,0,0,0
  79.    750 DATA 3,0,0,0,0,0,5,4
  80.    755 DATA 0,0,0,2,1,8,0,0
  81.    760 DATA 0,0,0,0,3,2,1,0
  82.    765 DATA 0,0,1,8,7,0,0,0
  83.    790 REM The sprite starting positions
  84.    791 REM *****************************
  85.    800 DATA 50,32,40,127,64,127
  86.    810 DATA 180,34,178,125,222,174
  87.    818 REM The initial directions of each sprite
  88.    819 REM *************************************
  89.    820 DATA 5,2,8,5,7,1
  90.    1000 REM Position the balls and start bouncing them
  91.    1001 REM ******************************************
  92.    1010 FOR a=1 TO 6 : SPRITE a,sx(a),sy(a),1 : NEXT a
  93.    1020 WHILE INKEY$=""
  94.    1030 FOR a=1 TO 6
  95.    1040 sx(a)=sx(a)+dx(d(a)) : sy(a)=sy(a)+dy(d(a))
  96.    1050 SPRITE a,sx(a),sy(a),1 : UPDATE
  97.    1060 IF DETECT(a)=1 THEN GOSUB 2000
  98.    1070 NEXT a
  99.    1080 WEND
  100.    1090 RETURN
  101.    2000 REM Change direction
  102.    2001 REM ****************
  103.    2020 num=0 : LOGIC=BACK
  104.    2030 IF POINT(sx(a)-1,sy(a)+1)=1 THEN num=1
  105.    2040 IF POINT(sx(a)-1,sy(a)-1)=1 THEN num=num+2
  106.    2050 IF POINT(sx(a)+1,sy(a)-1)=1 THEN num=num+4
  107.    2060 IF POINT(sx(a)+1,sy(a)+1)=1 THEN num=num+8
  108.    2070 LOGIC=PHYSIC : d(a)=b(num,d(a))
  109.    2080 RETURN
  110.  
  111.      Since any two or three of the four points, and therefore the four bits,
  112. could be set it's a good idea to define a table showing all the possibilities
  113. and resulting denary numbers. See Table II:
  114.  
  115. Which points/bits         Corresponding binary       Corresponding denary
  116. are set?                        number                     number
  117.  
  118. p1,p2                           0011                         3
  119. p2,p3                           0110                         6
  120. p3,p4                           1100                        12
  121. p4,p1                           1001                         9
  122. p1,p2,p3                        0111                         7
  123. p2,p3,p4                        1110                        14
  124. p3,p4,p1                        1101                        13
  125. p4,p1,p2                        1011                        11
  126.  
  127. Table II: The possible points/bits along with their binary and denary numbers.
  128.  
  129.      The number we obtain from the four points tells us what type of obstacle
  130. has been hit, so all we now need is the original direction the ball was
  131. travelling in and its new direction can be read from an array.
  132.      Since there are eight possible directions, I've numbered them from one to
  133. eight .These are show in Figure I and reproduced in Table II.
  134.  
  135. STOSPIC.PC1:<<SCREENSHOT>>
  136.  
  137.      From these we can set up another table which tells us the new direction.
  138. You'll find this data starting at line 710 in the main program:
  139.  
  140. Obstacle type          Original direction         The eight directions
  141.                      1  2  3  4  5  6  7  8
  142.  
  143.      3               5  4  0  0  0  0  0  6             1  =  Left
  144.      6               0  8  7  6  0  0  0  0             2  =  Left/Up
  145.      7               7  6  5  0  0  0  0  0             3  =  Up
  146.      9               0  0  0  0  0  4  3  2             4  =  Right/Up
  147.     11               3  0  0  0  0  0  5  4             5  =  Right
  148.     12               0  0  0  2  1  8  0  0             6  =  Right/Down
  149.     13               0  0  0  0  3  2  1  0             7  =  Down
  150.     14               0  0  1  8  7  0  0  0             8  =  Left/Down
  151.  
  152. Table III: Calculating the new direction from the object type and original
  153. direction.
  154.  
  155.      For example, if the original direction was Down and the obstacle type was
  156. 13, the new direction can easily be read as 1, or Up. The code which controls
  157. this part of the program is the subroutine at line 2000. Notice that I've had
  158. to use the LOGIC instruction to set the screen on which the POINT command
  159. actually works.
  160.  
  161.      Running the demonstration program shows six sprites, each bouncing in
  162. slightly different ways. I don't normally give complete breakdowns of my
  163. programs, as this prevents you from working things out yourselves and therefore
  164. stops you thinking like a programmer. However, the six arrays used would
  165. probably cause great headaches if some explanation was not given.
  166.  
  167. B(14,8)   Stores the data from Table III.
  168. SX(6)     The X-coordinate for each sprite.
  169. SY(6)     The Y-coordinate for each sprite.
  170. D(6)      The direction of each sprite.
  171. DX(8)     What to add to the X coordinate of each sprite.
  172. DY(8)     What to add to the Y coordinate of each sprite.
  173.  
  174.  
  175.      Looking at the main routine of the program, lines 1020 to 1080, shows that
  176. the X and Y coordinates or each sprite is altered by the value of
  177. dx(d(sprite)). Line 600 holds the data for both the DX and DY arrays, which can
  178. be either 1, -1 or 0. This means that the X and Y coordinates will have 1, -1
  179. or zero added to them, depending on the sprite's direction, so giving the
  180. effect of movement.
  181.  
  182.      For example, if the sprite's coordinates were 150 and 90 and it was
  183. travelling down (7), it's new position would be calculated as follows:
  184.  
  185.    X=X+(DX(D(sprite_number)) : Y=Y+(DY(D(sprite_number))
  186. or
  187.                   X=X+(DX(7) : Y=Y+DY(7)
  188. or
  189.                        X=X+0 : Y=Y+1
  190.  
  191.      To speed the sprite up a little, try changing line 600 to:
  192.  
  193.    600 DATA -2,0,-2,-2,0,-2,2,-2,2,0,2,2,0,2,-2,2
  194.  
  195.      This cannot normally be done unless the distance between each of the
  196. obstacles is an exact multiple of 2, but luckily the screen I designed had just
  197. that property. To see what would happen if the distances were not correct,
  198. change line 600 to:
  199.  
  200.    600 DATA -4,0,-4,-4,0,-4,4,-4,4,0,4,4,0,4,-4,4
  201.  
  202.     That's all for now. See you on next month's cover disk.
  203.  
  204.  
  205.  
  206.