如何在旋转框上渲染边界框

时间:2017-10-17 19:54:22

标签: c++ debugging sfml

您好,我是C ++ SFML的新手。我想问一下如何在旋转框上渲染一个边界框。我这样做的方法是制作另一个透明但具有轮廓的矩形并将其转换为旋转矩形的getGlobalBounds,但它不能正确渲染,因为我一直在使用setSize来使边界框增长,这意味着盒子的上部和左部没有调整大小。我觉得我在这里遗漏了一些东西所以这是我的代码:

You can do:

    try {
        a.should.equal(b)
    } catch(e) {
        console.log(e)
        done(e)
    } 
    done()

1 个答案:

答案 0 :(得分:2)

我终于开始工作了。这是我做的:

sf::RectangleShape Rect[RECT];
sf::RectangleShape AABB[RECT];
Rect[0].setSize(sf::Vector2f(50.0f, 50.0f));
Rect[1].setSize(sf::Vector2f(50.0f, 100.0f));
Rect[2].setSize(sf::Vector2f(60.0f, 80.0f));
Rect[3].setSize(sf::Vector2f(100.0f, 60.0f));
Rect[4].setSize(sf::Vector2f(30.0f, 250.0f));

for (int x = 0; x < RECT; x++)
{
    //Starting Position
    Rect[x].setOrigin(Rect[x].getSize().x / 2, Rect[x].getSize().y / 2);
    AABB[x].setOrigin(AABB[x].getSize().x / 2, AABB[x].getSize().y / 2);
    xpos += 150;
    Rect[x].setPosition(xpos, ypos);
    AABB[x].setSize(sf::Vector2f(Rect[x].getGlobalBounds().width, Rect[x].getGlobalBounds().height));
    AABB[x].setPosition(Rect[x].getPosition().x, Rect[x].getPosition().y);
    colcount++;
    if (colcount == 3)
    {
        xpos = 0;
        ypos += 200;
        colcount = 0;
    }

    Rect[x].setFillColor(sf::Color::Red);
    AABB[x].setFillColor(sf::Color::Transparent);
    AABB[x].setOutlineThickness(1);
    AABB[x].setOutlineColor(sf::Color::White);
}

while (window.isOpen())
{

    window.clear(sf::Color::Black);
    //Drawing Shapes
    for (int x = 0; x < RECT; x++)
    {
        window.draw(Rect[x]);
        window.draw(AABB[x]);
    }

    //Rotation
    Rect[0].rotate(1);
    Rect[1].rotate(45);
    Rect[2].rotate(11.25);
    Rect[3].rotate(5.625);
    Rect[4].rotate(22.5);

    for (int i = 0; i < RECT; i++)
    {
        MinPoint[i].x = AABB[i].getPosition().x - (AABB[i].getSize().x / 2);
        MaxPoint[i].x = AABB[i].getPosition().x + (AABB[i].getSize().x / 2);
        MinPoint[i].y = AABB[i].getPosition().y - (AABB[i].getSize().y / 2);
        MaxPoint[i].y = AABB[i].getPosition().y + (AABB[i].getSize().y / 2);

        AABB[i].setOrigin(AABB[i].getSize().x / 2, AABB[i].getSize().y / 2);
        AABB[i].setSize(sf::Vector2f(Rect[i].getGlobalBounds().width, Rect[i].getGlobalBounds().height));
        AABB[i].setPosition(Rect[i].getPosition().x, Rect[i].getPosition().y);
    }