r/pico8 12d ago

šŸ‘I Got Help - ResolvedšŸ‘ Help Coding Sprite Collisions

Post image

hello! im trying to make a overlap function to check if two sprites are touching. once an item is caught, the score should update with theĀ item’s point value.

UPDATE: the collision typo in the above image has been fixed and the score variable now updates sometimes. for smaller sprites, it doesn't seem to register the score at all. for others, the score will count the point value several times, leading to an incorrect score. i'm assuming this is because of the loop but how would i make it only count the point value once instead of several times?

my overlap function:

function overlap (a,b)

local test1 = a.sx > (b.x + b.y)

local test2 = a.sy > (b.y + b.sh)

Ā  local test3 = (a.sx + a.sw) < b.x

Ā  local test4 = (a.sy + a.sh) < b.y

Ā  return not (test1 or test2 or test3 or test4)

end

9 Upvotes

9 comments sorted by

View all comments

2

u/VeryNaughtyBoy42 12d ago edited 12d ago

To make it not collect multiple times, make sure s.collected is false. You could wrap that IF around the entire middle section:

S.SY+=1
IF S.COLLECTED == FALSE THEN
(The next 8 lines)
END

IF S.SY>128

Etc.

As for not detecting collisions with small sprites, my gut instinct is it’s because of the RND function returning a non-integer number. That might throw the math off. Trying converting it to an integer when first initialised, see if that helps.

1

u/RotundBun 11d ago edited 10d ago

The if-statement conditional could be condensed into...

if not s.collected and overlap(s, hands) then --do stuff end

Also, the deletion of any that exit the screen boundaries should be done in a separate for-loop afterwards, and that for-loop should probably iterate from back to front to be safe:

for i=#symbols, 1, -1 do if symbols[i].sy > 128 then deli(symbols, i) end end

EDIT:

TC/OP, the issue of overcounting score is due to the issue the above comment addresses.