8

I'm struggling with implementing a collision detection system through the tiledmap. I have a 2d "pokemon style" game that has a tiled map rendered. Specifically, I have a 'collision' layer in my tiled map .tmx file that I want to interact with the player and other entities. My question is how do I connect the player sprite (extends Sprite class) to the 'collision' layer of the tiledmap and cause collision between the two. Any advice is appreciated.

bhafenri
  • 140
  • 1
  • 1
  • 5

1 Answers1

15

First of all your Player should probably not extend Sprite, because your player is usually much more than a Sprite. It probably consists of several sprites or even Animations. Keep a sprite as a property of the player.

The question itself has already been adressed several times. You usually need the following steps:

  1. Find the collision layer in your map
  2. Extract all objects from this layer
  3. Check each of those objects for a collision

In code this might look a bit like this:

int objectLayerId = 5;
TiledMapTileLayer collisionObjectLayer = (TiledMapTileLayer)map.getLayers().get(objectLayerId);
MapObjects objects = collisionObjectLayer.getObjects();

// there are several other types, Rectangle is probably the most common one
for (RectangleMapObject rectangleObject : objects.getByType(RectangleMapObject.class)) {

    Rectangle rectangle = rectangleObject.getRectangle();
    if (Intersector.overlaps(rectangle, player.getRectangle()) {
        // collision happened
    }
}

Some more links which you might be interested in:

noone
  • 19,520
  • 5
  • 61
  • 76
  • I implemented your code, but objects returns a count of 0 and the loop is never run. Any idea on what that could be from? I don't have any objects in my layer, I just have a layer that acts as the background that requires no collision and a layer that acts as the tiles the player can collide on. – bhafenri Nov 19 '13 at 19:45
  • When you wrote "I have a 'collision' layer" I assumed that you used an object layer in TilEd for that. You probably should do it that way, because it's really quite comfortable to model your collisions and also easy to use it in the code. With your approach you would have to iterate through your tiles instead of the objects and create a rectangle yourself for all tiles in the collision layer. I still advise you to use the object layer instead. – noone Nov 19 '13 at 20:19
  • Will do, thanks for the help! Finally figured it out. – bhafenri Nov 19 '13 at 20:38
  • @noone doesn't it take too much resources if we check `overlaps` for each rectangle every frame? I have seen a way with an array of 1 or 0 to detect if the tile is walkable or not. Have you experienced some issues (on mobile for example) with the overlaps method on many objects? – Paul Jan 18 '14 at 19:23
  • 1
    @Paul I think simple bounding box collision checks are nothing to worry about, even on mobile devices and with many rectangles. You might optimize it, but you should not pre-optimize, as long as you don't run into problems and profiled where they come from. – noone Jan 18 '14 at 20:22
  • 1
    Theres nothing stopping you extracting sll the shapes out at loadout and then building a quadtree or similar to hasten a collision search, if your expecting a lot of complexity – Shayne Jul 06 '19 at 00:37