user1818410
user1818410

Reputation: 41

libgdx collision with line and polygon (Intersector)

My libgdx game has a laser and I want to know if the beam hits it's target.

I had some trouble with the function (always getting true) so I made a very simple test and I still get true!

Did I miss something?

Why is this function returning true?

if(Intersector.intersectLinePolygon(new Vector2(100, 100), new Vector2(200, 100), new Polygon(new float[] {0, 0, 5, 0, 5, 5}))) {
    System.out.println("true");
}

Thanks in advance!

Upvotes: 1

Views: 516

Answers (1)

Phil Anderson
Phil Anderson

Reputation: 3146

I got caught by this when I first used Intersector too.

The intersectLinePolygon() method works on a line that extends infinitely, and not just between the two points you specify.

Use the intersectSegmentPolygon() method which does what you want...

if(Intersector.intersectSegmentPolygon(new Vector2(100, 100), new Vector2(200, 100), new Polygon(new float[] {0, 0, 5, 0, 5, 5}))) {
    System.out.println("true");
}

Upvotes: 1

Related Questions