如何在SFML中检查与矩形的碰撞

问题描述

我正在尝试检查2D数组中存储的形状是否存在碰撞。

我正在尝试在创建的Pacman克隆中使用逻辑。我检测两个Sprite之间的碰撞没有问题,但是我似乎无法用它来检测Pacman Sprite与矩形形状之间的碰撞。如何使精灵与数组中的任何矩形发生碰撞,以及如何检查与之碰撞的图块的值。谢谢!

const int rows = 28; //width
const int columns = 36; //height

int gridArray[rows][columns];

gridArray[6][3] = 1; //I want to check if the rectangle I collide with is = to 1,then do logic.

sf::RectangleShape grid1;
grid1.setFillColor(sf::Color::Transparent);
grid1.setoutlineColor(sf::Color::White);
grid1.setoutlineThickness(.5f);
grid1.setSize(sf::Vector2f(16,16)); //each square is 16 x 16
int positionx = -16;
int positiony = 0;

for (int i = 0; i < 28; i++)
{
    positionx = positionx + 16;
    positiony = 0;
    for (int j = 0; j < 36; j++)
    {
        grid1.setPosition(positionx,positiony);
        window.draw(grid1);
        positiony = positiony + 16;
    }
}
window.draw(pacman->pacMansprite);
if (pacman->pacMansprite.getGlobalBounds().intersects(grid1.getGlobalBounds())) //me attempting to detect collision of ANY rectangle.

{

    pacman->canMove = false;
    std::cout  << "UGHGHGHGHG" << std::endl;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...