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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - newtosfml9

Pages: [1]
1
I was able to work it out. Rather than use the modulo operator I used division to drop the remainder. Thanks for your help!

2
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];
 

3
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.

Pages: [1]