有没有办法旋转 sf::FloatRect

Is there a way to rotate an sf::FloatRect

我正在我的项目中制作碰撞检测系统,我需要检测旋转纹理上的碰撞。

为了检测碰撞,在主要角色纹理周围制作了 4 个 sf::FloatRect 对象(用于检测左右上下的碰撞),并使用 sf::FloatRect纹理的位置及其尺寸。如果主纹理周围的 4 个 sf::FloatRect 对象之一与另一个 sf::FloatRect 相交,则检测到碰撞。

当我要检测碰撞的纹理被旋转时,问题就来了。 sf::FloatRect不旋转。

有没有办法旋转它?或者在这种情况下有替代品吗?我查看了 SFML 的在线文档,但找不到任何旋转它的方法。

    RectangleShape collisionArea(Vector2f(pos[2], pos[3]));
    collisionArea.setPosition(pos[0], pos[1]);
    collisionArea.setRotation(rotation);

    FloatRect box = collisionArea.getGlobalBounds();


        FloatRect areaLeft(position.x - movementSpeed, position.y, movementSpeed, textureDimentions.y);
        FloatRect areaRight(position.x + textureDimentions.x, position.y, movementSpeed, textureDimentions.y);
        FloatRect areaUp(position.x, position.y - movementSpeed, textureDimentions.x, movementSpeed);
        FloatRect areaDown(position.x, position.y + textureDimentions.y, textureDimentions.x, movementSpeed);

        if(box.intersects(areaLeft))
        {
            collidingMap[0] = true;
        }

        if(box.intersects(areaRight))
        {
            collidingMap[1] = true;
        }


        if(box.intersects(areaUp))
        {
            collidingMap[2] = true;
        }


        if(box.intersects(areaDown))
        {
            collidingMap[3] = true;
        }
SFML中的

sf::FloatRect代表一个AABB(AABB代表Axis-Aligned Bounding Box)。所以不,它不能旋转。您需要的是 OBB(Oriented Bounding Box)碰撞检测。你将需要

  • 自己写代码(难)

  • 寻找适合您的图书馆!

我在 SFML 论坛上搜索后找到 this。可能有比这更好的解决方案,但这是我找到的第一个。