Sprite Collisions (moved from coding questions)

Advanced game related topics
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Sprite Collisions (moved from coding questions)

Post by em_uk »

Hello,

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
My level gets loaded into an array :

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
My sprite moves per pixel and also jumps/falls. For a rough collison I use:

Code: Select all

  Protected offsetX.f = Int(((pl\x+SpriteWidth(sprite)/1.5))/16)
  Protected offsetY.f = Int((pl\y+SpriteHeight(sprite)-16)/16)
I then use the following for checking is a wall is left/right/below/up :

Code: Select all

  ;BELOW
  Blk_below=MapArray(x,y+1)\i   ; block below
  Select blk_below
    Case 7 ; spike!
     ;   Debug "dead"
..ETC
Now everything works very nicely apart from when my player sprite is half way up (say from moving from a wallsprite(2) to nothing (0) - this can happen when jumping or going up a ladder, I am able to ghost through the platforms due to my rough float conversion.

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
And I find it very hit and miss. So what I need to do, if work out how to correctly determin the blocks ahead/behind up and down of my sprite to prevent him in certain circumstances floating into a wall.


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!
----

R Tape loading error, 0:1
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: Sprite Collisions (moved from coding questions)

Post by em_uk »

Solved it myself after maybe 20+ hours trial and error.

Never give up!

:D
----

R Tape loading error, 0:1
Post Reply