Results 1 to 4 of 4

Thread: Collision Detection between paintings????

  1. #1
    Join Date
    Jul 2012
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows Symbian S60
    Thanks
    8

    Question Collision Detection between paintings????

    Hi

    I have painted a big rectangle which surrounds a tiny push button , the push button moves to a new place where the mouse is clicked. but the problem is that I do not want this push button to come out of the big rectangle so I need collision detection which will help the pushbutton not to pass through the boundaries. becouse I'm new to Qt , I do not know what to do exactly. if some helps me about it I will be grateful.
    here's my dialog.cpp code , I only have dialog.cpp and main.cpp ( this part works correct but I do not know what else I should do)
    please tell me the source code for collision in this case
    thanks


    Dialog :: Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui:: Dialog)
    {
    ui->setupUi(this);

    }
    void Dialog :: paintEvent(QPaintEvent *e)
    {
    QPainter painter(this);
    QRect rect(5,5,350,395);
    QPen mypen(Qt::darkCyan);
    mypen.setWidth(20);
    painter.setPen(mypen);
    painter.drawRect(rect);
    }

    void Dialog::mousePressEvent(QMouseEvent *event)
    {
    int _x_pos = event->pos().x();
    int _y_pos = event->pos().y();
    int _btn_pos_x = ui->pushButton->x() + ui->pushButton->width()/2;
    int _btn_pos_y = ui->pushButton->y() + ui->pushButton->height()/2;
    int offset = 40;
    int start = _x_pos - offset;
    int end = _x_pos + offset;
    int begin = _y_pos - 10;
    int finish = _y_pos + 10;
    if( _btn_pos_x >= start && _btn_pos_x <= end ){
    if( _y_pos < _btn_pos_y )
    ui->pushButton->move(ui->pushButton->pos().x(), _btn_pos_y - 100);
    else
    ui->pushButton->move(ui->pushButton->pos().x(), _btn_pos_y + 100);
    }

    else if( _btn_pos_y >= begin && _btn_pos_y <= finish )
    {
    if( _x_pos > _btn_pos_x )
    ui->pushButton->move(_btn_pos_x + 10,ui->pushButton->pos().y() );
    else
    ui->pushButton->move( _btn_pos_x /3 ,ui->pushButton->pos().y());

    }
    QDialog::mousePressEvent(event);
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Collision Detection between paintings????

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows Symbian S60
    Thanks
    8

    Default Re: Collision Detection between paintings????

    I have 2 more questions:
    1) in the discription of "bool QRect::contains ( const QPoint & point, bool proper = false ) const" what does PROPER mean?

    2) what should I use for the collision of the smaller rectangle with a simple painted line?

    thanks a lot for wasting ur time answering my silly questions

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Collision Detection between paintings????

    Quote Originally Posted by mahsa View Post
    1) in the discription of "bool QRect::contains ( const QPoint & point, bool proper = false ) const" what does PROPER mean?
    It's described in the docs: "If proper is true, this function only returns true if the given point is inside the rectangle (i.e., not on the edge)."

    2) what should I use for the collision of the smaller rectangle with a simple painted line?
    Use math to calculate intersection of a line with four sides of a rectangle. Alternatively convert the rect and line to painter paths and intersect those using QPainterPath API.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    mahsa (19th July 2012)

Similar Threads

  1. QGraphicsPixmapItem collision detection
    By sophister in forum Qt Programming
    Replies: 7
    Last Post: 4th July 2010, 19:36
  2. collision detection...
    By Muffin in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 10:28
  3. turn off collision detection?
    By Deacon in forum Qt Programming
    Replies: 14
    Last Post: 30th December 2008, 17:37
  4. Collision detection QGraphicsItem
    By Blade in forum Qt Programming
    Replies: 5
    Last Post: 5th January 2007, 10:20
  5. Painting and collision detection
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2006, 08:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.