检查运动线是否被阻挡

时间:2016-12-31 16:41:43

标签: c++ collision-detection sfml

我编写了一个小型自上而下的射击游戏,但有时两侧的人工智能会被卡在一个将它们分开的墙上。我已经尝试过编写一个检查此功能的函数,但最终会有一些会返回true或false,即使你输入相同的东西!

我尝试检查它的方式是通过这个类

    class Level{
    public:
        sf::Sprite* background;
        sf::Texture* tex;
        sf::Image* im;
        std::vector<std::reference_wrapper<sf::Color>> colCollisions;

        int Width, Height;

        Level(std::string name, int width, int height,std::vector<std::reference_wrapper<sf::Color>>col){
            Width = width;
            Height = height;
            tex = new sf::Texture();
            im = new sf::Image();
            im->loadFromFile(name);
            tex->loadFromImage(*im);


            background = new sf::Sprite(*tex);
            background->setTextureRect(sf::IntRect(0, 0, width, height));

            colCollisions = col;
        }

        Level(std::string name, int width, int height){
            Width = width;
            Height = height;
            tex = new sf::Texture();
            im = new sf::Image();
            im->loadFromFile(name);
            tex->loadFromImage(*im);


            background = new sf::Sprite(*tex);
            background->setTextureRect(sf::IntRect(0, 0, width, height));
        }


        void modulate(sf::RenderWindow& window){
            window.draw(*background);
        }

        bool check(sf::Sprite* s){
            sf::Vector2f pos = s->getPosition();

            if (pos.x<0 || pos.x>Width || pos.y<0 || pos.y>Height){
                return true;
            }

            for (int i = 0; i < colCollisions.size(); i++){
                if (colourCollides(pos.x,pos.y, colCollisions[i], im)){
                    return true;
                }
            }

            return false;
        }

        bool check(int x, int y){

            if (x<0 || x>Width || y<0 || y>Height){
                return true;
            }

            for (int i = 0; i < colCollisions.size(); i++){
                if (colourCollides(x,y, colCollisions[i], im)){
                    return true;
                }
            }

            return false;
        }

    };

真的很感激任何建议或建议!

0 个答案:

没有答案