Author Topic: Sprite Collision Detection  (Read 3104 times)

0 Members and 1 Guest are viewing this topic.

Offline collechess

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +22/-2
    • View Profile
Sprite Collision Detection
« on: May 02, 2011, 05:35:35 pm »
Is the easiest way to implement collision detection on the graphscreen with 8x8 sprites using pxltest to check for each point the sprite will move to?  If not what is the best way?


  • Guest
Re: Sprite Collision Detection
« Reply #1 on: May 02, 2011, 05:39:18 pm »
TTTT, it really depends on what you're doing.  Can you explain the situation and the thing you're making, so we can make more specific suggestions? ;)

Offline Runer112

  • Project Author
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Sprite Collision Detection
« Reply #2 on: May 02, 2011, 05:42:46 pm »
How about this?

Here is a pretty small method for pixel-based sprite collision checking. Whereas your previous combination of 22 hard-coded pxl-Test() checks weighs in at a whopping 620 bytes, this routine is 99 bytes. As you might expect from this loop-based approach, though, it trades size for speed. Whereas your previous method would take about 9,000 cycles, this method will take about 26,000 cycles for your sprite.

Another great thing about this approach is that it, instead of using a large list of X and Y offsets for each pixel that you want to check, it simply reads from a sprite. This means that this routine can be adapted to any sprite by simply changing the Pic0 reference to point to your 8x8 sprite. However, this also means that is slowed down additionally by checking pixels inside of the sprite, which you may not need to check. You can get rid of these unnecessary checks by having this routine read from an 8x8 sprite only containing the collision points (probably the border). If you added another 8x8 sprite containing the 22 collision points you used in your hardcoded method, you would be adding 8 bytes for the sprite but subtracting about 6,000 cycles from the collision checking.

Code: (99 bytes, ~26,000 cycles if no collision (based on Scout's helicopter sprite)) [Select]
While 1
  -1→T    .that's a subtraction sign
  While 1
    If U^2
      If pxl-Test(X+S,Y+T)
        Goto COL
  End!If S
End!If T