From 266eb10765a9fe1619093c47d19d7c4d6bec9e5c Mon Sep 17 00:00:00 2001 From: Wouter Commandeur Date: Sat, 31 May 2014 12:13:59 +0200 Subject: [PATCH] Fix Phaser.Line.intersectsPoints for floating point inaccuracy. Round the result to 3 decimals, should be enough precision and solves the problems. See: http://www.html5gamedevs.com/topic/6840-phaserlineintersects-does-not-work-for-floats/ --- src/geom/Line.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/geom/Line.js b/src/geom/Line.js index 75f21de85..10da09506 100644 --- a/src/geom/Line.js +++ b/src/geom/Line.js @@ -375,8 +375,13 @@ Phaser.Line.intersectsPoints = function (a, b, e, f, asSegment, result) { return null; } - result.x = ((b1 * c2) - (b2 * c1)) / denom; - result.y = ((a2 * c1) - (a1 * c2)) / denom; + /* + Round to 3 decimals here, due to javascript floating point is 'broken' + http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript + See workaround explanation there in accepted answer there.. + */ + result.x = Math.round( ((((b1 * c2) - (b2 * c1)) / denom)+0.00001)*1000 ) / 1000; + result.y = Math.round( ((((a2 * c1) - (a1 * c2)) / denom)+0.00001)*1000 ) / 1000; if (asSegment) {