SFML 的 'intersects' 函数总是 returns 'true'

SFML's 'intersects' function always returns 'true'

我想知道两个矩形是否相互重叠 SFML。这是我的代码:

if (ball.getLocalBounds().intersects(paddle.getLocalBounds()))
{
    //perform action

}

假定两个矩形相互重叠,代码将执行操作。但不知何故,它总是 returns true 在每种情况下,即使在两个矩形甚至不相交的情况下,例如:

从图片中可以看出,左边的球拍和小球并没有靠近,但是控制台还是提示重叠。这里发生了什么,你如何解决这个问题?

编辑:我试着用刚好足够的代码来制作一个测试项目来重现这个问题。从两个矩形的位置来看,我们可以看出它们绝对没有相交。但是,它仍然说它们相互重叠。

#include <SFML\Graphics.hpp>
#include <iostream>

using namespace std;
using namespace sf;

int main()
{

RenderWindow window(VideoMode(800, 600), "Test", Style::Close);

RectangleShape r1 = RectangleShape(Vector2f(100, 100));
RectangleShape r2 = RectangleShape(Vector2f(100, 100));

r1.setPosition(0, 0);
r2.setPosition(700, 500);

while (window.isOpen())
{
    Event e;

    while (window.pollEvent(e))
    {
        if (e.type == Event::Closed)
        {
            window.close();
        }
    }

    cout << r1.getLocalBounds().intersects(r2.getLocalBounds()) << endl;

    window.clear();

    window.draw(r1);
    window.draw(r2);

    window.display();
}

return 0;

}

getLocalBounds() 的文档说

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.

这让您了解为什么两个矩形总是重叠。全局 (x: 300, y: 300, w: 500, h: 400) 矩形在它自己的坐标系中将是 (x: 0, y: 0, w: 500, h: 400)

您的两个边界都从 (x: 0, y: 0) 开始,因此它们总是相交。