Collision detection

A nice and quick way to create two filled sprites (sprite 1 and 2) is to enter the monitor and type:

F 0E00 0E3E FF
F 0E3F 0E7E FF

Of course you can fill both at once, but this the above code nicely declares the two memory areas I am using in this example. Now, let’s write a simple Basic program to fire the two sprites so that they collide now and then. This will make one sprite white and the other cyan, and fire the movements.

10 SPRITE 1, 1, 2, 0
20 MOVSPR 1, 100, 100
30 MOVSPR 1, 90#1
40 SPRITE 2, 1, 2, 0
50 MOVSPR 2, 100, 115
60 MOVSPR 2, 270#1

To detect collision between the two sprites, we can use the BUMP function. BUMP(1) returns a bit pattern telling us what sprites has collided, and BUMP(2) returns a bit pattern that tells us what sprites has collided with static graphics on screen. We would expect BUMP(1) to return 3 if sprite 1 and 2 is colliding, given that no other sprites are colliding as well, because the binary number 00000011 represents the decimal number 3. And indeed. Add these two lines to your program to see what BUMP(1) returns:

80 GOTO 70

To interrupt the program, press RunStop. If you are using the VICE emulator, that key might be mapped to the Escape key. BUMP(1) is actually just returning the bit pattern in the byte located at D01E, so BUMP(1) is equivalent to PEEK(53278).

Leave a Reply

Your email address will not be published. Required fields are marked *