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

Author Topic: [SOLVED]Tilemap movement with a std::vector problem  (Read 1810 times)

0 Members and 1 Guest are viewing this topic.

Dubmosphere

  • Newbie
  • *
  • Posts: 13
    • View Profile
[SOLVED]Tilemap movement with a std::vector problem
« 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);
            }
        }
    }
 
« Last Edit: July 01, 2014, 02:36:33 am by Dubmosphere »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Tilemap movement with a std::vector problem
« Reply #1 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.

Dubmosphere

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Tilemap movement with a std::vector problem
« Reply #2 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
« Last Edit: June 30, 2014, 10:59:54 am by Dubmosphere »

 

anything