Here's the alpha test! Media in the download.
remstart
Library for Pixel Perfect Sprite Collision v0.9
by Kira Vakaan
Functions:
InitSpriteCollision()
-This function sets up the dummy sprite used in sprite point collision detection as well as the transparent color
used in all collisions. Call this command before any other.
SpritePointCollide(x,y,spr)
-Returns a one if the screen point (x,y) is colliding with sprite spr. If spr is set to zero, the number of the sprite
the point collides with is returned. Supports sprite scaling and offsetting.
SpriteCollide(spr1,spr2)
-Intended to replace the built-in command: sprite collision(spr1,spr2). Currently does NOT support sprite scaling but
DOES support sprite offsetting.
SetTransparentColor(color as dword)
-Sets the color to be ignored during collision detection.
GetTransparentColor()
-...really..?
In the future (maybe):
- Support for sprite scaling during sprite on sprite collision detection.
- Any suggestions from people.
remend
Set display mode 400,900,32
set window on
set window layout 0,0,10
hide mouse
sync on
sync rate 0
BACKDROP ON : COLOR BACKDROP RGB(128,0,128)
InitSpriteCollision()
load image "alphatest1.png",1
load image "alphatest2.png",2
load image "alphatest3.png",3
load image "alphatest4.png",4
sprite 1,0,0,1
sprite 2,0,200,2
sprite 3,0,400,3
sprite 4,0,0,4
sprite 5,0,700,2
set sprite priority 1,1
set sprite priority 2,1
set sprite priority 3,3
set sprite priority 4,2
set sprite priority 5,1
IMG=1
do
MX=mousex()
MY=mousey()
sprite 4,MX,MY,4
if mouseclick()=1 and FLAG=0
IMG=IMG+1
if IMG=4 then IMG=5
if IMG=6 then IMG=1
FLAG=1
endif
if mouseclick()=0 then FLAG=0
TEST1=SpritePointCollide(MX,MY,1)
TEST2=SpritePointCollide(MX,MY,2)
TEST3=SpritePointCollide(MX,MY,3)
TEST4=sprite collision(4,5)
TEST5=SpriteCollide(4,IMG)
text 0,600,"Backdrop tile - FPS: "+str$(screen fps())
text 0,612,"Solid Alpha Collision: "+str$(TEST1)
text 0,624,"Solid Collision: "+str$(TEST2)
text 0,636,"Half Alpha Collision: "+str$(TEST3)
text 0,648,"Normal Sprite Collision: "+str$(TEST4)
text 0,660,"SpriteCollide function: "+str$(TEST5)
text 0,672,"IMG number: "+str$(IMG)
text 0,688,"Normal Sprite Collision below"
sync
loop
end
function InitSpriteCollision()
#constant DUMMY 1000
global Transparent as dword : Transparent=0
create bitmap DUMMY,1,1
get image DUMMY,0,0,1,1
delete bitmap DUMMY
set current bitmap 0
sprite DUMMY,0,0,DUMMY
hide sprite DUMMY
endfunction
function SpritePointCollide(x,y,spr)
sprite DUMMY,x,y,DUMMY
Collision=sprite collision(DUMMY,spr)
if not Collision then exitfunction 0
if not spr then spr=Collision
dec x,sprite x(spr)
dec y,sprite y(spr)
x=(x/(sprite scale x(spr)/100.0))+sprite offset x(spr)
y=(y/(sprite scale y(spr)/100.0))+sprite offset y(spr)
make memblock from image 255,sprite image(spr)
Width=memblock dword(255,0)
Height=memblock dword(255,4)
if x<0 or y<0 or x=Width or y=Height then exitfunction 0
if memblock dword(255,12+4*((x%%Width)+((y%%Width)*Width)))=Transparent then exitfunction 0
endfunction Collision
function SpriteCollide(spr1,spr2)
Collision=sprite collision(spr1,spr2)
if not Collision then exitfunction 0
Color1 as dword
Color2 as dword
if not spr2 then spr2=Collision
XPos1=sprite x(spr1)-sprite offset x(spr1) : YPos1=sprite y(spr1)-sprite offset y(spr1)
XPos2=sprite x(spr2)-sprite offset x(spr2) : YPos2=sprite y(spr2)-sprite offset y(spr2)
if XPos2<XPos1
OverlapX=XPos1
OverlapWidth=XPos2+sprite width(spr2)-XPos1
else
OverlapX=XPos2
OverlapWidth=XPos1+sprite width(spr1)-XPos2
endif
if YPos2<YPos1
OverlapY=YPos1
OverlapHeight=YPos2+sprite height(spr2)-YPos1
else
OverlapY=YPos2
OverlapHeight=YPos1+sprite height(spr1)-YPos2
endif
Spr1X=OverlapX-XPos1
Spr1Y=OverlapY-YPos1
Spr2X=OverlapX-XPos2
Spr2Y=OverlapY-YPos2
make memblock from image 255,sprite image(spr1)
make memblock from image 256,sprite image(spr2)
Spr1Width=memblock dword(255,0)
Spr1Height=memblock dword(255,4)
Spr2Width=memblock dword(256,0)
Spr2Height=memblock dword(256,4)
for y=1 to OverlapHeight
for x=1 to OverlapWidth
if Spr1X<0 or Spr1Y<0 or Spr1X>=Spr1Width or Spr1Y>=Spr1Height then goto continue
if Spr2X<0 or Spr2Y<0 or Spr2X>=Spr2Width or Spr2Y>=Spr2Height then goto continue
Color1=memblock dword(255,12+4*((Spr1X%%Spr1Width)+((Spr1Y%%Spr1Width)*Spr1Width)))
Color2=memblock dword(256,12+4*((Spr2X%%Spr2Width)+((Spr2Y%%Spr2Width)*Spr2Width)))
if Color1<>Transparent and Color2<>Transparent then exitfunction Collision
continue:
inc Spr1X
inc Spr2X
next x
dec Spr1X,OverlapWidth
dec Spr2X,OverlapWidth
inc Spr1Y
inc Spr2Y
next y
endfunction 0
function SetTransparentColor(color as dword)
Transparent=color
endfunction
function GetTransparentColor()
endfunction Transparent
Four images; Completely transparent areas, Completely solid sprite, half transparent alpha, and solid sprite with the old sprite collision check.
Mouseclick to change the image number for the SpriteCollide check.
Works like a charm. Look at what the framerate does.