Skip to content

Commit 266eb10

Browse files
author
Wouter Commandeur
committed
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/
1 parent 1e9d0b2 commit 266eb10

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/geom/Line.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,13 @@ Phaser.Line.intersectsPoints = function (a, b, e, f, asSegment, result) {
375375
return null;
376376
}
377377

378-
result.x = ((b1 * c2) - (b2 * c1)) / denom;
379-
result.y = ((a2 * c1) - (a1 * c2)) / denom;
378+
/*
379+
Round to 3 decimals here, due to javascript floating point is 'broken'
380+
http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript
381+
See workaround explanation there in accepted answer there..
382+
*/
383+
result.x = Math.round( ((((b1 * c2) - (b2 * c1)) / denom)+0.00001)*1000 ) / 1000;
384+
result.y = Math.round( ((((a2 * c1) - (a1 * c2)) / denom)+0.00001)*1000 ) / 1000;
380385

381386
if (asSegment)
382387
{

0 commit comments

Comments
 (0)