How to determine if two sprites are overlapping?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Diet Estus

I can determine if my Player’s bounding box overlaps an Enemy’s bounding box. But I want a finer-grained determination of overlap.

Is there a way to tell if two sprites are overlapping?

Note: I am not interested in creating complex polygons on top of the sprites, then checking the polygons for overlap. The sprites are animated, so a single polygon per animation wouldn’t work.

:bust_in_silhouette: Reply From: eons

The only way to do it is by doing a complex system on top of sprites, polygons will be the easiest and better performant way.

To do that you will need to pick every single pixel on the image data, translate to world position, and compare against the position of every single pixel on the other image.

Usually, this is not needed in games (and less with animated things), it do not offer anything to the players and perfection can be annoying to them too (plus the low performance of that kind of check), is generally better to do rough polygon or simple boxes, always favoring the gameplay, and work with that.

Bodies and areas can have many shapes, and use some of these shapes can be related to the animations, for shape comparison when the bigger box of the 2 elements is overlapping.

I agree with this. Usually a bounding box is preferable and perfectly good enough, even if the sprite is animated. Sometimes the BB animates to accommodate to the frames, and sometimes that’s not necessary.

I wonder, though: I know CollisionShape2Ds can be animated, as far as position, rotation, etc, go, and I suppose CollisionPolygons can be animated too, but I never tried animating their actual vertices. I wonder if that can be achieved, as that might be a solution for the OP.

woopdeedoo | 2018-05-15 22:53

CollisionPolygons can be animated but the physics server may not notice it fast enough for detections, also the polygon making may get glitchy, a convex polygon shape may be better but the use is limited.

eons | 2018-05-22 05:38