Little wrinkles in PICO-8's language implementation.
Overflow and Distance Calculations[]
Pico-8's numbers are 16.16 fixed point, with a maximum integer value of 32767. This means it's quite easy to overflow numbers when squaring for distance calculations: 181^2==32761, almost overflowing, so even pixels two screens away will overflow a naive distance calculation. One workaround is to move the fixed point artificially, like this:
-- works with x,y up to +/-8191
-- and distance up to 11584 by
-- sacrificing some precision
function dist(x0,y0,x1,y1)
-- scale inputs down by 6 bits
local dx=(x0-x1)/64
local dy=(y0-y1)/64
-- get distance squared
local dsq=dx*dx+dy*dy
-- in case of overflow/wrap
if(dsq<0) return 32767.99999
-- scale output back up by 6 bits
return sqrt(dsq)*64
end