Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Setting offset between two rotating QGraphicsRectItems

Setting offset between two rotating QGraphicsRectItems

Scheduled Pinned Locked Moved Solved Game Development
c++graphicsqgraphicsitemqt6
5 Posts 3 Posters 799 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    black_gay
    wrote on 25 Apr 2022, 18:03 last edited by
    #1

    I have a red rectangle (QGraphicsRectItem), which position and rotation depends on pressed keys (up, down, left, right, turn right, turn left). I want another - green rectangle to follow red's position and rotation, but with a certain offset, in this manner:
    rotation

    I'd like to know how to achieve it without making the red parent of the green. My code (withotu the desired offset) is:

    Game::Game()
       {
           player->setRect(0,0,100,100);
           player->setBrush(Qt::red);
           player->setPos(400,300);
           this->addItem(player);
           player->setFocus();
        
           rectItem->setRect(0,0,100,100);
           rectItem->setBrush(Qt::green);
           rectItem->setPos(400,330);
           this->addItem(rectItem);
        
           player->setZValue(10);
           rectItem->setZValue(1);
        
           player->setTransformOriginPoint(player->boundingRect().width()/2 ,
                                           player->boundingRect().height()/2 );
           rectItem->setTransformOriginPoint(player->transformOriginPoint());
       }
        
        
       void Game::advance()
       {
           rectItem->setPos(player->pos().x(), player->pos().y());
           rectItem->setRotation(player->rotation());
       }
    
    1 Reply Last reply
    0
    • J JonB
      27 Apr 2022, 06:59

      @black_gay said in Setting offset between two rotating QGraphicsRectItems:

      If i add them both to a QGraphicsItemGroup, then id need to apply movement and rotation directly to the whole QGraphicsItemGroup

      I don't follow. That is exactly right, and isn't it precisely what you want? Set up the red & green rectangles initially in their right positions, like the first picture, and from then on when you rotate the group they will rotate together like your other pictures show?

      Otherwise if for some reason the movements/rotations must be applied to the red rectangle, and you do not want to use either parenting or grouping, then you will have to apply the necessary transformation to the green rectangle whenever the red one is moved?

      B Offline
      B Offline
      black_gay
      wrote on 27 Apr 2022, 10:28 last edited by black_gay
      #5

      @JonB You’re right, Thanks, QGraphicsItemGroup did the trick. I just needed to set the transform origin point to the centre of the red rectangle. Now the desired effect is achieved by this code:

      #include "game.h"
      #include <QGraphicsItemGroup>
       
      QGraphicsItemGroup* g = new QGraphicsItemGroup;
       
      Game::Game()
      {
          player  = new QGraphicsRectItem();
          rectItem = new QGraphicsRectItem();
          m_view = new QGraphicsView(this);
          m_view->setSceneRect(QRectF(0,0,800,600));
       
       
          this->setBackgroundBrush(Qt::black);
          m_view->setScene(this);
          m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          m_view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
          m_view->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
          m_view->show();
       
          player->setRect(0,0,100,100);
          player->setBrush(Qt::red);
          player->setPos(400,300);
          player->setFocus();
       
          rectItem->setRect(0,0,100,100);
          rectItem->setBrush(Qt::green);
          rectItem->setPos(400,330);
       
          this->addItem(g);
       
          player->setZValue(10);
          rectItem->setZValue(1);
       
          g->addToGroup(player);
          g->addToGroup(rectItem);
       
          g->setTransformOriginPoint(player->pos().x() + player->boundingRect().width()/2
                                    ,player->pos().y() + player->boundingRect().height()/2);
      }
       
       
      void Game::advance()
      {
       
      }
       
       
      void Game::keyPressEvent(QKeyEvent *event)
      {
          switch(event->key())  {
          case Qt::Key_Left :
              g->moveBy(-5,0);
              break;
          case Qt::Key_Right :
              g->moveBy(5,0);
              break;
          case Qt::Key_Up :
              g->moveBy(0,-5);
              break;
          case Qt::Key_Down :
              g->moveBy(0,5);
              break;
          case Qt::Key_Q :
              g->setRotation(g->rotation()- 3);
              break;
          case Qt::Key_W :
              g->setRotation(g->rotation() +3);
              break;
          }
          update();
      }
      
      1 Reply Last reply
      1
      • J Offline
        J Offline
        JoeCFD
        wrote on 26 Apr 2022, 17:08 last edited by
        #2

        https://doc.qt.io/qt-5/qgraphicsitemgroup.html

        B 1 Reply Last reply 26 Apr 2022, 21:43
        1
        • J JoeCFD
          26 Apr 2022, 17:08

          https://doc.qt.io/qt-5/qgraphicsitemgroup.html

          B Offline
          B Offline
          black_gay
          wrote on 26 Apr 2022, 21:43 last edited by
          #3

          @JoeCFD „A QGraphicsItemGroup is a special type of compound item that treats itself and all its children as one item (i.e., all events and geometries for all children are merged together).” But i want the green item to follow the red, meaning movement is applied to the red only (player->moveBy(0,5); so on). If i add them both to a QGraphicsItemGroup, then id need to apply movement and rotation directly to the whole QGraphicsItemGroup. I could add the green to a QGraphicsItemGroup and give the Group the red as the parent, but i want to avoid parenting.

          J 1 Reply Last reply 27 Apr 2022, 06:59
          0
          • B black_gay
            26 Apr 2022, 21:43

            @JoeCFD „A QGraphicsItemGroup is a special type of compound item that treats itself and all its children as one item (i.e., all events and geometries for all children are merged together).” But i want the green item to follow the red, meaning movement is applied to the red only (player->moveBy(0,5); so on). If i add them both to a QGraphicsItemGroup, then id need to apply movement and rotation directly to the whole QGraphicsItemGroup. I could add the green to a QGraphicsItemGroup and give the Group the red as the parent, but i want to avoid parenting.

            J Offline
            J Offline
            JonB
            wrote on 27 Apr 2022, 06:59 last edited by JonB
            #4

            @black_gay said in Setting offset between two rotating QGraphicsRectItems:

            If i add them both to a QGraphicsItemGroup, then id need to apply movement and rotation directly to the whole QGraphicsItemGroup

            I don't follow. That is exactly right, and isn't it precisely what you want? Set up the red & green rectangles initially in their right positions, like the first picture, and from then on when you rotate the group they will rotate together like your other pictures show?

            Otherwise if for some reason the movements/rotations must be applied to the red rectangle, and you do not want to use either parenting or grouping, then you will have to apply the necessary transformation to the green rectangle whenever the red one is moved?

            B 1 Reply Last reply 27 Apr 2022, 10:28
            2
            • J JonB
              27 Apr 2022, 06:59

              @black_gay said in Setting offset between two rotating QGraphicsRectItems:

              If i add them both to a QGraphicsItemGroup, then id need to apply movement and rotation directly to the whole QGraphicsItemGroup

              I don't follow. That is exactly right, and isn't it precisely what you want? Set up the red & green rectangles initially in their right positions, like the first picture, and from then on when you rotate the group they will rotate together like your other pictures show?

              Otherwise if for some reason the movements/rotations must be applied to the red rectangle, and you do not want to use either parenting or grouping, then you will have to apply the necessary transformation to the green rectangle whenever the red one is moved?

              B Offline
              B Offline
              black_gay
              wrote on 27 Apr 2022, 10:28 last edited by black_gay
              #5

              @JonB You’re right, Thanks, QGraphicsItemGroup did the trick. I just needed to set the transform origin point to the centre of the red rectangle. Now the desired effect is achieved by this code:

              #include "game.h"
              #include <QGraphicsItemGroup>
               
              QGraphicsItemGroup* g = new QGraphicsItemGroup;
               
              Game::Game()
              {
                  player  = new QGraphicsRectItem();
                  rectItem = new QGraphicsRectItem();
                  m_view = new QGraphicsView(this);
                  m_view->setSceneRect(QRectF(0,0,800,600));
               
               
                  this->setBackgroundBrush(Qt::black);
                  m_view->setScene(this);
                  m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                  m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                  m_view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
                  m_view->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
                  m_view->show();
               
                  player->setRect(0,0,100,100);
                  player->setBrush(Qt::red);
                  player->setPos(400,300);
                  player->setFocus();
               
                  rectItem->setRect(0,0,100,100);
                  rectItem->setBrush(Qt::green);
                  rectItem->setPos(400,330);
               
                  this->addItem(g);
               
                  player->setZValue(10);
                  rectItem->setZValue(1);
               
                  g->addToGroup(player);
                  g->addToGroup(rectItem);
               
                  g->setTransformOriginPoint(player->pos().x() + player->boundingRect().width()/2
                                            ,player->pos().y() + player->boundingRect().height()/2);
              }
               
               
              void Game::advance()
              {
               
              }
               
               
              void Game::keyPressEvent(QKeyEvent *event)
              {
                  switch(event->key())  {
                  case Qt::Key_Left :
                      g->moveBy(-5,0);
                      break;
                  case Qt::Key_Right :
                      g->moveBy(5,0);
                      break;
                  case Qt::Key_Up :
                      g->moveBy(0,-5);
                      break;
                  case Qt::Key_Down :
                      g->moveBy(0,5);
                      break;
                  case Qt::Key_Q :
                      g->setRotation(g->rotation()- 3);
                      break;
                  case Qt::Key_W :
                      g->setRotation(g->rotation() +3);
                      break;
                  }
                  update();
              }
              
              1 Reply Last reply
              1

              2/5

              26 Apr 2022, 17:08

              • Login

              • Login or register to search.
              2 out of 5
              • First post
                2/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved