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

Author Topic: MapPixelToCoords and zoom out (incorrect position) [SOLVED]  (Read 2123 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
MapPixelToCoords and zoom out (incorrect position) [SOLVED]
« on: August 23, 2019, 02:55:38 am »
Hello,

So I have a way to click a startTile and a goalTile based off where you click and pressing LShift or LCtrl.

I used Num1 (view.zoom(1.1)) and Num2 (view.zoom(1.1)) to zoom in and out of the 250x250 (64x64) map.
        const auto mousePos = sf::Mouse::getPosition();
        const auto pixelToCoordPos = m_pGame->getWindow().mapPixelToCoords(mousePos,view);
        const sf::Vector2f tilePosFromMouse = {pixelToCoordPos.x / 64,pixelToCoordPos.y/64};
 

When I click somewhere, the tiles are off. But when I maximize the window, it all of sudden goes to the right tile. But when I zoom out it is off a few tiles on the Y-axis (undershoots it).

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) {
                        if (currentNode != nullptr) {
                                auto currTile = m_map->getTile(tilePosFromMouse.x, tilePosFromMouse.y);
                                currentNode = currTile;
                                m_map->setStartNode(currTile);
                        }
                }

Tile* Map::getTile(int columnIndex, int rowIndex)
{
        if (tileIsValid(columnIndex, rowIndex)) {
                return &m_grid[columnIndex][rowIndex];
        }
        else {
                return nullptr;
        }
}

Any idea how I can correct this? Hope I explained the situation correctly.

Thanks!

« Last Edit: August 23, 2019, 05:53:51 am by SFMLNewGuy »

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: MapPixelToCoords and zoom out (incorrect position) [SOLVED]
« Reply #1 on: August 23, 2019, 05:53:31 am »
I just realized I forgot to get the position relative to the window.

const auto mousePos = sf::Mouse::getPosition(m_pGame->getWindow());

Thanks anyway!