Procedural TileMaps?

Procedural TileMaps?
0.0 0


Has anyone implemented procedurally generated tilemaps in Cocos?
What was your approach?
Construct TMXMapInfo etc. structures yourself and feed to the supplied TMXTiledMap nodes, or something else?


Figure what you plan to do already?

I’ve used TMXTiledMap directly for the only non-asset procedurally generated map.

A base map is loaded. I think generally just to easily set up the few TMX data structures.

I believe the layers are set up using Tiled where the map/layer size is defined, and all layers are added, but the tiles themselves are left empty (transparent w/grid when displayed in Tiled app).

Also the Tilesets are pre-setup in the .tmx file as well along with the layers as noted.

Then during the generation a few outside data structures are created (a couple 2d arrays for tiles, and a few lists for objects/entities/etc). Then those are used for a good portion of gameplay (pathfinding, etc). While initializing these arrays/lists we also set the tile GIDs.

Literally just using setTileGID.

// get layer from TMXTiledMap getLayer("name");
auto layer = ...;
// get tileset's "zero" GID (since GIDs are unique across all tilesets)
int firstGid = layer->getTileSet()->_firstGid;

// loop over tiles, or otherwise find a tile coord to set, figure out what tile in the tileset you want 
Vec2 tileCoord { 5, 4 }; // or { row, col }, etc
int gidOffset = ... tilesetOffsetForSpecificTileType;
metaLayer->setTileGID(firstGid + gidOffset - 1, tileCoord, (TMXTileFlags)0);
// could define a TMXTileFlags for flip/rotation

Code if you dare (no warranty, heh). .

Could be more data-driven as desired. I’ve defined a set of common tile types across multiple tilesets for each “zone or planet biome”, for example. Also use hidden layers for meta-data like collisions/walkables, and such.

Again with most of what I write ends up being what I consider pragmatic :smiley: and is thus somewhat of a hack or using a hammer (cocos2d classes) for everything, instead of the superior solution of writing your own Map data+rendering from scratch instead (maybe using the .tmx parser, though there are better ones on the Tiled site). It felt like this was the quicker solution, and if needed in the future can always re-write some or all to get improved tile map capabilities.

Most often I’m dealing with designed tilemaps, so this isn’t a high-priority.


Pretty much this :slight_smile:
… once I get back to it :roll_eyes:

Code will be handy, thanks.

The tilemaps I’ll be generating will have randomly sized playable areas using impassible walls to block off the inaccessible areas . I guess the “base” tilemap I load should be saved at the maximum size? That might only be 30x30 tiles anyway.

What’s the difference between a TMXTiledMap and FastTMXTiledMap? I notice you use the former.


Actually I believe I’m using a modified version of FastTMXTiledMap.

Basically TMXTiledMap uses the older SpriteBatchNode for rendering (texture atlas). It’s older. It has the restrictions of SpriteBatchNode. It’s the original.

FastTMXTileMap uses the newer Triangle (or Primitive) Command for rendering. I believe this one supports depth buffer and having tiles with varying Z-ordering. It also can cull tiles outside the screen, optionally (unsure how well this works, and may actually be less performant if cull costs on CPU are higher than cost to render excess tiles). It’s the experimental (also in that namespace) version.

They’re probably mostly identical until you want to modify the map, get a Sprite node for a given tile(s) and then effect on that sprite (Action, Animate, etc).

I have a custom version of FastTMXTileMap, STTMXTileMap (and Layer), which is customized for isometric and adds some simplified lighting capabilities among other z-ordering tweaks.

Switch between mine and cocos2d-x’s version:

typedef STTMXTileMap SCTMXTiledMap;
typedef STTMXTileLayer SCTMXLayer;
//typedef cocos2d::experimental::TMXTiledMap SCTMXTiledMap;
//typedef cocos2d::experimental::TMXLayer SCTMXLayer;


As for the “base” tilemap.

Yeah, you’ll want the base map to have your maximum size so that it parses in and you can set any tile’s GID without worrying about how to change a given map/layer’s size.

And anything smaller than, I believe, 75x75 per layer should be fine. It’s an OpenGL limit we’ve discussed somewhere around here before based on some combo of tile rows+columns+layers and 128x128 rings a bell as too large for the cocos2d-x’s parser. There’s two issues. The OpenGL vertex limit per draw call so now that I think about that it’s likely a given layer can’t have more than 128x128 (or some combo of R x C that matches total number of quads). The second issue would occur if the tile GID unique number overflowed being a 32-bit integer … and the top 3 bits are the flip flags, so it’s more like 2^29 limit.

Anyway, there are limits, but for smaller map sizes you should be totally fine. And you can always stitch them together if that makes sense.

For collisions and other meta-data I tend to prefer parsing that into my own custom array(s) such that the TMXTiledMap is only used for the rendering.

Note: The rest of this may be of no interest or use.
(figure maybe someone find use, otherwise my personal reference to come back to)

I experimented with chunking and using multiple map instances to make larger maps, but really if you need to render larger than say 50x50x5layers or 100x100x2layers at any given moment you should probably write your own tile map renderer.

If you need larger maps you could also checkout Kobold’s TilemapKit … maybe find Steffen and ask for a copy of the source now that it’s been abandoned. He was supporting ‘infinite’ tilemaps and lighting, among other things. (tile map stuff not in sources, afaict)

Here’s the source I paid him early bird price for, to support as I never really planned to use it until it was finished.
I don’t believe the source is accessible, but I think I may have backed up a copy if you were interested we could ask him to grant license to use.

I’ve also done chunking in Unity where I also created the mesh+vertices manually … I think I threw that up on github.

Sorry for the lengthy post, but I’ve been researching/studying/hacking on tile maps for most of game dev career. I should’ve probably made an awesome renderer and editor by now, but meh I’m too lazy for that, heh.

Unity chunked tilemap sample testing prototype. .
Relevant Classes: .

As always, probably better solutions, code, etc out there.

Speaking of … apparently I’ve had ambition to improve cocos2d-x’s tilemap, but never got around :smiley:
(nothing in this repo, sadly) .