防止将精灵移动到另一个精灵上

时间:2014-03-29 13:54:42

标签: c++ sprite sfml

我有一些存储在编队矢量中的相同类型的精灵(2d-box)。现在我想用鼠标移动它们,效果很好。但是代码应该阻止玩家将一个精灵移动到另一个已经存在的矢量精灵上。

我的解决方案非常丑陋且不起作用。每当精灵移动时,如果将精灵移动到另一个精灵上,我将使用spriteoverlap函数进行测试。问题:

每当精灵直接靠近它时它停止移动,这就是想要的。 但之后我不能再移动它,因为重叠功能会将bool设置为false。

while (App.pollEvent(Event))
{


    //Moving the playerbuttons on the formation screen
    for (size_t k = 0; k < formation.size(); k++)
    {
        bool drag_onto_otherplayer = false;
        if (isMouseOver(formation[k], App) == true)
        {
        //The next loop tests if the sprite being moved with the mouse overlaps with another sprite from the formation vector       
        for (size_t j = 0; j < formation.size(); j++)
        {
            if (spriteOverlap(formation[j], formation[k], App) == true && k!=j)
            {
                std::cout << drag_onto_otherplayer << std::endl;
                drag_onto_otherplayer = true;
                std::cout << drag_onto_otherplayer <<std::endl;                                                 
            }

            if (drag_onto_otherplayer == false)
            //(If the sprite  does not overlap getting the new sprite position from the mouseposition) 

            {
                Mouseposition =sf::Vector2f(sf::Mouse::getPosition(App));
                Mouseposition.x = Mouseposition.x - formation[k].getLocalBounds().width / 2;
                Mouseposition.y = Mouseposition.y - formation[k].getLocalBounds().height / 2;
                formation[k].setPosition(sf::Vector2f(Mouseposition));
                Formation_playernames.clear();
                Formation_playerinformation.clear();
                Formation_Playernames(Font, Formation_playernames, formation, playerlist);
                Formation_Playerinformation(Font, Formation_playerinformation, formation, playerlist);
                    }

所以问题是我的循环和bool测试我猜,但我不知道如何解决它。 有什么想法吗?

这是我的spriteoverlap函数:

bool spriteOverlap(sf::Sprite &sprite1, sf::Sprite &sprite2, sf::RenderWindow &App)
{

    float x_min1 = sprite1.getPosition().x;
    float x_max1 = sprite1.getPosition().x + sprite1.getLocalBounds().width;
    float y_min1 = sprite1.getPosition().y;
    float y_max1 = sprite1.getPosition().y + sprite1.getLocalBounds().height;
    float x_min2 = sprite2.getPosition().x;
    float x_max2 = sprite2.getPosition().x + sprite2.getLocalBounds().width;
    float y_min2 = sprite2.getPosition().y;
    float y_max2 = sprite2.getPosition().y + sprite2.getLocalBounds().height;

    if (x_min1 > x_max2 | x_max1 < x_min2 | y_min1 > y_max2 | y_max1 < y_max2)
        return false;
    else
        return true;
};

我的isMouseover功能:

bool isMouseOver(sf::Sprite &sprite, sf::RenderWindow &App)
{
    float pos_x = sprite.getPosition().x;
    float pos_y = sprite.getPosition().y;
    if (sf::Mouse::getPosition(App).x > pos_x && sf::Mouse::getPosition(App).x < pos_x+sprite.getLocalBounds().width &&
        sf::Mouse::getPosition(App).y  >pos_y && sf::Mouse::getPosition(App).y < pos_y + sprite.getLocalBounds().height)
    {
        return true;
    }
    else
        return false;

};

1 个答案:

答案 0 :(得分:2)

检查是否已在sfml中包含了一些冲突:

bool spriteOverlap(sf::Sprite& sprite1, sf::Sprite& sprite2) // possibly const, dunno
{
    return sprite1.getGlobalBounds().intersects(sprite2.getGlobalBounds());
}

通常尝试这样:只有在下一帧的位置有效时才移动。这可以防止物体卡住,因为您已将它们移动到无效位置,从而阻止了任何进一步的移动

编辑:

//untested
bool spritesWillOverlap(sf::Sprite& sprite1, sf::Sprite& sprite2, sf::Vector2f vel)
{
   top1 = sprite1.getGobalBounds().top + vel.y;
   left1 = sprite1.getGlobalBounds().left + vel.x;
   right1 = left1 + sprite1.getGlobalBounds().width;
   bottom1 = top1 + sprite1.getGlobalBounds().height;

   top2 = sprite2.getGobalBounds().top + vel.y;
   left2 = sprite2.getGlobalBounds().left + vel.x;
   right2 = left2 + sprite1.getGlobalBounds().width;
   bottom2 = top2 + sprite1.getGlobalBounds().height;

   sf::FloatRect rect1(top1, left1, right1 - left1, bottom1 - top1);
   sf::FloatRect rect2(top2, left2, right2 - left2, bottom2 - top2);

   return rect1.intersects(rect2);
}

vel:velocity - &gt;每帧都通过此2D矢量移动一个对象

如果不熟悉“帧”的概念,请阅读帧率/固定帧率和/或时间步长。这是一个开始的示例文章:Fix Your Timestep!