围绕另一个物体旋转的物体继续加速

时间:2015-03-11 09:35:10

标签: c++ rotation sfml

好吧所以我还是编程的新手,我一直在努力研究一个场景图,每当我旋转一个物体时,当我想要他们做的就是围绕父对象旋转时,它的孩子们一直在加速它(父对象)旋转的速度相同。

这就是他们旋转的原因

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) == true)
{
    if (Entity != nullptr)
    {
    Entity->theSprite.rotate(-10);
    if (Entity->isParent == true)
        {
        for (int i = 0; i < Entity->listofChildren.size(); i++)
            {
            Entity->listofChildren.at(i)->theSprite.rotate(-10);
            Entity->listofChildren.at(i)->theSprite.setPosition(rotateAround(Entity, Entity->listofChildren.at(i)));
            }
        }
    }
}

这是计算旋转的原因

sf::Vector2f winMan::rotateAround(_Entity* toRotateAround, _Entity* toRotate)
{
    sf::Vector2f newPos;
    sf::Vector2f toRotateAroundpos = toRotateAround->theSprite.getPosition();
    sf::Vector2f toRotatepos = toRotate->theSprite.getPosition();

    float angle = toRotateAround->theSprite.getRotation() * (_PI / 180);

    newPos.x = cos(angle) * (toRotatepos.x - toRotateAroundpos.x) - sin(angle) * (toRotatepos.y - toRotateAroundpos.y) + toRotateAroundpos.x;
    newPos.y = sin(angle) * (toRotatepos.x - toRotateAroundpos.x) + cos(angle) * (toRotatepos.y - toRotateAroundpos.y) + toRotateAroundpos.y;

    return newPos;
}

我一直在试图弄清楚为什么会发生这种情况,而我的猜测是它只是不断加起来每一帧但我不知道如何让它停止:(

1 个答案:

答案 0 :(得分:0)

问题是您首先在

中旋转父级
Entity->theSprite.rotate(-10);

然后使用此父级的旋转来确定这里的孩子的旋转角度

float angle = toRotateAround->theSprite.getRotation() * (_PI / 180);

让我们看看程序计算的内容:

  1. 迭代:ParentAngle = -10 - &gt;孩子的旋转加-10 - &gt; -10
  2. 迭代:ParentAngle = -20 - &gt;孩子的旋转加-20 - &gt; -30
  3. iteration:ParentAngle = -30 - &gt;孩子的轮换加-30 - &gt; -60
  4. 您应该将旋转值直接传递给函数rotateAround

    ,而不是使用父精灵的当前角度作为参考。
相关问题