pats
pats

Reputation: 1291

Overlap detection on part of sprite in Libgdx

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.

enter image description here

enter image description here

Upvotes: 2

Views: 454

Answers (2)

dfour
dfour

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.

enter image description here

Upvotes: 1

Tenfour04
Tenfour04

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

Related Questions