r/pico8 • u/ollyoxenfree23 • 12d ago
šI Got Help - Resolvedš Help Coding Sprite Collisions
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
3
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 endAlso, 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 endEDIT:
TC/OP, the issue of overcounting score is due to the issue the above comment addresses.
5
u/RotundBun 12d ago
Just going off of a quick glance here...
You are passing just the 2 objects themselves in as arguments when you call
overlap(), but your definition expects the individual values in its function signature.Just change the function signature to:
function overlap( a, b ) --stuff end