SFML community forums

Help => Graphics => Topic started by: koyuki on November 01, 2019, 09:04:16 pm

Title: get click on an isometric tile
Post by: koyuki 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?
Title: Re: get click on an isometric tile
Post by: Geheim 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.
Title: Re: get click on an isometric tile
Post by: koyuki 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
Title: Re: get click on an isometric tile
Post by: Stauricus 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.
Title: Re: get click on an isometric tile
Post by: koyuki 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?