Skip to content

Commit

Permalink
Merge pull request #178 from oakmound/fix/polygon-collides
Browse files Browse the repository at this point in the history
alg\floatgeom: Fix Polygon rect collision overeager singledimension overlap
  • Loading branch information
200sc committed Dec 20, 2021
2 parents 6a1342f + 5e79f98 commit 7cdacfd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions alg/floatgeom/polygon.go
Expand Up @@ -137,15 +137,19 @@ func (pg Polygon2) RectCollides(r Rect2) bool {
dx2 := pg.Bounding.Max.X()
dy2 := pg.Bounding.Max.Y()

overlapX := false
if x > dx {
if x < dx2 {
return pg.OverlappingRectCollides(r)
overlapX = true
}
} else {
if dx < x2 {
return pg.OverlappingRectCollides(r)
overlapX = true
}
}
if !overlapX {
return false
}
if y > dy {
if y < dy2 {
return pg.OverlappingRectCollides(r)
Expand Down
9 changes: 9 additions & 0 deletions alg/floatgeom/polygon_test.go
Expand Up @@ -119,6 +119,15 @@ func TestPolygon2_RectCollides(t *testing.T) {
rect: NewRect2(0, 0, 2, 2),
shouldCollide: true,
},
{
poly: NewPolygon2(
Point2{1.1, 1.1},
Point2{1.1, 1.1},
Point2{1.1, 1.1},
),
rect: NewRect2(0, 2, 2, 2),
shouldCollide: false,
},
}
for i, tc := range tcs {
if tc.poly.RectCollides(tc.rect) != tc.shouldCollide {
Expand Down

0 comments on commit 7cdacfd

Please sign in to comment.