Jump to content

  • Log In with Google      Sign In   
  • Create Account


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

  • You cannot reply to this topic
14 replies to this topic

#1 Borax Kid   Members   -  Reputation: 123

Like
0Likes
Like

Posted 23 August 2014 - 03:55 PM

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.

Sponsor:

#2 shacktar   Members   -  Reputation: 706

Like
1Likes
Like

Posted 23 August 2014 - 07:43 PM

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



#3 Eck   Members   -  Reputation: 2065

Like
4Likes
Like

Posted 23 August 2014 - 09:10 PM

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



#4 Servant of the Lord   Crossbones+   -  Reputation: 18483

Like
12Likes
Like

Posted 23 August 2014 - 10:24 PM

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
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#5 Borax Kid   Members   -  Reputation: 123

Like
0Likes
Like

Posted 25 August 2014 - 04:58 AM

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 



#6 haegarr   Crossbones+   -  Reputation: 4155

Like
2Likes
Like

Posted 25 August 2014 - 05:10 AM

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, 25 August 2014 - 09:57 AM.


#7 Servant of the Lord   Crossbones+   -  Reputation: 18483

Like
0Likes
Like

Posted 25 August 2014 - 09:28 AM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#8 Borax Kid   Members   -  Reputation: 123

Like
0Likes
Like

Posted 28 August 2014 - 08:32 AM

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



#9 Servant of the Lord   Crossbones+   -  Reputation: 18483

Like
0Likes
Like

Posted 28 August 2014 - 11:48 AM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 Borax Kid   Members   -  Reputation: 123

Like
0Likes
Like

Posted 28 August 2014 - 04:27 PM


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, 28 August 2014 - 04:33 PM.


#11 Servant of the Lord   Crossbones+   -  Reputation: 18483

Like
1Likes
Like

Posted 28 August 2014 - 04:59 PM

That looks very good! smile.png 
 

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, 28 August 2014 - 05:05 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#12 Borax Kid   Members   -  Reputation: 123

Like
0Likes
Like

Posted 29 August 2014 - 12:29 AM

That looks very good! smile.png 
 

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, 29 August 2014 - 06:53 AM.


#13 Servant of the Lord   Crossbones+   -  Reputation: 18483

Like
0Likes
Like

Posted 29 August 2014 - 10:08 AM

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 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)

 

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, 29 August 2014 - 10:16 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#14 Borax Kid   Members   -  Reputation: 123

Like
0Likes
Like

Posted 05 September 2014 - 11:34 AM

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, 05 September 2014 - 12:20 PM.


#15 Dragonsoulj   Crossbones+   -  Reputation: 2084

Like
0Likes
Like

Posted 05 September 2014 - 03:34 PM

Just to add a potential option: http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/advanced-terrain-texture-splatting-r3287







PARTNERS