I am currently making a 2D platform game, I well into the mechanics of my game engine and all is going well. Apart from one thing I just cannot nail!
COLLISION DETECTION!
I am almost convinced that SpriteCollision doesn't work as expected (however I am sure I will be told otherwise) but its more likely to do with my maths.
A little background :
My screen size is 640x480. My player is 17wx26h : My level is comprised of 16x16px blocks.
I am only showing portions of the code!
...
I load a background sprite strip, with 17 16x16px tiles into a linkedlist :
Code: Select all
imageA=CatchSprite(#PB_Any,?bg_spr)
img_w=SpriteWidth(imageA)/16
For a = 0 To img_w
AddElement(Sprite())
imageA=CatchSprite(#PB_Any,?bg_spr,#PB_Sprite_PixelCollision)
image=CopySprite(imageA,#PB_Any,#PB_Sprite_PixelCollision)
ClipSprite(image,img_w*a,0,16,16)
With Sprite()
\fname=fname$
\imageid=image
\id=ListSize(Sprite())
\active=0
\sprid=image
EndWith
Next
Code: Select all
Dim MapArray.leveldata(30,40)
For y = 0 to 30
for x = 0 to 40
NextElement(Level())
MapArray(x,y)\i = Level()\Ldata (this would be a number from 0 - 17 depending on the block needed)
Next
Next
I then draw my map to screen :
For ymap=0 To 30
For xmap=0 To 40
If MapArray(xmap, ymap)\i
If MapArray(xmap, ymap)\i=22
MapArray(xmap, ymap)\i=2
SelectElement(Sprite(),1)
Else
SelectElement(Sprite(),MapArray(xmap,ymap)\i-1)
EndIf
DisplayTransparentSprite(Sprite()\imageid,(xmap*16),ymap*16)
EndIf
Next
Next
Code: Select all
Protected offsetX.f = Int(((pl\x+SpriteWidth(sprite)/1.5))/16)
Protected offsetY.f = Int((pl\y+SpriteHeight(sprite)-16)/16)
Code: Select all
;BELOW
Blk_below=MapArray(x,y+1)\i ; block below
Select blk_below
Case 7 ; spike!
; Debug "dead"
..ETC
So my plan was to check using SpriteCollision and workout the sprite used for the background and see if I get a hit :
Code: Select all
Blk=MapArray(Int(PL\x+8)/16,Int(PL\y)/16)\i
SelectElement(sprite(),Blk)
If BLK>0
hit = SpriteCollision(sprite,pl\x,pl\y,sprite()\sprid,PL\X,PL\Y)
if hit
debug BLK
endif
So whats the magical piece of maths that I am missing here? Please help! I cannot survive staying up until 4am trying to figure it out any more!