Skip to content

Commit

Permalink
Fix Intersector.intersectPolygons() corner case (#7278)
Browse files Browse the repository at this point in the history
  • Loading branch information
obigu committed Feb 19, 2024
1 parent 0de4d89 commit 7bed7d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion gdx/src/com/badlogic/gdx/math/Intersector.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ public static boolean intersectPolygons (Polygon p1, Polygon p2, Polygon overlap
floatArray2.addAll(floatArray);
floatArray.clear();
}
if (floatArray2.size != 0) {
// Check for 3 or more vertices needed due to floating point precision errors
if (floatArray2.size >= 6) {
if (overlap != null) {
if (overlap.getVertices().length == floatArray2.size)
System.arraycopy(floatArray2.items, 0, overlap.getVertices(), 0, floatArray2.size);
Expand Down
14 changes: 11 additions & 3 deletions gdx/test/com/badlogic/gdx/math/IntersectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class IntersectorTest {

Expand Down Expand Up @@ -223,4 +221,14 @@ public void testIsPointInTriangle3D () {
new Vector3(50.008057f, 22.20586f, 124.62208f), new Vector3(62.282288f, 22.205864f, 109.665924f),
new Vector3(70.92052f, 7.205861f, 115.437805f)));
}

@Test
public void testIntersectPolygons () {
// Corner case with extremely small overlap polygon
Polygon intersectionPolygon = new Polygon();
assertFalse(
Intersector.intersectPolygons(new Polygon(new float[] {3200.1453f, 88.00839f, 3233.9087f, 190.34174f, 3266.2905f, 0.0f}),
new Polygon(new float[] {3213.0f, 131.0f, 3214.0f, 131.0f, 3214.0f, 130.0f, 3213.0f, 130.0f}), intersectionPolygon));
assertEquals(0, intersectionPolygon.getVertexCount());
}
}

0 comments on commit 7bed7d3

Please sign in to comment.