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

Author Topic: How do I detect a mouse click on an sf:Quad in an sf::VertexArray?  (Read 2535 times)

0 Members and 1 Guest are viewing this topic.

newtosfml9

  • Newbie
  • *
  • Posts: 3
    • View Profile
I have an sf::VertexArray of sf::Quads. As you can see by my code below, I loop through, then display them in a grid that takes up the entire window. Based on this code that determines where each tile goes into the VertexArray (0,4,8,12, etc)...
(x + y * numOfHorizTiles) * 4;
How can I determine which tile was clicked on with the x/y coordinates of a mouse click? Getting the mouse coordinates isn't a problem. The trouble is when I try to determine which tile it translates to. "x" in this sense means the number of tiles across, but the mouse click will be in pixels not tiles. So an x axis coordinate of 15 pixels could be on tile two.

for(int x = 0; x < numOfHorizTiles; ++x)
{
    for(int y = 0; y < numOfVertTiles; ++y)
    {
        sf::Vertex* quad = &tileVertices[(x + y * numOfHorizTiles) * 4];

        quad[0].position = sf::Vector2f(x * TILE_SIZE, y * TILE_SIZE);
        // etc...
    }
}

Thanks for any help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: How do I detect a mouse click on an sf:Quad in an sf::VertexArray?
« Reply #1 on: May 22, 2017, 06:59:32 pm »
You can use the modulo operator:"How many times does the tile size have space in the given mouse coordinates?"

x = mouse_coords.x % tile_size.width;
y = mouse_coords.y % tile_size.height;
 

If the tile grid isn't rendered at 0,0 you'll need to subtract the grid's "origin" from the mouse position first.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

newtosfml9

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How do I detect a mouse click on an sf:Quad in an sf::VertexArray?
« Reply #2 on: May 22, 2017, 07:34:50 pm »
You can use the modulo operator:"How many times does the tile size have space in the given mouse coordinates?"

x = mouse_coords.x % tile_size.width;
y = mouse_coords.y % tile_size.height;
 

Thanks for your help, but I'm not sure how that would work.

Let's say my mouse coordinates are x(15) and y(1). Each tile is 10x10 pixels in a 100x100 pixel window. Those mouse coordinates would equal the second grid square from the left. Which is:
&tileVertices[4]

If we run that through your code it's incorrect, unless I'm misunderstanding how to use it.


// mouse_coord.x is equal to 15
// mouse_coord.y is equal to 1
// tile_size width and height is 10
x = mouse_coords.x % tile_size.width;
y = mouse_coords.y % tile_size.height;

// This calculates to (5 + 1 * 10) * 4 = 60
// This should calculate to 4
int positionInVectorArray = (x + y * numOfHorizTiles) * 4;
&tileVertices[positionInVectorArray];
 

newtosfml9

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How do I detect a mouse click on an sf:Quad in an sf::VertexArray?
« Reply #3 on: May 22, 2017, 07:43:42 pm »
I was able to work it out. Rather than use the modulo operator I used division to drop the remainder. Thanks for your help!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: How do I detect a mouse click on an sf:Quad in an sf::VertexArray?
« Reply #4 on: May 22, 2017, 07:45:53 pm »
Right, I had it wrong in my head. You want to actually do an integer division or just a regular division and then just drop the decimal result.

So your mouse position x is 15 and the tile width 10, 15/10 = 1.5
If for an integer division you'd just get 1 otherwise you can just use std::floor() to make sure to always round down or convert to integer (which you'll need anyways as the container index is an (unsigned) integer).

Modulo has its use case, but not directly in that regard.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything