I recently created a map editor, and i came across a problem: I can't position some tiles on some grid blocks, but if i go farther from Y-axis near 0, i can place blocks correctly.

This is the video: Test_001

Map.cpp - createBlock func

void Map::createBlock(sf::Vector2f position, std::string name) {
        double gridPositionX = floor(position.x / grid) * grid;
        double gridPositionY = floor(position.y / grid) * grid;

        double ceiledGridPositionX = ceil(gridPositionX);
        double ceiledGridPositionY = ceil(gridPositionY);

        int mapPositionArrayX = ceiledGridPositionX / grid;
        int mapPositionArrayY = ceiledGridPositionY / grid;

        sf::Vector2f gridPosition( ceiledGridPositionX, ceiledGridPositionY );

        if (!isPositionOccupiedByBlock(mapPositionArrayX, mapPositionArrayY)) {
            Block block = blockTypes[ findBlockNameVector(name) ];

            wMap[ mapPositionArrayX ][ mapPositionArrayY ] = block.getID();

bool Map::isPositionOccupiedByBlock(int mapX, int mapY) {
    return (wMap[mapX][mapY] != 0) ? true : false;

Nothing jumps out at me as wrong. However, your video shows you having an issue on certain vertical columns, then going left and continuing to make more blocks without issue.

if i go farther from Y-axis near 0

This indicates to me that you are moving into negative x coordinates. If wMap is an int[][] or something similar then I'd be very curious how you're handling negative coordinates, since the code above has no safeguards or wraps. If you have special code to handle this elsewhere, I would check there first.

If I am misinterpreting you and the video (I have no sound at the moment, so perhaps you said something in there that I missed), then the only other option is that there is a specific arbitrary set of columns in your grid that can't be set. You should export the coordinates where you're having the issue, then perhaps we can narrow down the issue.

Other things to consider:

  1. This could be a drawing issue. The blocks could be getting placed correctly, but not drawn correctly. Debug output of wMap would confirm or dismiss this. Based only on the code you provided this seems like the most likely option.
  2. This could be an input issue. If you're using the standard SFML mouse stuff then this seems unlikely, but if you're doing something special then it's possible.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.