Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetLineToPolygon doesn't work as expected #6467

Closed
Abspirit opened this issue Apr 11, 2023 · 5 comments · Fixed by #6577
Closed

GetLineToPolygon doesn't work as expected #6467

Abspirit opened this issue Apr 11, 2023 · 5 comments · Fixed by #6577

Comments

@Abspirit
Copy link

Abspirit commented Apr 11, 2023

Hello,

I don't know if it's a mistake from my side or not, but it seems that Phaser.Geom.Intersects.GetLineToPolygon does not work with the last vertice (the one which is not closed) of the polygon.

Here's an example
https://codepen.io/Abspirit/pen/PoyPNOK

the green dot is not supposed to be here or I did not get what it's supposed to do ?
And I also don't get why when I use the other "line4" (quoted in the example) it does not work.
(because X1 = X2 ?). I'm lost.

Thanks for your answer

@Abspirit
Copy link
Author

I think I fixed the issue I had with GetLineToPolygon by changing the GetLineToPoints function.

I replaced

var prev = points[0]; for (var i = 1; i < points.length; i++)

by

var prev = points[points.length-1]; for (var i = 0; i < points.length; i++)

So the intersection between the line and the segment composed with the first and the last point of the polygon is now checked.

Here's the new codepen with my fix :
https://codepen.io/Abspirit/pen/xxyZxBr?editors=1111

@Abspirit
Copy link
Author

Well I also confirm that there is no intersection between the line and the polygon when x1 = x2 on the line. I will try to investigate to find where is the issue (Codepen below)

No intersection between the blue line or yellow line with the polygon here (green line intersection can be fixed with the changes I showed above):
https://codepen.io/Abspirit/pen/QWZydKJ?editors=1111

@Abspirit
Copy link
Author

I feel a bit tired to talk alone as i posted that issue one week ago and that nobody answers.. But here's what I found.
I fixed the issue by changing the very bad function "GetLineToLine"...
As I said, when you set a line1 with x1 = x2 that does not work... Because of the line below.. dx1 can not be equal to 0 ? Why not ? And the check is not symetric.. that's not mathematically serious.

    if (dx1 === 0 || denom === 0)
    {
        return false;
    } 

Here's what I changed in the whole function GetLineToLine

I replaced

  if (dx1 === 0 || denom === 0)
    {
        return false;
    }

    var T2 = (dx1 * (y3 - y1) + dy1 * (x1 - x3)) / (dx2 * dy1 - dy2 * dx1);
    var T1 = (x3 + dx2 * T2 - x1) / dx1;

    //  Intersects?
    if (T1 < 0 || T2 < 0 || T2 > 1)
    {
        return null;
    }

by

    if (denom === 0)
    {
        return false;
    }
    var T2 = (dy1 * (x3 - x1) - dx1 * (y3 - y1)) / denom;
    var T1 = (dy2 * (x3 - x1) - dx2 * (y3 - y1)) / denom;
    //  Intersects?
    if (T1 < 0 || T2 < 0 || T2 > 1 || T1 > 1)
    {
          return null;
    }

Source : https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
Paragraph : "Given two points on each line segment"

To resume, I had a problem with the GetLineToPolygon function because of two errors:

  • GetLineToPoints works by pair but does not check the pair composed by the first and the last point of the serie.
  • GetLineToLine had an asymetric check of dx1 and wrong calculation.

Here's the pen with everything working normally (I hope)
https://codepen.io/Abspirit/pen/ZEqOMgp?editors=0011

@Abspirit
Copy link
Author

It seems you fixed one of the issues I mentioned in the Phaser 3.6 version and I can see it in the pen below.
"_Geometry Bug Fixes : Fixed issue in Geom.Intersects.GetLineToLine function that would fail with colinear lines"

However, it seems that GetLinetoPolygon still not check all vertices in 3.6 (green line).
https://codepen.io/Abspirit/pen/vYVmMzY

@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants