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

Started by
13 comments, last by Dragonsoulj 9 years, 7 months ago
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 : tumblr_inline_nai7fdo1Vo1rxgxsk.png
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) : VGOHLW6.png
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.
Advertisement

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.

zelda005.png

This article is about transitioning terrain tiles:

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

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

There are multiple ways to go about it.
One easy way is to have 2 or 3 layers of tiles. Instead of putting down one tile for each spot on the map, what if there are three (or more) layers of tiles?

Then you can put down solid tiles, like entirely water and entirely grass tiles on the bottom layer, and transitioning tiles on the next layer up.
  • Background layer 1 (water, sand, grass, whatever)
  • Background layer 2 (transition tiles)
  • Background layer 3 (bushes, tree trunks, rocks, etc...)
  • Background layer 4 (anything else needed, like maybe flowers in the bush, or vines wrapped around the tree trunk)
  • <Player and NPCs get drawn next>
  • Foreground layer 1 (tree shadows, wall shadows)
  • 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)
Make sure you permit yourself to place any tile on any layer you want - don't limit what layers the map maker can put tiles on!

Here's an example of how this can improve the visual quality of your game using just three layers:

Layer 1:
Layer_1.png

Layer 2:
Layer_2.png

Layer 1 and 2 drawn together:
Layer_1_and_2_merged.png


Layer 3:
Layer_3.png

All layers drawn together:
All_layers_merged.png

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

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.

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

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

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

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?

Inside_turn.png

(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! wink.png

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.


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. wink.png.

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 happy.png)


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" : nxsmVlj.png

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.

This topic is closed to new replies.

Advertisement