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?
![](/gfx/set_like0.png)
![](/gfx/top_drop.png)
![](https://www.lexaloffle.com/bbs/files/25532/icon.png)
Squares are definitely very prone to overflow but...
in your case, the formula is incorrect (the term b.x a.y - b.y a.y is wrong) :)
Correct version:
function dist2line(r,a,b) local dx=b.x-a.x local dy=b.y-a.y local d=sqrt(dx*dx+dy*dy) return abs(dx*(a.y-r.y)-dy*(a.x-r.x))/d end print(dist2line( {x=4,y=7}, {x=1,y=5}, {x=1,y=10})) -- prints 3 |
[Please log in to post a comment]