2
votes
if (points.size() >= 3) {
   for (int i = 1; i <= points.size() - 1; i++) {
      if (Intersector.intersectLines(points.get(0), points.get(1), points.get(i), points.get(i + 1), null)) return true;
   }
}
return false;

I cant find my error with this, so it seems like the method intersecLines(), which as you could guess checks if two lines have one or more common points, returns wrong results.

Im using this in a Snake-kind-of game, and this method should check if the line between the first and the second point of this "snake-path" (which is existing as an ArrayList consisting of the points of the path) intersects with one of the other lines, which the snake consists of.

The parameters are: Vector2 first Point of first line; Vector2 second point of first line; Vector2 first point of second line; Vector2 second point of second line; Vector2 that will be set to the intersecting point.

For those of you who are not familiar with GDX, it is a framework used for cross platform development in java, most of the methods are implementations of openGL ES methods. Heres the api reference of the Intersector class: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html

2

2 Answers

3
votes

intersectLines actually is a RayRay Collision check, where the ray is defined by two points on a line. The documentation is not very clear at this.

you may want to use intersectSegments instead, which works with line segments

0
votes

Looking at the code, it would seem that the intersectLines method checks lines, not line segments which I think is what you're looking for.

Look at this excerpt specifically:

float d = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
if (d == 0) return false;

This returns false if and only if the two lines slopes are equal (the lines are parallel). Notice that it's equivalent to:

if ((y4 - y3) / (x4 - x3) == (y2 - y1) / (x2 - x1)) return false;

For your purposes, you'd need to use a custom method or add in some post processing. For example, if you added in the fifth parameter to the method, and then checked if that intersection point lied between two of your points, that would suffice.

I'm not sure exactly how your points are stored, but it might also be sufficient to check if any of the endpoints are equal.