Posted: 15th Feb 2003 12:57
wanting to write a 2d retro game for the compo i started work but soon discovered dbp sprite collision not pixel perfect. So i made my own function, it works best if you pass smallest sprite as sp1.

+ Code Snippet
sync on
set text size 20
color backdrop 0
make object cube 1,1
hide object 1
rem make 2 sprites
ink rgb(250,100,120),0
box 20,20,40,40
box 10,15,20,55
box 30,25,60,60
ink rgb(180,180,180),0
box 120,120,135,145
ink rgb(55,200,120),0
box 125,120,145,130
get image 1,0,0,70,70,1
sprite 1,150,150,1
get image 2,115,115,150,150,1
sprite 2,10,10,2
cls
sp1x=10
sp1y=10
do
if upkey() and sp1y>0 then dec sp1y,1
if downkey() and sp1y0 then dec sp1x,1
if rightkey() and sp1xmemblock byte(2,x)
  if memblock byte(1,x)0
   delete memblock 1
   delete memblock 2
   exitfunction 1
  endif
 endif
next x

delete memblock 1
delete memblock 2

endfunction 0
Posted: 15th Feb 2003 12:59
oh btw i wanted to make memblocks from the images not make bitmaps then make memblocks but when i made memblocks from images they weren't same as origional images??
Posted: 15th Feb 2003 13:03
my code didn't post right??
here again:
+ Code Snippet
sync on
set text size 20
color backdrop 0
make object cube 1,1
hide object 1
rem make 2 sprites
ink rgb(250,100,120),0
box 20,20,40,40
box 10,15,20,55
box 30,25,60,60
ink rgb(180,180,180),0
box 120,120,135,145
ink rgb(55,200,120),0
box 125,120,145,130
get image 1,0,0,70,70,1
sprite 1,150,150,1
get image 2,115,115,150,150,1
sprite 2,10,10,2
cls
sp1x=10
sp1y=10
do
if upkey() and sp1y>0 then dec sp1y,1
if downkey() and sp1y<screen height() then inc sp1y,1
if leftkey() and sp1x>0 then dec sp1x,1
if rightkey() and sp1x<screen width() then inc sp1x,1
if spritehit(2,1)
 center text screen width()/2,10,"COLLISION!!"
 sync
 suspend for key
endif
sprite 2,sp1x,sp1y,2
sync
loop


function spritehit(sp1,sp2)
if sprite collision(sp1,sp2)=0 then exitfunction 0
create bitmap 1,sprite width(sp1),sprite width(sp2)
paste sprite sp1,0,0
make memblock from bitmap 1,1
delete bitmap 1
create bitmap 1,screen width(),screen height()
paste sprite sp1,sprite x(sp1),sprite y(sp1)
paste sprite sp2,sprite x(sp2),sprite y(sp2)
get image 1000,sprite x(sp1),sprite y(sp1),sprite x(sp1)+sprite width(sp1),sprite y(sp1)+sprite height(sp1),1
delete bitmap 1
create bitmap 1,memblock word(1,0),memblock word(1,4)
paste image 1000,0,0
make memblock from bitmap 2,1
delete bitmap 1
delete image 1000

for x=12 to get memblock size(1)-1
 if memblock byte(1,x)<>memblock byte(2,x)
  if memblock byte(1,x)<>0
   delete memblock 1
   delete memblock 2
   exitfunction 1
  endif
 endif
next x

delete memblock 1
delete memblock 2

endfunction 0
Posted: 15th Feb 2003 13:31
And thats number 3 ...
Posted: 16th Feb 2003 13:43
okay i found if u make memblocks from loaded images they not right, but if you paste image then grab it u can make memblock from that image with no probs.
so i use that to improve my collision function which now runs quicker:

+ Code Snippet
sync on
set text size 20
color backdrop 0
make object cube 1,1
hide object 1
create bitmap 1,800,600
set current bitmap 0
rem make 2 sprites
ink rgb(250,100,120),0
box 20,20,40,40
box 10,15,20,55
box 30,25,60,60
ink rgb(180,180,180),0
box 120,120,135,145
ink rgb(55,200,120),0
box 125,120,145,130
get image 1,0,0,70,70,1
sprite 1,150,150,1
get image 2,115,115,150,150,1
sprite 2,10,10,2
cls
sp1x=10
sp1y=10
do
if upkey() and sp1y>0 then dec sp1y,1
if downkey() and sp1y<screen height() then inc sp1y,1
if leftkey() and sp1x>0 then dec sp1x,1
if rightkey() and sp1x<screen width() then inc sp1x,1
if spritehit(2,1)
 center text screen width()/2,10,"COLLISION!!"
 sync
 suspend for key
endif
sprite 2,sp1x,sp1y,2
sync
loop

function spritehit(sp1,sp2)
if sprite collision(sp1,sp2)=0 then exitfunction 0
set current bitmap 1
paste sprite sp1,0,0
get image 1000,0,0,sprite width(sp1),sprite height(sp1),1
make memblock from image 1,1000
delete image 1000
cls
paste sprite sp1,sprite x(sp1),sprite y(sp1)
paste sprite sp2,sprite x(sp2),sprite y(sp2)
get image 1000,sprite x(sp1),sprite y(sp1),sprite x(sp1)+sprite width(sp1),sprite y(sp1)+sprite height(sp1),1
make memblock from image 2,1000
set current bitmap 0
delete image 1000
for x=12 to get memblock size(1)-1
 if memblock byte(1,x)<>memblock byte(2,x)
  if memblock byte(1,x)<>0
   delete memblock 1
   delete memblock 2
   exitfunction 1
  endif
 endif
next x

delete memblock 1
delete memblock 2

endfunction 0
Posted: 17th Feb 2003 8:06
http://www.darkbasicpro.com/apollo/view.php?t=4826&b=6

try this one, anysize sprites and memblocks for speed
Posted: 17th Feb 2003 11:14
The reason your collision doesn't work all the time without pasting to a bitmap is that you are not taking any account of the pixel depths of your images. Pasting them removes this difference. You are also using GET IMAGE which is *slow*. You are also comparing the whole sprite, not just the overlapping portions.

The one posted above is freddix's one that handles multiple pixel depths, or there's this one originally by Chipone and optimised by me.

http://www.darkbasicpro.com/apollo/view.php?t=5209&b=4

If you can ensure that all your sprites are 32 bit, then use this one as it's faster, otherwise use the one by freddix. It does sound as if freddix's is better for the images you are using.

Look out for a changed version of the 32 bit one that will work with any image bit depth at high speed - coming to a forum near you - soon!
Posted: 17th Feb 2003 12:06
Sonic informed me freddix had made a pixel perfect sprite collision function when i was making my function.
I just wanted to make one on my own - which i did and it works(even though its not as fast as the others u mentioned).
I got plans to improve it by using a 3rd memblock insted of a bitmap and paste and get image. But at the minute i doing my retro compo thing
neway thnx for info.