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

Author Topic: Texture intersect - TiledMap(editor)  (Read 1679 times)

0 Members and 1 Guest are viewing this topic.

Usak96

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Texture intersect - TiledMap(editor)
« on: April 10, 2017, 05:48:41 pm »
Hello,

I'm learning SFML and I don't know how to handle this problem.
I have VertexArray:

if (!m_Tileset.loadFromFile("./resources/tiles/tile.png")) { //Tileset got only one tile
      throw("Cant load tileset");
      return false;
   }

   int width, height;
   width = height = 150;
   m_Vertices.setPrimitiveType(sf::Quads);
   m_Vertices.resize(width * height * 4);
   sf::Vector2f tileSize(64, 64);
   for (unsigned int i = 0; i < width; ++i)
      for (unsigned int j = 0; j < height; ++j)
      {
         
         sf::Vertex* quad = &m_Vertices[(i + j * width) * 4];
   
         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);

         quad[0].texCoords = sf::Vector2f(tileSize.x, 0);
         quad[1].texCoords = sf::Vector2f(tileSize.x,0);
         quad[2].texCoords = sf::Vector2f(tileSize.x, tileSize.y);
         quad[3].texCoords = sf::Vector2f(0, tileSize.y);
      }



I would like to add game object (like trees/castles etc.) to my TiledMap having record, where they was added.
Like on these pictures..

Before adding I have TiledMap like this:


And after adding trees, I need to know that tree's texture is intersecting with rectangles at coords [3,2],[4,2],[5,2], [3,1], [3,2] etc..



I have no idea how to do that. I would be grateful for any advice.

Thank you!

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: Texture intersect - TiledMap(editor)
« Reply #1 on: April 16, 2017, 12:45:34 pm »
Hello,

you have very basic structure which only shows how draw with vertex arrays. But otherwise it's not very useful
I think. Read some articles about tilemaps.

You can have an array in which can be stored "ID" or pointer to your object (tree, castle).

ID:
0 - nothing
1 - tree 1
2 - tree 2
3 - castle

Some tips:
- map grid starts from [0;0] not [1;1]
- you draw whole map but you need draw only tiles which are on screen

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Texture intersect - TiledMap(editor)
« Reply #2 on: April 17, 2017, 02:11:18 am »
You can use Selba Ward's Tile Map, which includes (among other things) point-collision with a tile. ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*