Program the Bouncing Ball

Type
Education
Category
Player/missile animation tutorial
Author
David Fox and Mitchell Waite
Published in
Antic, October 1984 (Vol. 3, No. 6); excerpted from the book Computer Animation Primer (Byte Books/McGraw-Hill, 1984)
Year
1984
Language
Atari BASIC
Requirements
Any Atari 8-bit, Atari BASIC; no joystick
A black GRAPHICS 3 screen with a green ball resting on the dividing line above the text window, which reads BOUNCING BALL DEMO, Initial velocity: 70, Elasticity: 0.8

About the program

This program is "Example 8" from Computer Animation Primer by David Fox and Mitchell Waite (Byte Books/McGraw-Hill, 1984). Antic printed an edited excerpt in October 1984 as a tutorial on player/missile graphics. A ball, made from player 0, is launched from the left of the screen. It falls under gravity, squashes flat for a moment each time it hits the floor, and bounces a little lower each time until it rolls to a stop. Then you are asked for a new velocity and "elasticity" and can watch it again. It runs on any Atari, whatever its memory, and needs no joystick.

What you need

Atari BASIC. That's all.

Typing it in

Type line 70 first. It must be the first line you enter that contains a variable, because it creates PLR0$ as the first entry in the variable value table. The program later relocates that string onto player 0's memory (lines 9000–9080), and that only works if it is the first variable. The listing uses inverse video for its messages ({inv}…{/inv}, Atari key), {CLEAR} (ESC then SHIFT-CLEAR) and {CTRL-S} (hold CTRL and press S). Antic asked readers to check it with TYPO and to SAVE a backup.

How to use it

RUN it and watch the first bounce sequence. When the ball has stopped, type a velocity (any positive number) and press RETURN, then an elasticity. Elasticity is the fraction of its speed that the ball keeps at each bounce: 0.5 keeps half, 1.0 bounces for ever, and 0 doesn't bounce at all. The authors noted that a toy super ball manages about 0.85. Values above 1 make each bounce higher than the last.

How it works

  • Turning on P/M graphics takes three POKEs: the page of P/M memory into PMBASE (54279), 42 into SDMCTL (559) for a normal screen with double-line player DMA, and 2 into GRACTL (53277) (lines 7000–7130). GRAPHICS 3 is used only because it needs the least screen memory.
  • A string as a player. Lines 9000–9080 calculate the distance from the start of string/array space (location 140) to player 0's memory, then write it into PLR0$'s entry in the variable value table (location 134). From then on, assigning to PLR0$ writes straight into the player, so the ball is moved vertically with fast string operations.
  • Filling a string fast. Line 160 fills BLANK$ with zero bytes using the classic trick of setting the first character and then BLANK$(2)=BLANK$, which copies each character onto the next.
  • Physics. Lines 440 onward use the falling-body formula, height = velocity × time − 16 × time², restarting the clock at each bounce with the velocity multiplied by the elasticity. The squash is one of three animation frames (seven bytes each) read from the DATA at line 20060.

Listing

10 REM {inv}*** BOUNCING BALL 1 PROGRAM ***{/inv}
20 REM {inv}                 Example 8     {/inv}
30 REM
40 REM Program to demonstrate Player-Missile Graphics using string manipulation
50 REM Copyright (C) 1982 by David Fox and Mitchell Waite
60 REM
70 DIM PLR0$(128):GOTO 140:REM This MUST be the first variable in the program
80 REM
100 REM {inv}Hi/Lo Byte Calculation{/inv}
110 HIBYTE=INT(X/256):REM Calculate High Byte
120 LOBYTE=X-HIBYTE*256:REM Calculate Low Byte
130 RETURN
140 REM {inv}Initialize{/inv}
150 DIM BLANK$(128),PLR(3),HPLR(3)
160 BLANK$(1)=CHR$(0):BLANK$(128)=CHR$(0):BLANK$(2)=BLANK$:REM Fill with blanks
170 GRAPHICS 3:POKE 752,1:PRINT "One moment please...":REM Turn off cursor, print message
190 GOSUB 5000:REM Set up memory locations
220 GOSUB 7000:REM Set up Player area
230 GOSUB 9000:REM Point PLR0$ to Player 0 RAM
240 GOSUB 10000:REM Read frames into RAM
295 REM {CTRL-S}
300 ? "{CLEAR}     {inv}*** BOUNCING BALL DEMO ***{/inv}"
305 REM {CTRL-S}
310 VEL=70:ELASTIC=0.8
320 PRINT "Initial velocity: ";VEL:PRINT "Elasticity: ";ELASTIC;
330 REM
400 REM {inv}Main Animation Loop{/inv}
410 BOTTOM=91:XPOS=40:TIME=0.5:HORIZ=0.75
420 GOSUB 700:REM Move Player off screen
430 IF ELASTIC<=0.1 THEN SNDFLAG=1
440 YPOS=BOTTOM-(VEL*TIME-16*TIME*TIME):FRMNO=1
450 IF YPOS>82 AND VEL>30 THEN FRMNO=2
460 IF YPOS>=BOTTOM THEN YPOS=BOTTOM:VEL=VEL*ELASTIC:TIME=0:FRMNO=1:IF VEL>14 THEN FRMNO=3
470 IF XPOS>220 OR YPOS<=1 THEN 600
480 POKE HPLR(0),XPOS
490 FRAME$=FRAMEMEM$((FRMNO-1)*FRMSIZE+1,FRMNO*FRMSIZE):REM Select correct frame
500 BUFFER$=BLANK$:REM Fill Buffer with blanks
510 BUFFER$(YPOS,YPOS+FRMSIZE-1)=FRAME$:REM Move current frame into buffer
520 PLR0$=BUFFER$:REM Move buffer into Player 0 RAM
530 XPOS=XPOS+HORIZ
540 IF YPOS=BOTTOM AND (VEL+SNDFLAG>0.5) THEN SOUND 1,250,10,14:SNDFLAG=0:SOUND 1,0,0,0
550 IF VEL>0.5 THEN TIME=TIME+0.15:GOTO 440
560 HORIZ=HORIZ-0.01:IF HORIZ>0 THEN FRMNO=1:GOTO 470
570 REM
600 REM {inv}Get Parameters for Ball{/inv}
610 GOSUB 700
620 POKE 752,0:REM Turn on cursor
630 PRINT "{CLEAR}Enter initial velocity: ";
640 TRAP 630:INPUT VEL
650 PRINT "Enter the ball's elasticity (a number":PRINT "  from 0-1 [or more]): ";
660 INPUT ELASTIC
670 POKE 752,1:PRINT " ";:REM Turn off cursor
680 TRAP 40000:GOTO 400
690 REM
700 REM {inv}Move Player 0 to Left of Screen{/inv}
710 POKE HPLR(0),0
730 RETURN
740 REM
5000 REM {inv}Set Up Memory Locations{/inv}
5100 READ FRAMES,FRMSIZE,NUMPLRS
5120 PLRFRMMEM=FRAMES*FRMSIZE
5130 FRAMEMEM=PLRFRMMEM*NUMPLRS
5170 DIM BUFFER$(128),FRAME$(FRMSIZE),FRAMEMEM$(FRAMEMEM)
5360 RETURN
5370 REM
7000 REM {inv}Initialize Player-Missile Graphics{/inv}
7010 TEMP=PEEK(106)-8:REM Set aside Player-Missile area
7020 POKE 54279,TEMP:REM Tell ANTIC where PM RAM is
7030 PMBASE=256*TEMP:REM Find PM Base address
7040 FOR I=0 TO 3
7050 PLR(I)=PMBASE+128*I+512:REM Set addresses of Players
7060 HPLR(I)=53248+I:REM Horizontal Player Position registers
7070 NEXT I
7080 POKE 559,42:REM Set PM 2 line resolution, Players enabled
7090 POKE 704,12*16+8:REM Color ball green
7100 POKE 53277,2:REM Enable Player display
7120 RETURN
7130 REM
9000 REM {inv}Point PLR0$ to Player 0 RAM{/inv}
9010 STARP=PEEK(140)+PEEK(141)*256:REM Start of String Array area
9020 VVTP=PEEK(134)+PEEK(135)*256:REM Start of Variable Value Table
9030 OFFSET=PLR(0)-STARP:REM Calculate offset from String Array to Player 0
9040 X=OFFSET:GOSUB 110
9050 POKE VVTP+2,LOBYTE:REM Poke offset of string into Variable Value Table
9060 POKE VVTP+3,HIBYTE:REM This points the first string (PLR0$) to PLR(0)
9070 RETURN
9080 REM
10000 REM {inv}Read in Frame Data{/inv}
10090 FOR J=1 TO PLRFRMMEM
10100 READ BYTE
10110 FRAMEMEM$(J,J)=CHR$(BYTE)
10120 NEXT J
10130 RETURN
10140 REM
20000 REM {inv}FRAME DATA{/inv}
20030 REM
20040 REM Number of Frames, Frame Size, Number of Players
20050 REM .  (Bouncing Ball)
20060 DATA 3,7,1
21000 REM {inv}Frame Data for Bouncing Ball{/inv}
21010 REM Frame 1
21020 DATA 0,60,126,126,126,60,0
21030 REM Frame 2
21040 DATA 24,60,60,60,60,60,24
21050 REM Frame 3
21060 DATA 0,0,0,126,255,126,0

Download

The original program from the Antic disk, to LOAD or ENTER instead of typing it: EXAMPLE8.BAS.

Credits