SFML community forums

Help => General => Topic started by: Dubmosphere on June 30, 2014, 01:00:33 am

Title: [SOLVED]Tilemap movement with a std::vector problem
Post by: Dubmosphere on June 30, 2014, 01:00:33 am
Hello, I'm trying to implement a tilemap engine for making a tetris or jump n run... the collision detection works but i have no clue why the programm crashes if i touch the border of the window... everytime I get out of the window the following message appears:

terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check
Aborted

here is my movement:

    if(isMovingUp) {
        unsigned int x = hero.getPosition().x / 32;
        unsigned int y = hero.getPosition().y / 32 - 1;

        if(y >= 0) {
            if(map.getTile(x, y).collision == false) {
                hero.setPosition(x * 32, y * 32);
            }
        }
    }

    if(isMovingDown) {
        unsigned int x = hero.getPosition().x / 32;
        unsigned int y = hero.getPosition().y / 32 + 1;

        if(y < map.getSize().y) {
            if(map.getTile(x, y).collision == false) {
                hero.setPosition(x * 32, y * 32);
            }
        }
    }

    if(isMovingLeft) {
        unsigned int x = hero.getPosition().x / 32 - 1;
        unsigned int y = hero.getPosition().y / 32;

        if(y >= 0) {
            if(map.getTile(x, y).collision == false) {
                hero.setPosition(x * 32, y * 32);
            }
        }
    }

    if(isMovingRight) {
        unsigned int x = hero.getPosition().x / 32 + 1;
        unsigned int y = hero.getPosition().y / 32;

        if(x < map.getSize().x) {
            if(map.getTile(x, y).collision == false) {
                hero.setPosition(x * 32, y * 32);
            }
        }
    }
 
Title: Re: Tilemap movement with a std::vector problem
Post by: Ixrec on June 30, 2014, 01:25:03 am
Quote
everytime I get out of the window
Quote
an instance of 'std::out_of_range'

Something tells me your x and/or y values are ending up outside the range of your tilemap.
Title: Re: Tilemap movement with a std::vector problem
Post by: Dubmosphere on June 30, 2014, 10:29:47 am
Quote
Something tells me your x and/or y values are ending up outside the range of your tilemap.

Yes I know, but I don't know why the player is moving if he has reached the right end of the map... he should only move if his position (/32 because of his movements are the same size as the tiles) is in the map...

Edit:
I simply made the tilemap one tile wider and higher and set it to collide... that way it works, but doesn't look pretty well coded... Thanks anyway