CruelNoise [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=5583 Circle-rectangle collision detection? <p>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:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre> -- 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 </pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>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.</p> <p>Does anyone have any workarounds, or am I gonna have accept this limitation?</p> https://www.lexaloffle.com/bbs/?tid=30028 https://www.lexaloffle.com/bbs/?tid=30028 Mon, 02 Oct 2017 14:13:48 UTC