SFML community forums

Help => General => Topic started by: Austin J on February 13, 2014, 01:44:02 am

Title: Help applying line algorithm
Post by: Austin J on February 13, 2014, 01:44:02 am
  I really wish I could just solve this myself but working with non-Cartesian coordinate system plus a few other things can confuse the hell out of me :(

  I'm working on a map editor that I want to be able to handle quite a few tiles. I've already set it up so that it won't render anything that is outside the view of the window. I can also get the tile the mouse is on by some simple math. (Use the mouses position to know what the index should be). However holding the mouse and moving it more than medium speed will still leave many gaps. I've been wanting to use a line algorithm to solve this, however I'm confusing the hell out of myself I feel like.

  Current implementation I have going it seems like it sort of does what its supposed to, but horribly. If I move the mouse just somewhat fast it's not that big a deal but if I move the mouse very fast it does odd things like this.

(http://i60.tinypic.com/k145yo.png)

The most top right tile and the very bottom left tile, I went in a mostly straight diagonal line between the two, never releasing the mouse. So yeah what I'm doing now definitely doesn't work.

Here's the code I've got trying to deal with this. Note that I'm not worried about views yet, it's not on my to do list currently.

void Map::handleClick(sf::RenderWindow & WINDOW,std::string tile)
{
        sf::Vector2i mp = sf::Mouse::getPosition(WINDOW);
        sf::Vector2f mousePosition = static_cast<sf::Vector2f>(mp);
        if(lastTile != NULL) // When the mouse is released lastTile gets reset to NULL
        {
                int x= lastTile->getPosition().x;
                int y = lastTile->getPosition().y;
                int x2 = Map::getTileByCoords(mousePosition).getPosition().x;
                int y2 = Map::getTileByCoords(mousePosition).getPosition().y;
                int dx = x - x2; int dy = y - y2;
                for(int i = x/16; i != x2/16;)
                {
                        int cy = (i*(dy/dx))*16;
                        Map::assignTile(Map::getTileByCoords(i*16,y),tile);
                        if(i > x2/16)
                        {
                                i --;
                        }
                        else{ i++; }
                }
        }
        Map::assignTile(Map::getTileByCoords(mousePosition),tile);
        Map::lastTile = &Map::getTileByCoords(mousePosition);
}
 


Title: Re: Help applying line algorithm
Post by: Hapax on February 13, 2014, 02:03:58 am
You could try interpolating the mouse positions using something like this (http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm).
Title: Re: Help applying line algorithm
Post by: Austin J on February 13, 2014, 02:29:40 am
Nice, it even has an example using SFML type grid. Thanks, wish I had seen this before.
Title: Re: Help applying line algorithm
Post by: Hapax on February 13, 2014, 02:48:55 am
You're welcome.
Good luck with it. Looks too complicated for me  ;D
Title: Re: Help applying line algorithm
Post by: Mercy404 on February 15, 2014, 12:34:53 am
I would also recommend Bresenham's Line Algorithm, but I found this (http://deepnight.net/bresenham-magic-raycasting-line-of-sight-pathfinding/) resource to be a lot easier to wrap my head around the first time I implemented it.