megamarc / Tilengine

Free 2D graphics engine with raster effects for retro/classic style game development
https://www.tilengine.org
Mozilla Public License 2.0
847 stars 95 forks source link

Determining which sprites collided with each other #60

Closed danielgholmes closed 4 years ago

danielgholmes commented 4 years ago

I am currently using Tilengine's collision detection, which works well for certain scenarios. However, from what I can tell, Tilengine has no way of determining exactly which sprite or sprites are colliding with each other. Are there any plans (or would it even be reasonably possible) for Tilengine to determine this? For example, maybe a function that returns an array of sprite indexes which are colliding with a certain sprite.

megamarc commented 4 years ago

Per-pixel sprite collision in tilengine works like true 2D chipsets: just a flag signaling that the sprite is involved in a collision with another sprite. It's implemented as a plain sprite coverage buffer, so there's no way to keep track of more than two overlapping sprites at the same position. It's optimized and its performance is quite good compared to sprites without collision check enabled.

This feature is intended to compliment traditional AABB checks (axis-aligned bounding box): first you check your collisions with traditional overlapping bounding boxes, that's cheap. Once you find a possible collision, you can discard it if both sprites don't have their collision flag set. That's how it's supposed to work.

danielgholmes commented 4 years ago

Thanks for the response. So if I understand correctly, I should implement my own AABB checks and then use the Tilengine collision detection flag to confirm the collision on a pixel level?

megamarc commented 4 years ago

Yes! AABB checks give you a coarse (but in most cases good enough) approximation. Then you can check collision flag on both sprites, and discard the collision if at least one of them is not set. However you can get false positives: in areas where many sprites overlap, maybe two of them pass an AABB check, they're not colliding themselves, but with other sprites, so they have their flag set. However this is some extreme case, and gameplay shouldn't rely on very precise checks.

You should also only enable per-pixel collision checks on collidable items. Decorations, explosions and so should have them disabled

danielgholmes commented 4 years ago

Awesome, I will definitely try this! Just to give some background; I was using the Tilengine collision detection to check for when enemies have a collision, but I had no way of determining if the collision was with another enemy or with the player projectiles. But it seems like AABB will work.

As a side note: I'm not sure if I should rather have asked this question on the Tilengine support forum. It's hard to know sometimes what is more suitable as a GitHub issue. What is your preference?

megamarc commented 4 years ago

Thanks for your interest! My preference is using the forum for questions about how to use tilengine, help, etc. And GitHub specifically for bugs or errors on tilengine itself. The line is blurry sometimes