cocos2d-android collision map

| | August 5, 2015

How collision map? I have call:

// Get TMX Map and associated layers/groups
_map = CCTMXTiledMap.tiledMap("mapdemo.tmx");
_background = _map.layerNamed("Background");
_objects = _map.objectGroupNamed("Objects");

people can help me. check character when collision _object.

One Response to “cocos2d-android collision map”

  1. There is a tutorial HERE that will help you using Tiled Map to create collisions as you are using this technology.
    Basically, if you are using Tiled Map you will mostly do Pathfinding, when you calculate the closest path to move to X,Y position, you need to check if this path contains some “collider” tiles, using a method like:

    -(BOOL) isCollisioning:(CGPoint) position{
        CGPoint tileCoord = [self tileCoordForPosition:position];
        int tileGid = [_meta tileGIDAt:tileCoord];
        if (tileGid) {    
            NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
            if (properties) {        
                NSString *collision = properties[@"Collidable"];
                if (collision && [collision isEqualToString:@"True"]) {            
                   return true;
                }
           }
       }
       return false;
    }
    

    So the tile is not really a physical body, but you will not be able to go inside it. Of course, it means you have prepared your Tilemap accordingly (link explains how to with a step by step).

    If you wanted to make each of the squares collidable, which I don’t recommend for performance and complexity issues, you will have to use an external library (for example Box2D) and create a phyisic body for each collidable tile in your tilemap (of course you should try to make bigger physic bodies if you have them together, to improve performance).

Leave a Reply