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 colliding items with scene
Forum Updated to NodeBB v4.3 + New Features

Problem with colliding items with scene

Scheduled Pinned Locked Moved Solved General and Desktop
1 Posts 1 Posters 293 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
    BeastBook
    wrote on last edited by
    #1

    Hi i am creted enemy in my game and i want when my enemy hit the end of scene to go back in different angle .I Have a problem with implementation
    0_1557159745351_RandomAngleForEnemy.png
    I want something like this

    this is my move method

    void Enemy::move()
    {

    /*Check collision with other enemies .This works fine*/
    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 enemy down
     setPos(x(),y()+5);
    
    
       
    
    
    
    
    
       if(scene()->collidingItems(this).isEmpty())
           {
               //no collision
              qDebug() << "Still no collision";
           }
           else
           {
               //collision!!!!
    
    
               //Set the position
               DoCollision();
           }
    

    }

    now goes do collision method

    //Get a new position
    
     //Change the angle with a little randomness
     if(((qrand() %1)))
     {
         setRotation(rotation() + (180 + (qrand() % 10)));
     }
     else
     {
         setRotation(rotation() + (180 + (qrand() % -10)));
     }
    
     //see if the new position is in bounds
     QPointF newpoint = mapToParent(-(boundingRect().width()), -(boundingRect().width() + 2));
    
    
    
     if(!scene()->sceneRect().contains((newpoint)))
     {
         //move it back in bounds
         newpoint = mapToParent(0,0);
     }
     else
     {
         //set the new position
         qDebug() << "Collision";
         setPos(newpoint);
    
    
     }
    

    this is my constructor

    Enemy::Enemy():QObject(),QGraphicsPixMapItem()
    {

    int random_number = (rand() % 700)+80;

    setPos(random_number,0);
    
    // drew the rect
    setPixmap(QPixmap(":/res/RSC/enemy.png"));
    setRotation(180);
    
    // connect
    QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));
    
    timer->start(50)
    

    }

    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