Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sprite Collision With Map Tiles
#1
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?
Reply
#2
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)