Programming by Example

 

A BB4W Compendium

freeman69@gmx.com

IDIC BBC_Owl2 M&P

Rocket Rotation

The following demonstration combines simple drawing techniques with rotational transformations. A rocket at the bottom of the window can be turned and moved forward.

 

This program is an intermediate step towards the Asteroid Belt game, providing simplified  examples of the techniques to be used.

 

         Z= Rotate anticlockwise

         X= Rotate clockwise

         M= Move forward

 

  10 MODE 9:OFF

  20 VDU 23,23,2;0;0;0;:REM line thickness=2

  30 scale=12:angle=0:shipx=640:shipy=9*scale

  40

  50 REPEAT

  60   TIME=0

  70   CLS

  80  

  90   FOR a=1 TO 19

 100     READ c,d

 110     x=FNrotx(c,d,angle)*scale+shipx

 120     y=FNroty(c,d,angle)*scale+shipy

 130     CASE a OF

 140       WHEN 1 : CIRCLE x,y,scale

 150       WHEN 2 : MOVE x,y

 160       OTHERWISE : DRAW x,y

 170     ENDCASE

 180   NEXT

 190   RESTORE

 200  

 210   IF INKEY(-98) angle+=12

 220   IF INKEY(-67) angle-=12

 230   IF INKEY(-102) THEN

 240     shipx-=SIN(RAD(angle))*scale

 250     shipy+=COS(RAD(angle))*scale

 260   ENDIF

 270  

 280   *FX21

 290   WAIT 4-TIME

 300 UNTIL FALSE

 310 END

 320

 330 DATA 0,4

 340 DATA 0,10,2,8,4,4,3,-3,6,-7,5,-9,3,-9,2,-7,1,-9

 350 DATA -1,-9,-2,-7,-3,-9,-5,-9,-6,-7,-3,-3,-4,4,-2,8,0,10

 360

 370 DEF FNrotx(x,y,angle)

 380 =COS(RAD(angle))*x-SIN(RAD(angle))*y

 390

 400 DEF FNroty(x,y,angle)

 410 =SIN(RAD(angle))*x+COS(RAD(angle))*y

Rocket

Rocket Rotation: Code explained...

 

  10 MODE 9:OFF

  20 VDU 23,23,2;0;0;0;:REM line thickness=2

  30 scale=12:angle=0:shipx=640:shipy=9*scale

 

'scale' controls the size of the rocket.

'angle' holds the current angle of rotation, between zero and 360 degrees.

'shipx' & 'shipy' hold the current position of the ship.

 

  50 REPEAT

  60   TIME=0

  70   CLS

 

This is the first program that clears the window of all graphics (and text), prior to drawing each new animation frame. Previously, we've deleted individual graphics before updating their positions.

 

  90   FOR a=1 TO 19

 100     READ c,d

 110     x=FNrotx(c,d,angle)*scale+shipx

 120     y=FNroty(c,d,angle)*scale+shipy

 130     CASE a OF

 140       WHEN 1 : CIRCLE x,y,scale

 150       WHEN 2 : MOVE x,y

 160       OTHERWISE : DRAW x,y

 170     ENDCASE

 180   NEXT

 190   RESTORE

 

Because this program is quite short, we're using very few functions or procedures. This FOR... NEXT loop reads the 19 pairs of co-ordinates stored in DATA lines 330 to 350.

Each pair of co-ordinates is rotated around co-ordinate zero, zero. The calculations for this transformation are performed by two separate, but related functions (similar to Tetra Blocks): FNrotx and FNroty. We then add the co-ordinates of the position of the ship to each transformed co-ordinate.

The CASE command controls how these transformed co-ordinates are used. When the rocket was designed, the first pair of co-ordinates defined the circular port-hole. The second pair defined the beginning of the rocket outline. The remaining co-ordinates defined points needing to be drawn to.

Line 190 instructs BB4W to return to the beginning of the first line of DATA, ready for the next animation frame when we'll want to draw the rocket all over again.

 

 210   IF INKEY(-98) angle+=12

 220   IF INKEY(-67) angle-=12

 230   IF INKEY(-102) THEN

 240     shipx-=SIN(RAD(angle))*scale

 250     shipy+=COS(RAD(angle))*scale

 260   ENDIF

 

Keyboard input allows the user to steer the rocket. Pressing 'M' moves the ship forward. The X and Y (left/right, up/down) components of motion are calculated using SIN and COS.

 

 280   *FX21

 290   WAIT 4-TIME

 300 UNTIL FALSE

 310 END

 320

 330 DATA 0,4

 340 DATA 0,10,2,8,4,4,3,-3,6,-7,5,-9,3,-9,2,-7,1,-9

 350 DATA -1,-9,-2,-7,-3,-9,-5,-9,-6,-7,-3,-3,-4,4,-2,8,0,10

 360

 370 DEF FNrotx(x,y,angle)

 380 =COS(RAD(angle))*x-SIN(RAD(angle))*y

 390

 400 DEF FNroty(x,y,angle)

 410 =SIN(RAD(angle))*x+COS(RAD(angle))*y

 

As mentioned above, the rotational transformation of the X, Y co-ordinates are performed by two functions. Note that each function uses the original X and Y values. Do not overwrite the original X component before using it to transform the Y component (or vice versa)!

 

Many modern games depend on the capabilities of graphical processors, performing similar calculations. Due to the sophistication of modern systems, programmers often know how to ask a machine to perform a complex set of operations, without understanding how the machine completes the actual task.

Arrow black large Arrow black large