(Boolean/SFML) 如何创建一个布尔值来检查碰撞?
(Boolean/SFML) How do I create a boolean that checks collision?
所以我最近开始学习 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
是一个 class 模板,而不是实际的 class。对于 Rect<int>
和 Rect<float>
,预定义的 typedef
分别称为 IntRect
和 FloatRect
。你想使用其中之一:
bool collision(sf::FloatRect r1, sf::FloatRect r2)
{
sf::FloatRect intersection;
return r1.intersects(r2, intersection);
}
更新:
这是您的代码的 "fixed" 版本。我添加了一个需要 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();
}
}
请注意,这里还有其他问题;例如您的游戏循环 运行 现在尽可能快,这可能不是您想要的。不过这段代码确实 运行。
所以我最近开始学习 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
是一个 class 模板,而不是实际的 class。对于 Rect<int>
和 Rect<float>
,预定义的 typedef
分别称为 IntRect
和 FloatRect
。你想使用其中之一:
bool collision(sf::FloatRect r1, sf::FloatRect r2)
{
sf::FloatRect intersection;
return r1.intersects(r2, intersection);
}
更新:
这是您的代码的 "fixed" 版本。我添加了一个需要 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();
}
}
请注意,这里还有其他问题;例如您的游戏循环 运行 现在尽可能快,这可能不是您想要的。不过这段代码确实 运行。