Log In  
Follow
CruelNoise
[ :: Read More :: ]

Hey, I'm a relative beginner, and I've been trying to create a general-purpose shape collider for a little project I'm making. I've run into a bit of a problem when trying to make a function for detecting collision between circles and rectangles. The problem is, as far as I can tell, that Pico-8 doesn't have enough floating point precision to accurately calculate the distance between a point and a line (using a formula I copped off of Wikipedia). Here's what I've got:

-- I'm aware these function names are garbage, I'm gonna change them up later.
local function dOfLine(a, b)
  -- takes two points and returns the distance between
  return sqrt((b.y - a.y)^2 + (b.x - a.x)^2)
end

local function dToLine(p, a, b)
    -- takes a point and the two endpoints of a line and returns the length
    -- of the perpendicular between the point and the line.
    num = abs(((b.y - a.y) * p.x - (b.x - a.x) * p.y + b.x * a.y - b.y * a.y))
    return num / dOfLine(a, b)
end

Am I right in thinking that this is a platform limitation? When fed points (4, 7), (1, 5), (1,10) it resolves num as 25, and returns 5, which is of course incorrect.

Does anyone have any workarounds, or am I gonna have accept this limitation?

P#44863 2017-10-02 14:13 ( Edited 2017-10-02 20:37)