簡體   English   中英

SFML的“相交”功能始終返回“ true”

[英]SFML's 'intersects' function always returns 'true'

我想知道兩個矩形是否使用SFML彼此重疊。 這是我的代碼:

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

}

給定兩個矩形相互重疊,代碼將執行操作。 但是以某種方式,即使在兩個矩形甚至彼此不相交的情況下,它總是在每種情況下都返回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()的文檔說

返回的矩形位於局部坐標中,這意味着它會忽略應用於實體的轉換(平移,旋轉,縮放等)。 換句話說,此函數返回實體在坐標系中的邊界。

這使您了解為什么兩個矩形總是重疊。 全局(x: 300, y: 300, w: 500, h: 400)矩形在其自己的坐標系中為(x: 0, y: 0, w: 500, h: 400)

您的兩個邊界都始於(x: 0, y: 0) ,因此它們始終相交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM