Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check sprite collision with other sprite
#2
Hi,

Sprite collisions -with both tiles and other sprites- is a gameplay mechanics topic. As Tilengine is a graphics engine, not a full game engine, it won't directly solve collisions. However it will assist you in implementing the suitable kind of collisions required by your game.

Common way to deal with collisions is with so called "Axis-Aligned Bounding Box" -or AABB for short-. It consists on surrounding your objects inside rectangles, and check relative positions of these rectangles. When they overlap, there's a collision.

Here's a great in-depth article on this subject:
http://higherorderfun.com/blog/2012/05/2...atformers/


vs Sprite
AABB is fast but it can be imprecise, depending on the sizes of the "hitbox" relative to the sprite graphic and the distribution of transparent pixels, you can have false positives -detect a collision where no actual pixels are colliding- or false negatives -missing an actual collision-. For many games this is just ok, but if you need extra precision, Tilengine supports pixel accurate collision detection. Just as real 2D video processors, it will tell if a given sprite is colliding with another on a per-pixel basis, but it cannot tell which is/are the other sprites involved.

So let's suppose you want to check if OB1 and OB2 collide:
  • Do standard AABB betweeb OB1 and OB2 -check if their hitboxes overlap-
  • If they overlap, check if BOTH OB1 and OB2 have their collision flag set.
  • If so, OB1 and OB2 are colliding on a per-pixel basis.
This is the docs reference for enabling pixel accurate sprite collision:
http://www.tilengine.org/doc/md_sprites....totoc_md99

vs Foreground Tiles
Doing sprite vs tiles means examining the tiles that overlap with the area of a sprite
A common optimization is to check only the tiles that overlap with the edges facing the motion vector of the sprite. For example:
  • if your object is moving to the right: only check the tiles on the right edge of the sprite
  • if it's jumping: check the top edge
  • if it's falling: check the bottom edge
And so

Here's the documentation for querying tile data:
http://www.tilengine.org/doc/md_layers.h...totoc_md47

For a working example, you can check source code of my sister project Python Platformer. It uses the python binding, but you'll get the idea. Sprite vs sprite and vs tile layer are demosntrated:
https://github.com/megamarc/TilenginePythonPlatformer

Regards,
Reply


Messages In This Thread
RE: Check sprite collision with other sprite - by megamarc - 05-05-2021, 07:14 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)