Log In  


Hi. i found that code https://mboffin.itch.io/pico8-overlap
that checks for overlap.

But my initial thought was in term of sprites.
Since we can define sprites is there a simpler way to detect
when one sprite touched or overlapped another?



I'm not sure what you mean by sprite, like a 8x8 box?
I personally can't think of anything that's all-around use simpler than AABB but there's lots of ways of handling collision. I've seen stuff, I've seen more complex hitbox updating and pushing stuff, quarter frame checks, SAT collision, etc. lots of stuff in the field


I mean if there is a function like collision = SpritesCollide(A,B) that would return true or false.
By sprite i mean the sprites that we define in the sprite editor and use in the function spr()


So, I don't recommend using this. On the contrary, I highly recommend against using this rather than simple boxes, unless the only thing you're using it for is checking whether bullets hit a moving character. Pixel-perfect collision detection is pretty much always asking for trouble for anything else. However, that's your choice as a designer, so here's an implementation, adding in x and y coordinates since there's not much use if you can't move the sprites around. I don't know if it's optimal in either speed or tokens, but I have done some simple tests for if it works.

(x1, y1) would be the top-left corner of where sprite A is being placed
(x2, y2) would be the top-left corner of where sprite B is being placed

function spritescollide(x1, y1, a, x2, y2, b)

  local xdiff, ydiff = abs(x1-x2), abs(y1-y2)
  if (xdiff > 7 or ydiff > 7) return false --in this case they're just not close enough
  local xstop = 7 - xdiff

  -- calculations to determine the area that needs checking and how it relates to each sprite
  local ystart = y1 < y2 and ydiff or 0
  local ystop = 7 - ydiff + ystart
  local yoff = ydiff - ystart
  local xoff1 = x1 < x2 and xdiff or 0
  local xoff2 = xdiff - xoff1
  for y=ystart, ystop do

  -- fetch, isolate and align the relevant portion of each row of pixels
    local row1 = ($((a % 16) * 4 + (a \ 16) * 512 + y * 64) >>> xoff1 * 4) << xdiff * 4
    local row2 = ($((b % 16) * 4 + (b \ 16) * 512 + yoff * 64) >>> xoff2 * 4) << xdiff * 4

  -- check each pixel. if they're both non-0 at any point, they overlap
    for x=0,xstop do
      if ((row1 & (0xf000>>>x))~=0) and ((row2  & (0xf000>>>x))~=0) then
        return true
      end
    end

    yoff+=1
  end

  -- if they never both have a pixel in any spot, they don't overlap
  return false
end

@chomwitt, this was of interest a few years back.

See the full thread as my 'fastest' method which was been beaten by far and the new 'bitmask' code may be of use to you for speedy collision checks.

https://www.lexaloffle.com/bbs/?pid=70253


Thanks very much!!



[Please log in to post a comment]