Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with shooting bullet from rectangle in different angle



  • 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";
    
    }
    

    }



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

    setPos(mapToScene(0, -5));
    


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



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



  • 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);


  • @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 ?



  • @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



  • @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.



  • @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



  • 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



  • @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.


Log in to reply