Reputation: 1291
I have image sprites representing two polygons below. These polygons loosely represent the sprite area. What I want to do is use these polygons to detect the overlap (or collision) of sprites. However the overlap should be valid inside the green square. (this is a jigsaw puzzle game, what I am trying to implement is snapping of puzzle pieces when they are moved closer)
I tried Intersector.overlapConvexPolygons(adjacentPiece.polygon, currentPiece.polygon);
however this one detects overlaps for entire polygon.
Any clever things I can do here to detect the overlap.
Upvotes: 2
Views: 454
Reputation: 1376
If all your jigsaw pieces are a regular size you can simple use normal squares for each jigsaw piece. The square used to define the shape will be halfway between the solid part of the jigsaw piece and the extruded pieces.
From your image I have applied the squares to the pieces shown.
Upvotes: 1
Reputation: 93739
I think your approach might be over-complicating it. If you need your puzzle pieces to bump into each other, you can keep your physics boundaries, but if not, you can remove them entirely.
Either way, to detect if two pieces should snap, you can approximate each piece by a point roughly at the center of each of the piece's four basic sides. To test for pieces being close enough to snap together, you only need to measure the distance between the points on the sides of the two pieces and see if it's smaller than some threshold value you want to use.
If this is a typical puzzle game, you would only need to check this when the player releases a piece, so if it takes a while to brute-force cycle through all the potential matches, it won't really be noticeable because it isn't done while the player is dragging pieces.
Upvotes: 1