【问题标题】:Objects rotating around another object keep accelerating围绕另一个物体旋转的物体不断加速
【发布时间】:2015-03-11 09:35:10
【问题描述】:

好的,所以我还是编程新手,我一直在尝试处理场景图,每当我旋转一个对象时,它的子对象就会一直围绕它加速,而我想要他们做的只是围绕父对象旋转它(父对象)旋转的速度相同。

这就是它们旋转的原因

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;
}

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

【问题讨论】:

    标签: c++ rotation sfml


    【解决方案1】:

    问题是你先把父级旋转进去

    Entity->theSprite.rotate(-10);
    

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

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

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

    1. 迭代:ParentAngle = -10 -> 孩子的旋转加上 -10 -> -10
    2. 迭代:ParentAngle = -20 -> 孩子的旋转加上 -20 -> -30
    3. 迭代:ParentAngle = -30 -> 孩子的旋转加上 -30 -> -60

    您应该将旋转值直接传递给函数rotateAround,而不是使用父精灵的当前角度作为参考

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多