Karoshi MSX Community

Desarrollo MSX => Rutinas - Snipets => Mensaje iniciado por: nanochess en 08 de Enero de 2013, 02:32:32 am



Título: Collision between sprites
Publicado por: nanochess en 08 de Enero de 2013, 02:32:32 am
Hi everybody.

It appears that most programmers have problems detecting collision between sprites, sometimes the boundary-box is too big, sometimes at certain side there is a lack of collision.

Anyway, here is a routine that does collision detection, it is used with excellent results in Zombie Near.

In this case, IX points to data for first sprite, IY points to data for second sprite. We suppose that sprites are centered (i.e. a bullet is exactly at center of sprite drawing, same for ship/enemy)

We load register C with size of box, by example, a 4x4 bullet against a ship of 16x16 pixels would use 12 pixels. But if we are checking 16x16 square versus another 16x16 square, we would use 16 pixels.

In the case of Zombie Near, the collision check between zombies and player are 12 pixels and everybody founds it great :)

The structure of code makes it ideal to use IX to point to a list of sprites, B for size of list and IY to point to the bullet/player/zombie/ship.

Código:
d_x:    equ 0
d_y:    equ 1

ld c,8
        ld a,(ix+d_x)
        sub (iy+d_x)
        jp nc,.5
        neg
.5:     cp c
        jp nc,.2
        ld a,(ix+d_y)
        sub (iy+d_y)
        jp nc,.6
        neg
.6:     cp c
        jp nc,.2
        ; ...
        ; here we have a collision...
        ; ...
.2:

Enjoy it! ;)