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

Author Topic: get click on an isometric tile  (Read 2410 times)

0 Members and 1 Guest are viewing this topic.

koyuki

  • Guest
get click on an isometric tile
« on: November 01, 2019, 09:04:16 pm »
The problem is this.
I have to call a particoular method of a clicked tile
if (event.type == sf::Event::MouseButtonPressed)
                if(event.mouseButton.button == sf::Mouse::Left){
                    sf::Vector2f mouse = renderWindow.mapPixelToCoords(sf::Mouse::getPosition(renderWindow));
                    for (int i=0; i<tilesOnMap; i++){
                        sf::FloatRect bounds = tileMap.at(i).sprite.getGlobalBounds();
                        if (bounds.contains(mouse)){
                            std::cout << "clicked at x: "<< tileMap.at(i).axis << " and y : " << tileMap.at(i).ord<< std::endl;   //to test the position since i still have to think about the method to create an interaction menĂ¹ with the tile
                           
                        }

                    }
                }
 

but since i use isometric tile (partially transparent sprites to give the isometric lines) the rects are overlying so i always get an answer from 2 tiles.
Any solution?
« Last Edit: November 01, 2019, 10:31:13 pm by koyuki »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: get click on an isometric tile
« Reply #1 on: November 02, 2019, 11:25:45 am »
This article should give you some insight: http://clintbellanger.net/articles/isometric_math/

You don't need to loop over your tiles in this case, if you store them correctly.
Failing to succeed does not mean failing to progress!

koyuki

  • Guest
Re: get click on an isometric tile
« Reply #2 on: November 02, 2019, 11:45:13 am »
i really thanks you with your help, but this is not what i need :-\

i mean, the problem is not on counting the tile, the problem is that the mouse position is always inside two bonds since the rects of the sprite are overlying, but i need a solution to get an answer only from the tile beind the mouse cursor

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: get click on an isometric tile
« Reply #3 on: November 02, 2019, 08:43:38 pm »
actually the article is exactly what you need. you should calculate the tiles position with proper math. the way you are trying to do won't work, simply because a isometric tile has different bounds than the FloatRect.

OR you could do another way: create square tiles, calculate the mouse position, the selected tile, all based on square tiles. and at the end of everything, you just rotate the whole sf::View by 45 dregrees.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

koyuki

  • Guest
Re: get click on an isometric tile
« Reply #4 on: November 02, 2019, 09:16:56 pm »
i was thinking about the view rotation, it is as diabolic as cool  ;D

but i want to find a better solution, i am still learning and this project is for a university exam, so i just wanna do my best to gain the most knowledge i can.

by the way, is there a way to restrict the bound? l'ike just a smaller rect in the center on the tile?