Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: 2D or 3D?  (Read 4130 times)

0 Members and 1 Guest are viewing this topic.

frienzy

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
2D or 3D?
« on: March 12, 2021, 09:28:23 pm »
I want to implement the amplitude vector to my tilemap. Is there a way to achieve this with 2D techniques or is it 3D space? Any tips on where to start? :)
Thanks!
Attached the picture.

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: 2D or 3D?
« Reply #1 on: March 13, 2021, 12:41:20 pm »
As you can see on picture it can be 2D grid [x;y] with offsets for vertices (height). Use sf::VertexArray, map texture and use black color for shading (texture will have a burnt contrast in the base).

For the visual can be problem to highlight the terrain line, then you probably need to use shaders somehow.

frienzy

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: 2D or 3D?
« Reply #2 on: March 13, 2021, 04:20:55 pm »
That's great! Thank you, Paul, but I'm not sure how would I create this offset in the height axle...
The code for tilemap I have currently is:
// populate the vertex array, with one quad per tile
        for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                // get the current tile number
                int tileNumber = tiles[i + j * width];

                // find its position in the tileset texture
                int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                // get a pointer to the current tile's quad
                sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

                // define its 4 corners
                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                // define its 4 texture coordinates
                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: 2D or 3D?
« Reply #3 on: March 13, 2021, 07:08:37 pm »
I don't know what the rest of the code looks like, if there is any structure for each tile. Anyway while you are generating x and y positions during for loop, the heights for all tiles must be stored somewhere. For example in std::vector<int> m_heightmap[width * height - 1].

Then just add height to all y axis:
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y + m_heightmap[this_tile_index]);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y + m_heightmap[this_tile_index + 1]);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y + m_heightmap[this_tile_index + 1 + row_bellow]);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y + m_heightmap[index_of_tile_row_bellow]);

You must be carefull about index, it will be different in each line because quads have separated coordinates and you're counting with neighboring tiles which follow the changed height. It is probably not the best described, but the logic behind is straightforward.
« Last Edit: March 13, 2021, 07:10:25 pm by Paul »

frienzy

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: 2D or 3D?
« Reply #4 on: April 03, 2021, 08:00:45 pm »
Thank you, Paul!
You helped so much! I made std::vector<std::vector<int>> m_heightmap(width + 1, std::vector<int>(height + 1))
in the end, which allowed to address any individual quad on the map.
Now I need to figure out the way to edit this m_heightmap in the editor - similar to the example with a bigger hills :)