Everyone will agree, the sprite hit and sprite collision commands aren't that useful. So I came up with a function that checks pixel collision. The only drawback with my function is that it only checks pixel collision against the second sprite. This is both good and bad.
The purpose of the function is mainly to check for collision between a player and the level.
To use it just call up PixelCollision.
It will put a bounding box around spr1 and check that box against the pixels of spr2. It returns a string based on where the collision hits. It will return the following
TopL, TopR, BotL, BotR, LefT, LefB, RigT, RigB
This is how they correspond to the sprite
TopL TopR
LefT|-----------|RigT
| |
| |
| |
| |
LefB|___________|RigT
BotL BotL
So if the collision occurred on the bottom of the sprite, it would
return BotLBotR
if the collision occurred on the Left side it would return LefTLefB
ect.
Function PixelCollision(spr1,spr2)
col$=""
mem=1
while memblock exist(mem)
inc mem
endwhile
spr2Img=Sprite Image(spr2)
Make memblock from image mem,spr2Img
memWidth=Image Width(spr2Img)
memHeight=Image Height(spr2Img)
memSize=Get memblock size(mem)
Col$=GetPixels(spr1,spr2,mem,memSize,MemWidth)
`if spacekey()=1
`delete image spr2Img
`make image from memblock spr2Img,mem
`save image "TEST1.jpg",spr2Img
`delete memblock mem
`end
`endif
delete memblock mem
Endfunction Col$
Function GetPixels(spr1,spr2,mem,memSize,MemWidth)
col$=""
Xpos=sprite x(spr1)
Ypos=sprite y(spr1)
Width=Sprite width(spr1)
Height=Sprite height(spr1)
dim PointLoc(2,4,3)
Pos=(3+Xpos+1+(Ypos-1)*MemWidth)*4
if Pos<MemSize and Pos>11 and Xpos>=Sprite X(spr2) and Xpos<=Sprite X(spr2)+Sprite Width(spr2) and Ypos>=Sprite Y(spr2) and Ypos<=Sprite Y(spr2)+Sprite Height(spr2)
PointLoc(1,1,1)=memblock byte(mem,Pos)
PointLoc(1,1,2)=memblock byte(mem,Pos+1)
PointLoc(1,1,3)=memblock byte(mem,Pos+2)
endif
Pos=((3+Xpos+Width-1)+(Ypos-1)*memWidth)*4
if Pos<MemSize and Pos>11
PointLoc(2,1,1)=memblock byte(mem,Pos)
PointLoc(2,1,2)=memblock byte(mem,Pos+1)
PointLoc(2,1,3)=memblock byte(mem,Pos+2)
endif
Pos=(3+Xpos+1+(Ypos+Height+1)*memWidth)*4
if Pos<MemSize and Pos>11
PointLoc(1,4,1)=memblock byte(mem,Pos)
PointLoc(1,4,2)=memblock byte(mem,Pos+1)
PointLoc(1,4,3)=memblock byte(mem,Pos+2)
endif
Pos=(3+Xpos+Width-1+(Ypos+Height+1)*memWidth)*4
if Pos<MemSize and Pos>11
PointLoc(2,4,1)=memblock byte(mem,Pos)
PointLoc(2,4,2)=memblock byte(mem,Pos+1)
PointLoc(2,4,3)=memblock byte(mem,Pos+2)
endif
Pos=(3+Xpos-1+(Ypos+1)*memWidth)*4
if Pos<MemSize and Pos>11
PointLoc(1,2,1)=memblock byte(mem,Pos)
PointLoc(1,2,2)=memblock byte(mem,Pos+1)
PointLoc(1,2,3)=memblock byte(mem,Pos+2)
endif
Pos=(3+Xpos-1+(Ypos+Height-1)*memWidth)*4
if Pos<MemSize and Pos>11
PointLoc(1,3,1)=memblock byte(mem,Pos)
PointLoc(1,3,2)=memblock byte(mem,Pos+1)
PointLoc(1,3,3)=memblock byte(mem,Pos+2)
endif
Pos=(3+Xpos+Width+1+(Ypos+1)*memWidth)*4
if Pos<MemSize and Pos>11
PointLoc(2,2,1)=memblock byte(mem,Pos)
PointLoc(2,2,2)=memblock byte(mem,Pos+1)
PointLoc(2,2,3)=memblock byte(mem,Pos+2)
endif
Pos=(3+Xpos+Width+1+(Ypos+Height-1)*memWidth)*4
if Pos<MemSize and Pos>11
PointLoc(2,3,1)=memblock byte(mem,Pos)
PointLoc(2,3,2)=memblock byte(mem,Pos+1)
PointLoc(2,3,3)=memblock byte(mem,Pos+2)
endif
local black as byte
Bground=0
black=3
x=1:y=1
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"TopL"
black=3
x=2:y=1
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"TopR"
black=3
x=1:y=4
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"BotL"
black=3
x=2:y=4
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"BotR"
black=3
x=1:y=2
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"LefT"
black=3
x=1:y=3
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"LefB"
black=3
x=2:y=2
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"RigT"
black=3
x=2:y=3
for color=1 to 3
if PointLoc(x,y,color)<>Bground then dec black
next PointLoc
if black<3 then Col$=Col$+"RigB"
Endfunction col$
My computer can render anything no matter the complexity of it in real time.
The memory is enormous. My computer is unique to everyone else’s. What computer do I have?