将无父节点场景项置于另一个场景项下

时间:2014-04-18 11:56:15

标签: c++ qt rotation qgraphicsview qgraphicsscene

在我的游戏中,我想从火箭发射器发射火箭。玩家将火箭发射器作为儿童物品。火箭必须是无父的。我正试图定位火箭,使其背部与火箭发射器的后部对齐(玩家在屏幕截图中朝向北方)并在其中水平居中:

correct

相反,我得到的是:

incorrect

旋转也不正确(运行示例并移动鼠标光标以查看我的意思)。我的代码在哪里出错?

#include <QtWidgets>

QPointF moveBy(const QPointF &pos, qreal rotation, float distance)
{
    return pos - QTransform().rotate(rotation).map(QPointF(0, distance));
}

float directionTo(const QPointF &source, const QPointF &target) {
    QPointF toTarget(target.x() - source.x(), target.y() - source.y());
    float facingTarget = qRadiansToDegrees(atan2(toTarget.y(), toTarget.x())) + 90.0f;
    facingTarget = fmod(facingTarget, 360.0f);
    if(facingTarget < 0)
        facingTarget += 360.0f;
    return facingTarget;
}

class Controller : public QObject
{
public:
    Controller(QGraphicsScene *scene) :
        mScene(scene)
    {
        mPlayer = scene->addRect(0, 0, 25, 25, QPen(Qt::blue));
        mPlayer->setTransformOriginPoint(mPlayer->boundingRect().width() / 2, mPlayer->boundingRect().height() / 2);

        mRocketLauncher = scene->addRect(0, 0, 16, 40, QPen(Qt::green));
        mRocketLauncher->setParentItem(mPlayer);
        mRocketLauncher->setPos(mPlayer->boundingRect().width() * 0.9 - mRocketLauncher->boundingRect().width() / 2,
            -mRocketLauncher->boundingRect().height() * 0.3);

        mRocket = scene->addRect(0, 0, 16, 20, QPen(Qt::red));
        scene->installEventFilter(this);

        QGraphicsTextItem *playerText = scene->addText("Player");
        playerText->setPos(0, 100);
        playerText->setDefaultTextColor(Qt::blue);
        QGraphicsTextItem *rocketLauncherText = scene->addText("Rocket launcher");
        rocketLauncherText->setPos(0, 120);
        rocketLauncherText->setDefaultTextColor(Qt::green);
        QGraphicsTextItem *rocketText = scene->addText("Rocket");
        rocketText->setPos(0, 140);
        rocketText->setDefaultTextColor(Qt::red);
    }

    bool eventFilter(QObject *, QEvent *event) {
        if (event->type() == QEvent::GraphicsSceneMouseMove) {
            const QGraphicsSceneMouseEvent *mouseEvent = static_cast<QGraphicsSceneMouseEvent*>(event);
            mPlayer->setRotation(directionTo(mPlayer->sceneBoundingRect().center(), mouseEvent->scenePos()));

            qreal rocketX = mRocketLauncher->sceneBoundingRect().center().x() - mRocket->boundingRect().width() / 2;
            QPointF rocketPos(rocketX, 0);
            rocketPos = moveBy(rocketPos, mPlayer->rotation(), mRocketLauncher->boundingRect().height() - mRocket->boundingRect().height());
            mRocket->setPos(rocketPos);
            mRocket->setRotation(mPlayer->rotation());

            return true;
        }
        return false;
    }
private:
    QGraphicsScene *mScene;
    QGraphicsRectItem *mPlayer;
    QGraphicsRectItem *mRocketLauncher;
    QGraphicsRectItem *mRocket;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QGraphicsView view;
    view.setMouseTracking(true);
    QGraphicsScene *scene = new QGraphicsScene;
    view.setScene(scene);

    Controller controller(scene);

    view.resize(300, 300);
    view.show();

    return app.exec();
}

1 个答案:

答案 0 :(得分:1)

这个想法是:

  1. 设置两个项目的轮换;
  2. 在全局(场景)坐标中获取发射器和火箭左下角的位置;
  3. 转换火箭以使positinos相等。
  4. 代码:

    mPlayer->setRotation(directionTo(mPlayer->sceneBoundingRect().center(), 
                                     mouseEvent->scenePos()));
    mRocket->setRotation(mPlayer->rotation());
    QPointF launcherPos = mRocketLauncher->mapToScene(
      mRocketLauncher->boundingRect().bottomLeft());
    QPointF currentRocketPos = mRocket->mapToScene(
      mRocket->boundingRect().bottomLeft());
    mRocket->setPos(mRocket->pos() - currentRocketPos + launcherPos);