# Adding gravity to a BASIC bouncing ball.

Using this Color BASIC code as a reference:

```0 REM gravity.bas
10 CLS
20 X=1:Y=1:XM=1:YM=1
30 PRINT@P," ";:P=X+Y*&H20:PRINT@P,"O";
50 X=X+XM:IF X<&H1 OR X>&H1E THEN XM=-XM
60 Y=Y+YM:IF Y<&H1 OR Y>&HE THEN YM=-YM
80 GOTO 30```

…how would you add simulated gravity to the bounce? When I was a teen, I did this on my CoCo 3. I forget how I did it, but here is what I tried tonight:

```0 REM gravity.bas
10 CLS
20 X=1:Y=1:XM=1:YM=.25
30 PRINT@P," ";:P=X+INT(Y)*&H20:PRINT@P,"O";
50 X=X+XM:IF X<&H1 OR X>&H1E THEN XM=-XM
60 Y=Y+YM:IF Y<&H1 OR Y>&HE THEN YM=-YM:Y=Y+YM
70 YM=YM+.25
80 GOTO 30```

But on a text screen, the “jump” is large enough when it’s near the bottom that it never actually hits the bottom of the screen. In the CoCo 3’s high-resolution screen, this wasn’t an issue. With only 16 horizontal positions, it’s quite limited.

I’m sure there’s a real clever way to do this. Any thoughts?

## 3 thoughts on “Adding gravity to a BASIC bouncing ball.”

1. Paul Fiscarelli

Hey Allen – one approach to implementing gravity can be done through a look-up table. This method is compact and removes the need for doing computationally expensive calculations during the animation.

I put together a video on one example of how this may be done – https://youtu.be/SqPHXNms9B8

This site uses Akismet to reduce spam. Learn how your comment data is processed.