gpt4 book ai didi

c++ - ( boolean/SFML)如何创建一个检查碰撞的 boolean 值?

转载 作者:行者123 更新时间:2023-11-30 03:19:41 27 4
gpt4 key购买 nike

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

这是目前为止的代码:

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

我收到这些错误:

  • 在“rect”之前缺少模板参数
  • 在“rect2”之前缺少模板参数
  • ')' 标记前的预期主表达式

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

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

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();
}
}

最佳答案

Rect是一个类模板,而不是一个实际的类。有预定义的 typedef我们叫 IntRectFloatRect对于 Rect<int>Rect<float>分别。你想使用其中之一:

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

更新:

这是您代码的“固定”版本。我添加了一个需要 Shape 的重载s 并转发到 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();
}
}

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

关于c++ - ( boolean/SFML)如何创建一个检查碰撞的 boolean 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53471814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com