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

Author Topic: Collision Detection Not Working Right  (Read 1103 times)

0 Members and 1 Guest are viewing this topic.

ozwurld

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Collision Detection Not Working Right
« on: September 25, 2014, 07:22:41 pm »
Hello all,

i am at my wits end with this one, i am trying to generate a maze for a battle
city game remake.

I am reading in the following from a text file "map.txt"

Code: [Select]
####################
#******************#
#******************#
#******************#
#******************#
#******************#
#******************#
#******************#
#******************# //Length and height is 20, square size is 32 therefore 32*20 = screen dimensions(640)
#******************#
#******************#
#******************#
#******************#
#******************#
#******************#
#******************#
#******************#
#******************#
#******************#
####################


This is the code i am using to generate co-ordinates for my maze: A '#' represents a square block

Code: [Select]
void Maze::readObstacleCoordinates()
{
    int x_pos = 0; //Where to begin drawing
    int y_pos = 0;

    _obstacles.clear(); //init _obstacles vector

    std::ifstream input("map.txt");
    std::string line;

    while(input>>line)
    {
        for(unsigned int i = 0; i < line.size(); i++)
        {

            if(line.at(i) == '#')
            {
                Vector2F Position(x_pos, y_pos);
                Obstacle obstacle(Position);
                _obstacles.push_back(obstacle); //create a vector of obstacles - which make up a maze
            }

            x_pos += 32; //squares  separated by distance of 32, i.e each square is 32 in size
        }
        x_pos = 0;
        y_pos += 32; //Square dimensions is 32
    }
}

Interface of the obstacle class

Code: [Select]
class Obstacle
{
public:
    Obstacle(const Vector2F& obstaclePos);
    Vector2F getPosition() const;
    int getSize() const;
    Vertices getVertices() const;

private:
    Vertices _vertices;
    Vector2F _obstaclePos;
    const int _obstacleSize = 32;
};

Interface of the maze class

Code: [Select]
class Maze
{
public:
    Maze();
    vector<Obstacle> getObstacles() const;

private:
    vector<Obstacle> _obstacles;
    void readObstacleCoordinates();
};

Draw obstacles using sfml rectangular shape
Code: [Select]
    for (unsigned int i = 0; i < _maze.getObstacles().size(); i++)
    {
        sf::RectangleShape pixel;
        pixel.setPosition(_maze.getObstacles().at(i).getPosition().x, _maze.getObstacles().at(i).getPosition().y);
        pixel.setTexture(&_wallTexture);
        pixel.setSize(sf::Vector2f(32, 32));
        _gameWindow.draw(pixel);
    }
   
My problem now arises when i try to do collision detection for all this, SFML draws everything on screen and it looks perfect:

Note A,B,C,D are the vertices of a square, My collision detection ( Horizontal Collision detection)

Code: [Select]
bool CollisionDetection::hasCollided(Vertices box1,Vertices box2) const
{
    if(box1.A.x - box2.B.x == 0 && box1.C.x - box2.D.x == 0)
    {
        return true;
    }
    return false;
}

The problem now is that sfml says there is a collision with the path that im supposed to move on, i.e the black spaces
shown on the picture below
http:// = [url]http://i1307.photobucket.com/albums/s582/ozwurld/Maze_zps507a6b9a.png

DOES ANYONE HAVE AN IDEA WHY THIS MAY BE??

-Thanks, sorry for the long question i am just trying to make sure i communicate the problem correctly!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Collision Detection Not Working Right
« Reply #1 on: September 26, 2014, 04:31:33 pm »
Please preview your post before submitting it, the code is difficult to read (should use [code=cpp][/code] tags) and the URL is broken. For others, the corrected link is:
http://s1307.photobucket.com/user/ozwurld/media/Maze_zps507a6b9a.png.html

And for such problems, you should always try to come up with a minimal complete example, so we can test it directly and see problems much faster. Chances are much higher that people will help you if your help request is well-structured and quick to answer, so take some time to read the whole thread ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: