首页 > 解决方案 > (布尔值/SFML)如何创建一个检查碰撞的布尔值?

问题描述

所以我最近进入了 SFML 编程,我需要帮助创建一个 bool 来检查两个矩形是否相互碰撞或重叠。

这是到目前为止的代码:

     bool collision(sf::Rect rect, sf::Rect rect2){
            return rect.getGlobalBounds().intersects(rect2.getGlobalBounds());
     }

我收到这些错误:

我正在制作一个平台游戏,但找不到一种有效的方法来测试玩家与世界之间的碰撞。

这是我想为这个简单程序编写的代码:

if(collision(rectangle1,rectangle2)){
std::cout << "collision" << std::endl;
}

任何帮助表示赞赏!:)

这是我的代码:

#include <SFML/Graphics.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Graphics/Rect.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800,600),"INSERT_WINDOW_TITLE", sf::Style::Titlebar | sf::Style::Close);
    sf::RectangleShape rect1(sf::Vector2f(20.f,20.f));
    rect1.setFillColor(sf::Color::Blue);
    sf::RectangleShape rect2(sf::Vector2f(20.f,20.f));
    rect2.setFillColor(sf::Color::Green);

    bool collision(sf::FloatRect r1, sf::FloatRect r2)
    {
        return r1.intersects(r2, sf::FloatRect());
    }

    while(window.isOpen()){
        sf::Event event;

        while(window.pollEvent(event)){
             if (event.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::KeyBoard::W) && collision(rect1,rect2) == false) rect1.move(0.f,-1.f);
        if(sf::Keyboard::isKeyPressed(sf::KeyBoard::S) && collision(rect1,rect2) == false) rect1.move(0.f,1.f);
        if(sf::Keyboard::isKeyPressed(sf::KeyBoard::A) && collision(rect1,rect2) == false) rect1.move(-1.f,0.f);
        if(sf::Keyboard::isKeyPressed(sf::KeyBoard::D) && collision(rect1,rect2) == false) rect1.move(1.f,0.f);

        window.draw(rect1);
        window.draw(rect2);
        window.display();
    }
}

标签: c++graphicsbooleansfml

解决方案


Rect是一个类模板,而不是一个实际的类。分别有预定义typedef的 sIntRectFloatRectforRect<int>Rect<float>。您想使用其中之一:

bool collision(sf::FloatRect r1, sf::FloatRect r2)
{
    sf::FloatRect intersection;
    return r1.intersects(r2, intersection);
}

更新:

这是您的代码的“固定”版本。我添加了一个重载,它接受Shapes 并转发到Rect碰撞检查功能。我还重新定位了您的rect2矩形;否则你不能移动,因为你已经发生了碰撞。

#include <SFML/Graphics.hpp>
#include <SFML/Window/Keyboard.hpp>

    bool collision(sf::FloatRect r1, sf::FloatRect r2)
    {
        sf::FloatRect intersection;
        return r1.intersects(r2, intersection);
    }

bool collision(sf::Shape const & r1, sf::Shape const & r2)
{
    return collision(r1.getGlobalBounds(), r2.getGlobalBounds());
}

int main(){
    sf::RenderWindow window(sf::VideoMode(800,600),"INSERT_WINDOW_TITLE", sf::Style::Titlebar | sf::Style::Close);
    sf::RectangleShape rect1(sf::Vector2f(20.f,20.f));
    rect1.setFillColor(sf::Color::Blue);
    sf::RectangleShape rect2(sf::Vector2f(20.f,20.f));
    rect2.setFillColor(sf::Color::Green);
    rect2.setPosition(100.f, 100.f);

    while(window.isOpen()){
        sf::Event event;

        while(window.pollEvent(event)){
             if (event.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) && collision(rect1,rect2) == false) rect1.move(0.f,-1.f);
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::S) && collision(rect1,rect2) == false) rect1.move(0.f,1.f);
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) && collision(rect1,rect2) == false) rect1.move(-1.f,0.f);
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::D) && collision(rect1,rect2) == false) rect1.move(1.f,0.f);

        window.clear();
        window.draw(rect1);
        window.draw(rect2);
        window.display();
    }
}

请注意,这里还有其他问题;例如,您的游戏循环现在尽可能快地运行,这可能不是您想要的。不过,这段代码确实可以运行。


推荐阅读