This program demonstrates how to do collision checks between objects on the screen. To have something to collide with I put two horizontal grid lines, one vertical grid line, one ship shaped character and sprite 1 shaped as a triangle on the screen. To the right I display the contents of the collision register. It shows which objects collide with sprite 0 which is the joystick controlled ball. The joystick and sprite move routines are recycled from the previous chapter, they are not explained again. Since the part of the program which displays the static objects mostly moves values into the correct VDC registers I don't explain it here.
It is only legal to read the collision register during VBLANK. Fortunately
the IRQ routine in the BIOS is doing this. The value is stored in internal RAM
at 03Dh. But before I can check the value, I have to tell the VDC which
object to test for collision. So at the beginning of every frame I write
vdc_coll_spr0 = 001h into the register
vdc_collision, which means I want to know what collides with
sprite 0 in the frame currently drawn. Then I read the collision register
stored in the internal RAM by the IRQ and display it as hexadecimal using a very
simple routine not explained here. This value represents the situation as drawn
in the last frame. If you move sprite 0 using the joystick you can see
which bit of the collision register is responsible for which object. The
external collision is used only on Videopac plus machines, there it flags
a collision with an object drawn in bright colors. The demo program uses only
normal Videopac graphics, so that feature is not demonstrated here.
call waitvsync ; wait for begin of frame
call vdcenable ; enable vdc
; tell the vdc to look for collisions with sprite 0
; in the next frame
mov r0,#vdc_collision
mov a,#vdc_coll_spr0
movx @r0,a ; activate collision checks
; display the contents of the collision register on
; the right side. this information reflects the
; situation of the last frame.
call gfxoff
mov r0,#iram_collision
mov a,@r0
mov r0,#vdc_char0
mov r3,#040h ; x position
mov r4,#01Eh ; y position
mov r6,#col_chr_white
call displayhex
call gfxon
This is the first demo program which is longer than one page (256 bytes), so
I put all code which uses data in program memory (accessed via
movp) into an extra page, this includes the routine to set the
sprite shape and the display a hex number routine. But this means there is some
unused space at the end of page 4, in bigger projects it could be necessary to
use this space. The only way to solve this is to shift routines internally
until everything fits and every movp gets its data from the
correct page.