【发布时间】: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;
}
我一直在试图弄清楚为什么会发生这种情况,我的猜测是它只是不断增加每一帧,但我不知道如何让它停止:(
【问题讨论】: