Beyond Basic - Part 5

Downloads|Programming|Beyond Basic

Written by: Tony Cruise

First Published: Micro’s Gazette – Issue 003 (May/June 1989)

 

This issue I will start to detail the collision detection routine to go with the automatic sprite routines from last issue.  But first here is a list of the conversions for SVI-318/328 to MSX.

Part 1

The SVI-318/328 use the port 8C to change slots.  So to change between the RAM and ROM slots use:

ROM to RAMRAM to ROM
LD A,0FHLD A,(FE64H)
OUT (88H),AOUT (8CH),A
IN A,(90H)
LD (FE64H),A
AND FDH
OUT (8CH),A

Part 2 Onwards

Here is a list of ROM calls, RAM locations and HOOK addresses to use instead of the MSX values.

  • HGONE = FF57H,
  • HTIMI = FF5AH,
  • SCRMOD = FE3AH
  • SETWRT = 373CH,
  • SETRD = 3747H

Change the following port calls to:

  • OUT (98H),A becomes OUT (80H),A
  • IN A,(98H) becomes IN A,(84H)

This list will be expanded if necessary, each issue.

Sprite Collision Detection

Now onto this months routine.   Our routine will test the collision of one sprite (you specify) with all 31 others.  A table of flags will be used to show which sprites the one specified is colliding with.  The size of each sprite will be specified by a X and Y with from 0 to 15 (There is no zero width, work out the real value and subtract one).  For the routine to work effectively, your sprite shapes should be drawn from the top left hand corner of the sprite shape pattern.

e.g. X width 5, Y width 2

To make the routine easier to understand I have split it into two sections.  The section I will show this issue is the actual test routine, that tests whether two specified sprites are colliding and setting the carry flag if so.  The values of the registers on entry are:

  • HL points to sprite 1 VRAM table location
  • DE points to sprite 2 VRAM table location
  • B lower 4 bits, y width and top 4 bits, x width for sprite 1
  • C lower 4 bits, y width and top 4 bits, x width for sprite 2

Machine Code Program

1 ;Subroutine to test the collision of two sprites
2 ;
3 ;
4 ;
5 ; Label settings
6 ;
7 SETRDEQU 0050H; Video read
8 ;
C0FAE59 STARTPUSH HL; Save registers
C0FBD510PUSH DE;
C0FCC511PUSH BC;
C0FD7812LD A,B; Get Y velocity 1
C0FEE60F13AND 15;
C1004714LD B,A;
C1017915LD A,C; Get Y velocity 2
C102E60F16AND 15;
C1044F17LD C,A;
C105CD55C118CALL RDVRM; Get Y1 value
C1088019ADD A,B; Add velocity
C1094720LD B,A;
C10AEB21EX DE,HL; Get Y2 value
C10BCD55C122CALL RDVRM;
C10EEB23EX DE,HL;
C10FB824CP B; Hit?
C110304025JR NC,NOHIT; No – Exit loop
C112EB26EX DE,HL; Get Y2 value
C113CD55C127CALL RDVRM;
C116EB28EX DE,HL;
C1178129ADD A,C; Add velocity
C1184F30LD C,A;
C119CD55C131CALL RDVRM; Get Y1 velocity
C11CB932CP C; Hit?
C11D303333JR NC,NOHIT; No – Exit loop
C11FC134POP BC; Restore BC
C120C535PUSH BC; Resave BC
C1212336INC HL; Increment pointers
C1221337INC DE;
C123CB3838SRL B; Get X velocity 1
C125CB3839SRL B;
C127CB3840SRL B;
C129CB3841SRL B;
C12BCB3942SRL C; Get X velocity 2
C12DCB3943SRL C;
C12FCB3944SRL C;
C131CB3945SRL C;
C133CD55C146CALL RDVRM; Get Y1 value
C1368047ADD A,B; Add velocity
C1374748LD B,A;
C138EB49EX DE,HL; Get Y2 velocity
C139CD55C150CALL RDVRM;
C13CEB51EX DE,HL;
C13DB852CP B; Hit?
C13E301253JR NC,NOHIT; No – Exit loop
C140EB54EX DE,HL; Get Y2 value
C141CD55C155CALL RDVRM;
C144EB56EX DE,HL;
C1458157ADD A,C; Add velocity
C1464F58LD C,A;
C147CD55C159CALL RDVRM; Get Y1 value
C14AB960CP C; Hit?
C14B300561JR NC,NOHIT; No – Exit loop
C14D3762SCF; Set carry flag
C14EC163 EXITPOP BC; Restore registers
C14FD164POP DE;
C150E165POP HL;
C151C966RET; Return from routine
C152AF67 NOHITXOR A; Clear flags
C15318F968JR EXIT; Go to EXIT
C15569 ;
C15570 ;Subroutine to read a byte from VRAM
C15571 ;
C155CD500072 RDVRMCALL SETRD; Set screen location
C158D89873IN A,(98H); Get value
C15AC974RET;
C15B75 ;
76 END