Tilengine - The 2D retro graphics engine forum
Sprite Collision With Map Tiles - Printable Version

+- Tilengine - The 2D retro graphics engine forum (http://tilengine.org/forum)
+-- Forum: English forums (http://tilengine.org/forum/forumdisplay.php?fid=3)
+--- Forum: Support (http://tilengine.org/forum/forumdisplay.php?fid=7)
+--- Thread: Sprite Collision With Map Tiles (/showthread.php?tid=114)



Sprite Collision With Map Tiles - Daniel H. - 06-19-2018

Hi,

I'm trying to get collision to work between a sprite and a map tile. From reading the docs, it would seem that there is no built in Tilengine functionality to check for this kind of collision. Am I correct in saying this? I turned to whatever examples I could find. The best I could find was from the Super Mario Clone in Player.cs.

 
Code:
      if (y < oldy)
        {
            floor = false;
            int[] points = { 4, 12 };
            int c;
            for (c = 0; c < points.Length; c++)
            {
                World.Tile tile = world.GetTile(x + points[c], y);
                if (tile.Type == World.Type.Solid || tile.Type == World.Type.Question)
                {
                    y = (y + 16) & ~15;
                    yspeed = 0;
                    t0Jump = 0;
                    if (tile.Type == World.Type.Question)
                        new Bumper(world, Game.objects, 0, tile.Row, tile.Col, 51);
                }
                else if (tile.Type == World.Type.Coin)
                {
                    world.ClearTile(tile.Row, tile.Col);
                    new Collect(world, tile.Col * 16, tile.Row * 16);
                    Game.AddCoin();
                }
            }
        }

Am I on the right track and are there any other examples of how this can be done?


RE: Sprite Collision With Map Tiles - megamarc - 06-22-2018

Hi Daniel!

You're on the right way. There's no built-in mechanism to do sprite vs playfield collision detection, but there are some helper functions to aid you. The usual way is to define some "hot points" in your sprite, usually spaced by the same amount of pixels than the tileset you're checking collision. Then test the suitable points, depending on the motion direction, in which tiles they fall and check their types. This is what SuperMarioClone and PythonPlatformer sister projects do.