Karoshi MSX Community
05 de Julio de 2021, 01:15:03 pm *
Bienvenido(a), Visitante. Por favor, ingresa o regístrate.

Ingresar con nombre de usuario, contraseña y duración de la sesión
Noticias:
 
   Inicio   Ayuda Buscar Ingresar Registrarse  
Páginas: [1]
  Imprimir  
Autor Tema: Collision between sprites  (Leído 3589 veces)
0 Usuarios y 1 Visitante están viendo este tema.
nanochess
Karoshi Lover
***
Mensajes: 141


Programando algo buenísimo :)


WWW
« : 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 Smiley

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! Wink
En línea

Mira mis juegos MSX/Colecovision/Atari/Intellivision http://nanochess.org/retro_es.html, y sígueme en Twitter http://twitter.com/nanochess
Páginas: [1]
  Imprimir  
 
Ir a:  

Impulsado por MySQL Impulsado por PHP Powered by SMF 1.1.21 | SMF © 2013, Simple Machines XHTML 1.0 válido! CSS válido!