Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with shooting bullet from rectangle in different angle
Forum Updated to NodeBB v4.3 + New Features

Problem with shooting bullet from rectangle in different angle

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.4k 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 BeastBook

    Hi i created QGraphicsRectItem and with size 100 x100 and bullet 10 x50
    problem is when i rotate my rect i want to shoot in different way not always in 90 degrees.I tried by creating MyRect object from my class MyRect and get current positions but it doesn't work .The goal is when rect is rotated shoot bullet in correct angle
    0_1556740199875_pic2.png

    void Bullet::move()
    {

    //checking collision
    
    
    
    QList <QGraphicsItem *>colliding_items = collidingItems();
    for(int i=0,n=colliding_items.size();i<n;i++)
    {
        if(typeid (*(colliding_items[i]))==typeid (Enemy))
    {
               //remove them both
            scene()->removeItem(colliding_items[i]);
            scene()->removeItem(this);
    
            //delete from heap
            delete colliding_items[i];
            delete this;
            return;
    }
    }
    

    //here is problem
    /*************************/
    if(y()>200)
    setPos(x()+10,y()-10);

           else
    
         setPos(x(),y()-10);
    

    /****************************/

    if(pos().y() + rect().height() < 0)
    {
        scene()->removeItem(this);
        delete this;
        qDebug() << "Bullet deleted";
    
    }
    

    }

    KillerSmathK Offline
    KillerSmathK Offline
    KillerSmath
    wrote on last edited by
    #2

    @BeastBook
    Use mapToScene(qreal x, qreal y) to get the translated point with rotation transformation.

    setPos(mapToScene(0, -5));
    

    @Computer Science Student - Brazil
    Web Developer and Researcher
    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

    B 1 Reply Last reply
    1
    • KillerSmathK KillerSmath

      @BeastBook
      Use mapToScene(qreal x, qreal y) to get the translated point with rotation transformation.

      setPos(mapToScene(0, -5));
      
      B Offline
      B Offline
      BeastBook
      wrote on last edited by
      #3

      @KillerSmath I tried but it didn't work still while i rotate my rect bullet goes with same angle

      KillerSmathK 1 Reply Last reply
      0
      • B BeastBook

        @KillerSmath I tried but it didn't work still while i rotate my rect bullet goes with same angle

        KillerSmathK Offline
        KillerSmathK Offline
        KillerSmath
        wrote on last edited by
        #4

        @BeastBook
        Can you show more detail of how you are implementing your Bullet Class ?

        @Computer Science Student - Brazil
        Web Developer and Researcher
        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

        1 Reply Last reply
        1
        • B Offline
          B Offline
          BeastBook
          wrote on last edited by
          #5

          Bullet::Bullet()
          {

          //dimensions of the bullet
          setRect(0,0,10,50);
          
          /
          
          
          QTimer *timer = new QTimer();
          connect(timer,SIGNAL(timeout()),this,SLOT(move()));
          
          timer->start(50);
          

          }

          void Bullet::move()
          {

          //check collision
          
          MyRect *rect1= new MyRect();
          
          
          
          QList <QGraphicsItem *>colliding_items = collidingItems();
          for(int i=0,n=colliding_items.size();i<n;i++)
          {
              if(typeid (*(colliding_items[i]))==typeid (Enemy))
          {
                     //remove them both
                  scene()->removeItem(colliding_items[i]);
                  scene()->removeItem(this);
          
                  //delete from heap
                  delete colliding_items[i];
                  delete this;
                  return;
          }
          }
          
          
          
          
          
          //moving bullet
          
          
          
          
          
          
          
          
          setPos(mapToScene(0,-5));
          
          
          
          
          
          
          
          
          /*check if the bullet is out or range
          if(pos().y() + rect().height() < 0)
          {
              scene()->removeItem(this);
              delete this;
              qDebug() << "Bullet deleted";
          
          }
          

          }

          Now MyRect class

          void MyRect::keyPressEvent(QKeyEvent *event)
          {

          if(event->key()==Qt::Key_Left)
          {
          
          
          
          
           setTransformOriginPoint(QPoint(50,50));
            setRotation(rotation()-10);
          
          
          
          
          
          
            
          
          }
          
          else if(event->key()==Qt::Key_Right)
          {
          
          
              setTransformOriginPoint(QPoint(50,50));
              setRotation(rotation()+10);
          
            
          
          }
          
          
          else if (event->key()==Qt::Key_Space) {
          
          
              Bullet *bullet = new Bullet();
              qDebug() << "Bullet created";
              bullet->setPos(x()+50,y()-5);
              scene()->addItem(bullet);
          
          }
          

          }

          and this is main , enemy class is fine

          QGraphicsScene *scene = new QGraphicsScene();
          
          
          
          MyRect *player  = new MyRect();
          
          
          player->setRect(0,0,100,100);  
          
          
          //add player to scene
          scene->addItem(player);
          
          
          player->setFlag(QGraphicsItem::ItemIsFocusable); 
          player->setFocus();
          
          
          QGraphicsView *view  = new QGraphicsView(scene); //adding scene on view
          
          //lock scrollbar
          view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          
          view->show();  
          
          view->setFixedSize(800,600);
          scene->setSceneRect(0,0,800,600); /*SetRect on scene point 0 0 size 800x600*/
          
          //setting position of player
          player->setPos(view->width()/2,view->height()/2);
          
          
          //creating enemy
          QTimer *timer = new QTimer();
          QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
          timer->start(2000);
          
          KillerSmathK 1 Reply Last reply
          0
          • B BeastBook

            Bullet::Bullet()
            {

            //dimensions of the bullet
            setRect(0,0,10,50);
            
            /
            
            
            QTimer *timer = new QTimer();
            connect(timer,SIGNAL(timeout()),this,SLOT(move()));
            
            timer->start(50);
            

            }

            void Bullet::move()
            {

            //check collision
            
            MyRect *rect1= new MyRect();
            
            
            
            QList <QGraphicsItem *>colliding_items = collidingItems();
            for(int i=0,n=colliding_items.size();i<n;i++)
            {
                if(typeid (*(colliding_items[i]))==typeid (Enemy))
            {
                       //remove them both
                    scene()->removeItem(colliding_items[i]);
                    scene()->removeItem(this);
            
                    //delete from heap
                    delete colliding_items[i];
                    delete this;
                    return;
            }
            }
            
            
            
            
            
            //moving bullet
            
            
            
            
            
            
            
            
            setPos(mapToScene(0,-5));
            
            
            
            
            
            
            
            
            /*check if the bullet is out or range
            if(pos().y() + rect().height() < 0)
            {
                scene()->removeItem(this);
                delete this;
                qDebug() << "Bullet deleted";
            
            }
            

            }

            Now MyRect class

            void MyRect::keyPressEvent(QKeyEvent *event)
            {

            if(event->key()==Qt::Key_Left)
            {
            
            
            
            
             setTransformOriginPoint(QPoint(50,50));
              setRotation(rotation()-10);
            
            
            
            
            
            
              
            
            }
            
            else if(event->key()==Qt::Key_Right)
            {
            
            
                setTransformOriginPoint(QPoint(50,50));
                setRotation(rotation()+10);
            
              
            
            }
            
            
            else if (event->key()==Qt::Key_Space) {
            
            
                Bullet *bullet = new Bullet();
                qDebug() << "Bullet created";
                bullet->setPos(x()+50,y()-5);
                scene()->addItem(bullet);
            
            }
            

            }

            and this is main , enemy class is fine

            QGraphicsScene *scene = new QGraphicsScene();
            
            
            
            MyRect *player  = new MyRect();
            
            
            player->setRect(0,0,100,100);  
            
            
            //add player to scene
            scene->addItem(player);
            
            
            player->setFlag(QGraphicsItem::ItemIsFocusable); 
            player->setFocus();
            
            
            QGraphicsView *view  = new QGraphicsView(scene); //adding scene on view
            
            //lock scrollbar
            view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            
            view->show();  
            
            view->setFixedSize(800,600);
            scene->setSceneRect(0,0,800,600); /*SetRect on scene point 0 0 size 800x600*/
            
            //setting position of player
            player->setPos(view->width()/2,view->height()/2);
            
            
            //creating enemy
            QTimer *timer = new QTimer();
            QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
            timer->start(2000);
            
            KillerSmathK Offline
            KillerSmathK Offline
            KillerSmath
            wrote on last edited by KillerSmath
            #6

            @BeastBook
            Okay, you are creating the Bullet on below code

            else if (event->key()==Qt::Key_Space) {
                Bullet *bullet = new Bullet();
                qDebug() << "Bullet created";
                bullet->setPos(x()+50,y()-5);
                scene()->addItem(bullet);
            }
            

            However, where were you change the rotation of this bullet to the same rotation of the player ?

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            B 1 Reply Last reply
            1
            • KillerSmathK KillerSmath

              @BeastBook
              Okay, you are creating the Bullet on below code

              else if (event->key()==Qt::Key_Space) {
                  Bullet *bullet = new Bullet();
                  qDebug() << "Bullet created";
                  bullet->setPos(x()+50,y()-5);
                  scene()->addItem(bullet);
              }
              

              However, where were you change the rotation of this bullet to the same rotation of the player ?

              B Offline
              B Offline
              BeastBook
              wrote on last edited by
              #7

              @KillerSmath That should be in metod in Bullet class void move() where i put your code setPos(mapToScene(0,-5));

              void Bullet::move()
              {

              MyRect *rect1= new MyRect();
              
              
              
              QList <QGraphicsItem *>colliding_items = collidingItems();
              for(int i=0,n=colliding_items.size();i<n;i++)
              {
                  if(typeid (*(colliding_items[i]))==typeid (Enemy))
              {
                         //remove them both
                      scene()->removeItem(colliding_items[i]);
                      scene()->removeItem(this);
              
                      //delete from heap
                      delete colliding_items[i];
                      delete this;
                      return;
              }
              }
              
              
              
              
              
              setPos(mapToScene(10,0));
              
              
              
              
              
              
              if(pos().y() + rect().height() < 0)
              {
                  scene()->removeItem(this);
                  delete this;
                  qDebug() << "Bullet deleted";
              
              }
              

              }

              Also i try this way in metod move ()
              get rect x and y axis

              and set rotation position
              setPos(rect1->pos().x() ,rect1->pos().y());
              size of scene is 800x600 and i center my rect with 100x100 at the half 400x300

              KillerSmathK 1 Reply Last reply
              0
              • B BeastBook

                @KillerSmath That should be in metod in Bullet class void move() where i put your code setPos(mapToScene(0,-5));

                void Bullet::move()
                {

                MyRect *rect1= new MyRect();
                
                
                
                QList <QGraphicsItem *>colliding_items = collidingItems();
                for(int i=0,n=colliding_items.size();i<n;i++)
                {
                    if(typeid (*(colliding_items[i]))==typeid (Enemy))
                {
                           //remove them both
                        scene()->removeItem(colliding_items[i]);
                        scene()->removeItem(this);
                
                        //delete from heap
                        delete colliding_items[i];
                        delete this;
                        return;
                }
                }
                
                
                
                
                
                setPos(mapToScene(10,0));
                
                
                
                
                
                
                if(pos().y() + rect().height() < 0)
                {
                    scene()->removeItem(this);
                    delete this;
                    qDebug() << "Bullet deleted";
                
                }
                

                }

                Also i try this way in metod move ()
                get rect x and y axis

                and set rotation position
                setPos(rect1->pos().x() ,rect1->pos().y());
                size of scene is 800x600 and i center my rect with 100x100 at the half 400x300

                KillerSmathK Offline
                KillerSmathK Offline
                KillerSmath
                wrote on last edited by
                #8

                @BeastBook
                You have misunderstand me, i told about the rotation when the bullet is created.
                The Bullet will be create on front of player and move in same direction of rotation of player when the bullet is created.

                See snippet of code to understand how it would be implemented.

                else if (event->key()==Qt::Key_Space) {
                    Bullet *bullet = new Bullet();
                    qDebug() << "Bullet created";
                    bullet->setPos(mapToScene(50, -5)); // use MapToScene to get the translated position of player with rotation transformation
                   bullet->setRotation(rotation()) // set the bullet rotation to the same rotation of the player when the bullet is created
                    scene()->addItem(bullet);
                }
                

                The setPos call on Bullet Move function is just to keep moving itself. However, if you don't rotate it to the same direction of player, it will be move to a default direction.

                @Computer Science Student - Brazil
                Web Developer and Researcher
                “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                B 1 Reply Last reply
                0
                • KillerSmathK KillerSmath

                  @BeastBook
                  You have misunderstand me, i told about the rotation when the bullet is created.
                  The Bullet will be create on front of player and move in same direction of rotation of player when the bullet is created.

                  See snippet of code to understand how it would be implemented.

                  else if (event->key()==Qt::Key_Space) {
                      Bullet *bullet = new Bullet();
                      qDebug() << "Bullet created";
                      bullet->setPos(mapToScene(50, -5)); // use MapToScene to get the translated position of player with rotation transformation
                     bullet->setRotation(rotation()) // set the bullet rotation to the same rotation of the player when the bullet is created
                      scene()->addItem(bullet);
                  }
                  

                  The setPos call on Bullet Move function is just to keep moving itself. However, if you don't rotate it to the same direction of player, it will be move to a default direction.

                  B Offline
                  B Offline
                  BeastBook
                  wrote on last edited by
                  #9

                  @KillerSmath Thank you very much now i understand this 0_1556748542031_PIC3.png

                  One more thing how to implement in my function to go to the end with this angle when i shoot

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BeastBook
                    wrote on last edited by
                    #10

                    I solve it i put in my move method function adnvance (int phase)

                    if(!phase) return;

                        QPointF location = this->pos();
                    
                        setPos(mapToParent(0,-(5)));
                    

                    Thank you again

                    KillerSmathK 1 Reply Last reply
                    0
                    • B BeastBook

                      I solve it i put in my move method function adnvance (int phase)

                      if(!phase) return;

                          QPointF location = this->pos();
                      
                          setPos(mapToParent(0,-(5)));
                      

                      Thank you again

                      KillerSmathK Offline
                      KillerSmathK Offline
                      KillerSmath
                      wrote on last edited by
                      #11

                      @BeastBook
                      Do not confuse mapToParent with mapToScene. The value of mapToParent is only equal to mapToScene if the object has not a parent because the default parent is the scene.

                      So, if every issues have been solved, mark this topic as Solved.

                      @Computer Science Student - Brazil
                      Web Developer and Researcher
                      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                      1 Reply Last reply
                      0

                      • Login

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