# How to program tile transitions (C++/SFML)

This topic is 1231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello ! (My first post on GameDev.net !)

I started creating a top-down 2D tile based game and i'm currently doing the map system side of the game. I already made several modules, such as loading the map, dividing it in chunks, and each chunk has its own Vertex Array which contains all the tiles in this chunk. This works pretty well, but for now, there is no tile transitions, as seen in this screenshot :
What i want is that tiles have a variant texture when they collide a tile with a different ID (dirt/sand or sand/water for example, but i think you got it). I made a "test" texture (and removed the sand for now) :
When i made this tiles i thought about first drawing the entire tile, and then, in another function, detect if it is needed to change the border, than if it is needed, taking a border and drawing it on the vertex array. It sounds simple but i can't figure out how to do it, i tried all the afternoon to do this, in verifying every tiles if they need borders with the one on their right, down, and down-right, to minimize the loop. This way, if the current tile and the tile on the right are different, it will draw the right border of the current tile, and the left border of the tile on the right.
It looks to me it is a kind of low resources use method to do it, but everything i have done so far is a buggy mess.

Any suggestions on how to do that ? I'd really appreciate it, thanks.

PS: I'm sorry if i made some grammar errors, english is not my native language.

##### Share on other sites

Typically, the transitions are part of the tiles themselves.

For instance, look in the top left of the following tileset. Notice how the grass and water meeting diagonally are part of the same tile. Same thing for sand meeting the grass, and the grass meeting the mountains.

##### Share on other sites

http://www.gamedev.net/page/resources/_/technical/game-programming/tilemap-based-game-techniques-handling-terrai-r934

And this forum post explores it a little further:

http://www.gamedev.net/topic/606520-tile-transitions/

I think they'll shed some light on your problem.

- Eck

##### Share on other sites

Thanks for the responses !

Eck: Thanks for the links, they're really useful

Servant: I thought about layers but only for objects (mostly trees), but putting the transitions in a layer is a really good idea, I'll try to implement it and give you feedback as soon as I can.

Thanks a lot.

PS: Shacktar: I don't want to have tiles with transitions directly in the tileset, what i want is the entire tile, and then the borders of the tiles, as shown in the link Eck gave, but thanks

##### Share on other sites

A possible technique for what Servant describes is named texture splatting. It can be done with a manually generated map but also with a map derived automatically from the surrounding tiles. The latter process is described in detail in Charles Bloom's good ol' article here.

Edited by haegarr

##### Share on other sites

Texture splatting is definitely an option. I was just suggesting tiles with alpha transparency.

In my own 2D tile-based game, I use splatting (using 'mask' tiles), but sometimes simple transparency works just as well.

##### Share on other sites

Hi everyone, I changed my opinion and now each of my rendered tiles are divided in 4, which is pretty simple to do and it let me make transitions easily. Each tile now have 9 textures tiles (full, right, left, top, bottom, and 4 corners). I'm pretty happy with the result I have (even if the textures are ugly for now), I just wanted to let you know what I did.

The only con (cons ?) with this method is that all the borders have to be the same tile/texture (dirt in my case), or if I make different borders (like water/sand or rock/grass) I will be forced to place sand around water (but what if it's a river ? water in rivers collide with dirt and not sand) so I made dirt for all borders, and it looks pretty cool imo.

So thanks again, even if, after rethought, I didn't use any of your solutions, but it made me learn things so that's cool.

Servant: With the layer system, I thought to put trees in a layer drawn after the player, but what if the player is in front of the tree and not behind ? The tree will always be on top of him, how can I fix this ? Any idea ? Maybe drawing entities (player, monsters etc) and the layer with objects like trees from top to bottom, but drawing for all the lines : Player, trees, next line, player, trees, next line etc. ? Thanks in advance

##### Share on other sites

Hi everyone, I changed my opinion and now each of my rendered tiles are divided in 4, which is pretty simple to do and it let me make transitions easily. Each tile now have 9 textures tiles (full, right, left, top, bottom, and 4 corners). I'm pretty happy with the result I have (even if the textures are ugly for now), I just wanted to let you know what I did.

You might find you want more than just those. What about inside-turns?

(And if you want to get more detailed, there are even more possible orientations - but those others are less-critical than corners and inside-corners)

Also, depending on your art style, maybe you can just make one corner and one inside turn, and one edge, and rotate them in SFML. It'd save ya alot of work, if it works with your art style.

The only con (cons ?) with this method is that all the borders have to be the same tile/texture (dirt in my case), or if I make different borders (like water/sand or rock/grass) I will be forced to place sand around water (but what if it's a river ? water in rivers collide with dirt and not sand) so I made dirt for all borders, and it looks pretty cool imo.

Rivers sometimes have sand on their banks also!

A better trick is to make your tiles use actual transparency. Instead of assuming they'll be over dirt, you could make the grass edge tiles actually have transparency, so they can go next to any tile.

Servant: With the layer system, I thought to put trees in a layer drawn after the player, but what if the player is in front of the tree and not behind ? The tree will always be on top of him, how can I fix this ? Any idea ? Maybe drawing entities (player, monsters etc) and the layer with objects like trees from top to bottom, but drawing for all the lines : Player, trees, next line, player, trees, next line etc. ? Thanks in advance

Two possible methods:
A) Sort your objects (including the player) by 'Y' position, and draw them in order from least to greatest.
Or B) Draw the trunks of the tree on a different layer then the tops of the tree.

##### Share on other sites

You might find you want more than just those. What about inside-turns?

Yeah, I forgot to tell that there is 4 more 8*8 textures for each tiles that represents inside corners, you're right.

Rivers sometimes have sand on their banks also!

… Yeah but hum… Yeah. So… Dirt. .

No for real, I think it's look pretty enough like it is now, maybe later I'll give another thought about this but for now, borders are only dirt. (But you're right about the rivers )

A better trick is to make your tiles use actual transparency. Instead of assuming they'll be over dirt, you could make the grass edge tiles actually have transparency, so they can go next to any tile.

With this method, I'll have to draw the colliding tile under the grass (or any other) tile, but if there is a map like this :

G-D

S-G

(G -> Grass, D -> Dirt and S -> Sand)

the corners of the grass will be half dirt and half sand, imo that won't look pretty.

Quote
Servant: With the layer system, I thought to put trees in a layer drawn after the player, but what if the player is in front of the tree and not behind ? The tree will always be on top of him, how can I fix this ? Any idea ? Maybe drawing entities (player, monsters etc) and the layer with objects like trees from top to bottom, but drawing for all the lines : Player, trees, next line, player, trees, next line etc. ? Thanks in advance
Two possible methods:
A) Sort your objects (including the player) by 'Y' position, and draw them in order from least to greatest.
Or B) Draw the trunks of the tree on a different layer then the tops of the tree.

Yeah, what I tried to define was the A method, I think I will go like this. I'll try to find a performance-friendly way to iterate through every objects (even if I don't think there will be thousands of objects, especially just on the view region).

Again, thanks for replying and taking some time to help those who need some.

Edit: I add a little screen of the current "game" :

The dirt and grass in one of the last line are "cut" because borders with other chunks are checked and drawn when the map is created, but when I change a block, only the chunk in which the tile is is updated, but I'll resolve this later.

Edited by Borax Kid

##### Share on other sites

That looks very good!

Yeah, what I tried to define was the A method, I think I will go like this. I'll try to find a performance-friendly way to iterate through every objects (even if I don't think there will be thousands of objects, especially just on the view region).

Pre-sort them. =)

Save your tilemap, but also save your object-map ontop of it. Your object-map contains a list of objects pre-sorted by their y position.

Then, you do this:

//Draw objects north of the player.
for every object with object.y < player.y
{
drawObject();
}

//Draw the player.
drawPlayer();

//Draw objects south of the player.
for every object with object.y > player.y
{
drawObject();
}

Since most graphics APIs make (0,0) be the upper-left corner of the image you are drawing, you actually need to sort by (object.y + object.height), because objects (like trees) might be taller than the player, and you're actually trying to sort by the bottom of the object (the tree trunk, the player's feet, etc...), not the top.

Edited by Servant of the Lord

##### Share on other sites

That looks very good!

Yeah, what I tried to define was the A method, I think I will go like this. I'll try to find a performance-friendly way to iterate through every objects (even if I don't think there will be thousands of objects, especially just on the view region).

Pre-sort them. =)

Save your tilemap, but also save your object-map ontop of it. Your object-map contains a list of objects pre-sorted by their y position.

Then, you do this:

//Draw objects north of the player.
for every object with object.y < player.y
{
drawObject();
}

//Draw the player.
drawPlayer();

//Draw objects south of the player.
for every object with object.y > player.y
{
drawObject();
}

Since most graphics APIs make (0,0) be the upper-left corner of the image you are drawing, you actually need to sort by (object.y + object.height), because objects (like trees) might be taller than the player, and you're actually trying to sort by the bottom of the object (the tree trunk, the player's feet, etc...), not the top.

Yes, that'd work if there was only the player to render, but there will be entities like monsters or animals.

Edited by Borax Kid

##### Share on other sites

It works for anything as long as you sort them by their (y+height) position.

Though, in the case where you have a huge mix of things moving around, I'd probably make trees count as "entities", and then just sort the entire array of "entities" and draw them in one loop. The rest of the terrain could still be tiles.

Or else, you can move to the other (easier) solution:

Draw the trunks of the tree on a different layer than the tops of the tree.

• Background layer 1 (water, sand, grass, whatever)
• Background layer 2 (transition tiles)
• Background layer 3 (bushes, tree trunks (the bottom part of the trunk, anyway), rocks, etc...)
• Background layer 4 (anything else needed, like maybe flowers in the bush, or vines wrapped around the tree trunk)
• <Player and other entities get drawn next>
• Foreground layer 2 (tree tops, so they get drawn OVER the player when he walks behind them, and other things like bridges the player walks under)
• Cloud layer (cloud effects, rain effects, etc...) (This layer is not tiles, but one repeating semi-transparent image that scrolls horizontally and vertically)

It's the less-perfect way, but it works well enough most of the time, as long as most of your entities are consistent sizes.

But it's really not too hard to do it the "perfect" way (sorted the trees with the entities by y pos), if you wanted to put in a little more work to do it right.

Edited by Servant of the Lord

##### Share on other sites

Thanks a lot, everything (at least concerning tiles transitions and Y axis sorting) is done.

I also made another topic for the sorting, if anyone is interested, it's here : http://www.gamedev.net/topic/660632-solved-sorting-a-stdmap/

Edited by Borax Kid