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

Author Topic: Editing Tiles in a Tilemap  (Read 3079 times)

0 Members and 1 Guest are viewing this topic.

Zantax

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Editing Tiles in a Tilemap
« on: April 04, 2017, 06:47:45 pm »
I'm trying to learn SFML on my own, but I'm a bit stumped right now and in need of some help. I've followed the official tile map tutorial (https://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php) and have it running with a basic tileset I made and a moveable player which for now is just a circle.

The problem I've run into is how to make the player and tilemap interact with eachother. I believe I know the basics on how to find the player's location on the tilemap, get their position and divide it by the tile width and height to find it's the closest x and y on the map (please correct me if I have that wrong).

What I'm trying to figure out now is what to do once I found the coordinates of the point the player is touching. The idea I have is to implement a fog of war style map. The tilemap will be completely black. When the player touches a corner in a quad, that corner will become transparent and use the built in gradient system to create a sort of fog that connects to the other corner.

Basically my question is how do I use the coordinates of a point in a vertex array to interact with the point and change its color.

Sorry if I tend to ramble a bit. I hope the question isn't too broad. I would post what code I have so far but I'm not at home.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Editing Tiles in a Tilemap
« Reply #1 on: April 05, 2017, 02:02:29 am »
What you are looking for seems to be contained within the sf::Rect class. It defines an axis-aligned rectangle and you will be using it when you get globalBounds or set a texture rectangle, for example, as FloatRect and IntRect are variations of this class.

The useful part here though, is its ability to test if a point is inside its rectangle or if it intersects/collides with another sf::Rect.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Zantax

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Editing Tiles in a Tilemap
« Reply #2 on: April 05, 2017, 02:36:29 am »
I have not seen those functions before, they will definitely help simplify things!

However I'm still having trouble figuring out how to actually edit an individual tile/point. Say I do something like this (rough code just for demonstration)

sf::Rect Bound=player.getglobalbounds;
if(Bound.intersects(25,50))
    Change Point at 25, 50

What would I have to do in order to actually edit that specific point?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Editing Tiles in a Tilemap
« Reply #3 on: April 05, 2017, 01:31:34 pm »
You don't check against hard coded values and instead use an sf::Vector, which you can change at any time.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kramitox

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Editing Tiles in a Tilemap
« Reply #4 on: April 05, 2017, 01:38:08 pm »
Okay so from what I am reading is that you are setting up a Map object and inside that there are Quads which you can manipulate.
My approach to this would probably be something along the lines of

for(int i = 0; i < map.quad.size() ; i++
{
  sf::FloatRect a = sf::FloatRect(map.quad.position.x , map.quad.position.y, map.quad.getSize().width, map.quad.getSize().heigth);
  if (player.getGlobalBounds().intersects(a)
  {
      map.quad.position = [insertPositionHere]
      map.quad.texCoords = [insertNewTexCoordsHere]
  }
}

that's one approach that I can think of off the top of my head. I for one wouldn't use Quads and would make my own "Tile" class.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Editing Tiles in a Tilemap
« Reply #5 on: April 05, 2017, 02:31:27 pm »
Remember though that "intersects" is for two overlapping rectangles and "contains" is for a point inside a rectangle. ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Zantax

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Editing Tiles in a Tilemap
« Reply #6 on: April 05, 2017, 04:51:17 pm »
My approach to this would probably be something along the lines of

for(int i = 0; i < map.quad.size() ; i++
{
  sf::FloatRect a = sf::FloatRect(map.quad.position.x , map.quad.position.y, map.quad.getSize().width, map.quad.getSize().heigth);
  if (player.getGlobalBounds().intersects(a)
  {
      map.quad.position = [insertPositionHere]
      map.quad.texCoords = [insertNewTexCoordsHere]
  }
}

That was my initial idea but I figured running a loop checking every single point every time you wanted to update would be very inefficient and could possibly cause lag if the player is moving around a lot.

that's one approach that I can think of off the top of my head. I for one wouldn't use Quads and would make my own "Tile" class.

I was so focused on the tutorial's coding I didn't even consider this idea. That might be exactly what I'm looking for, thank you! I knew I was just overthinking things. As soon as I get some spare time I'm going to try and write that up!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Editing Tiles in a Tilemap
« Reply #7 on: April 05, 2017, 11:14:46 pm »
You may also find Selba Ward's Tile Map useful ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything