Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check sprite collision with other sprite
How would i go about checking collision with another sprite? I know I can get if a sprite is colliding with anything but how do I check if it's a tilemap or sprite? And if it's a sprite how could I check which specific sprite?

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:

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:

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:

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:

Okay great, thanks so much!

Forum Jump:

Users browsing this thread: 1 Guest(s)