c++ - (布尔值/SFML)如何创建一个检查碰撞的布尔值?
问题描述
所以我最近进入了 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
的 sIntRect
和FloatRect
forRect<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();
}
}
请注意,这里还有其他问题;例如,您的游戏循环现在尽可能快地运行,这可能不是您想要的。不过,这段代码确实可以运行。
推荐阅读
- django - Django Admin 无法在具有多个外键的模型中显示属性到另一个模型
- .net - 具有众多身份角色的 .NET 5 400 错误
- visual-studio-code - pickProcess 命令的源代码 VS Code
- r - 删除数据框中所有列名的字符
- reactjs - 从 Material UI 下拉选择中显示模态
- javascript - 只有当我在 Spotify API 上发出第一次获取请求(单击搜索方法)时,我才会得到 401,之后就可以正常工作了。请帮我解决它
- python - Python .loc 返回一个 TypeError
- julia - 如何在 Colab 上打开 .ipynb 笔记本
- javascript - 使用useState在React中更新数组时如何保留以前的值
- coq - 如何在 Coq 中表达归纳关系的一个元素不能从另一个元素推导出来?