Jump to content
  • Advertisement
Sign in to follow this  
Borax Kid

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

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

If you intended to correct an error in the post then please contact us.

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 : 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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.

Share this post


Link to post
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 this post


Link to post
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?

 

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.

Share this post


Link to post
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. 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.

Edited by Borax Kid

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!