Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411283 Posts in 69325 Topics- by 58380 Members - Latest Member: bob1029

March 29, 2024, 07:26:53 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Isometric tile picking - X axis not working, Y kinda
Pages: [1]
Print
Author Topic: Isometric tile picking - X axis not working, Y kinda  (Read 913 times)
makerimages
Level 2
**


Makerimages Studios


View Profile WWW
« on: January 30, 2017, 12:28:56 PM »

Hi all!

Starting to gamedev again and Trying an isometric game. However, all I try, I can't seem to figure out tile picking properly.. Using SFML
 
Here's how I'm detecting the clicks and moving around a sf::View. Sprites are 64*32

Code:
#include "GameWorld.h"
#include <math.h>
GameWorld::GameWorld()
{
    this->camX = 0;
    this->camY = 0;
    this->factories.push_back(new Factory("Factory 1"));
}

GameWorld::~GameWorld()
{
    //dtor
}


void GameWorld::draw(sf::RenderWindow * win, ContentManager* contentManager) {
    this->factories.at(this->activeFactory)->render(win,contentManager, this->camX, this->camY);
}

void GameWorld::update(float delta, sf::RenderWindow* win) {
    float speed= 128;
        sf::View view = win->getView();

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        this->camY -= speed*delta;

    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        this->camX -= speed*delta;

    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        this->camY += speed*delta;

    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        this->camX += speed*delta;
    }
    view.setCenter(this->camX,this->camY);

    win->setView(view);

    if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {

    sf::Vector2i selectedTile;
int x = sf::Mouse::getPosition(*win).x - this->camX;
int y = sf::Mouse::getPosition(*win).y - this->camY;
selectedTile.x = (y + x/2)/32;
selectedTile.y = (y - x/2)/32;
 
        printf("%d %d %d %d\n",sf::Mouse::getPosition(*win).x,sf::Mouse::getPosition(*win).y, selectedTile.x,selectedTile.y);
    }


}

The map is a 2d vector which gets drawn like so

Code:
   float screenX = (indexX - indexY) * this->sprite->getGlobalBounds().width/2;
    float screenY = (indexX + indexY) * this->sprite->getGlobalBounds().height/2;

Heres what this currently prints out: When the game is loaded initially - clicking the first tile rendered (0,0) outputs 21 0 as the computed coords. As you can see, the X value is completely off, and the Y value goes off aswell as soon as I move the view in any direction. How could I get this picker to work properly?
Logged

Makerimages-Its in the pixel
Polly
Level 6
*



View Profile
« Reply #1 on: January 31, 2017, 11:39:22 AM »

Blast from the past Smiley
Logged
makerimages
Level 2
**


Makerimages Studios


View Profile WWW
« Reply #2 on: January 31, 2017, 11:56:53 AM »

Blast from the past Smiley

And for tile 0;0 .. that produces.. 30 -11 as the coords... Nope, not working...
Logged

Makerimages-Its in the pixel
makerimages
Level 2
**


Makerimages Studios


View Profile WWW
« Reply #3 on: January 31, 2017, 12:22:08 PM »

Problem solved.

Working code thanks to https://www.twitch.tv/siegegames

Code:
sf::Vector2f view_mouse_position = win->mapPixelToCoords(sf::Mouse::getPosition(*win));
    float x = view_mouse_position.x / 64;
    float y = view_mouse_position.y / 32;

    int tile_x = x + y;
    int tile_y = x - y;
Logged

Makerimages-Its in the pixel
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic