سؤال

I'm trying to rotate a QGraphicsItem by position of the mouse. So in my Dialog.cpp, I call this function whenever my mouse pos changes:

void Dialog::rotating()
{
    QPointF local = ui->graphicsView->mapToScene( ui->graphicsView->mouse );
        Player->rotate(local.x(),local.y());
}

with

ui->graphicsView->setScene(scene);

and

scene->addItem(Player);

in the constructor.

the rotate fucntion called does this:

void player::rotate(int x, int y)
{
    double disX = x - 1100;
    double disY = y - 1300;
    double angle = atan2(disY, disX) * 180 / M_PI;
    setTransformOriginPoint(boundingRect().center());
    setRotation(rotation() + angle - orig);

    orig = angle;
}

where (1100, 1300) is the position of where the player is. I'm pretty sure that this will do the trick, but it's not rotating correctly. Maybe the local position is incorrect?

Also, how do I move the player so that rotation does not affect it? Any input would be appreciated. Thanks in advance.

هل كانت مفيدة؟

المحلول

Took me some time but I finally figured it out!

the first line of function rotate(int,int) should be:

double disX = x - (1100 + boundingRect().center().x());
double disY = y - (1300 + boundingRect().center().y());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top