PDA

View Full Version : Collision of a pushbutton with lines does not work???



mahsa
28th July 2012, 06:49
Hi
I want to detect the collision between the movin pushbutton with several lines drawn on the screen , here is the code that I use for this purpose but it does not work I don't know why , please guide me:

(first of all I have changed the type of the lines of the pushbutton to QlineF inorder to be able to use intersection rules)

QLineF line1 (ui->pushButton->x(),ui->pushButton->y(),ui->pushButton->x()+ui->pushButton->width(),ui->pushButton->y());
QLineF line2 (ui->pushButton->x(),ui->pushButton->y(),ui->pushButton->x(),ui->pushButton->y()+ui->pushButton->height());
QLineF line3 (ui->pushButton->x(),ui->pushButton->y()+ui->pushButton->height(),ui->pushButton->x()+ui->pushButton->width(),ui->pushButton->y()+ui->pushButton->height());
QLineF line4 (ui->pushButton->x()+ui->pushButton->width(),ui->pushButton->y(),ui->pushButton->x()+ui->pushButton->width(),ui->pushButton->y()+ui->pushButton->height());

//determining whether or not pushButton collides with lines or not
QPointF* p1= new QPointF;
QPointF* p2= new QPointF;
QPointF* p3= new QPointF;
QPointF* p4= new QPointF;
int x1 = l1.intersect(line1,p1);
int x2 = l1.intersect(line2,p2);
int x3 = l2.intersect(line1,p3);
int x4 = l2.intersect(line2,p4);


if ( x1 == 1)
{
QMessageBox::StandardButton reply2;
reply2 = QMessageBox::critical(this,"Error1" , " G " , QMessageBox::Close);
......................................
}

mvuori
28th July 2012, 11:53
I don't know why it does not work, but before asking people to try to understand it, you should write correct code. For example, the results of your QLineF::intersect() calls are captured in an integer, but the functions really return IntersectType
IntersectType QLineF::intersect ( const QLineF & line, QPointF * intersectionPoint ) const

mahsa
24th August 2012, 13:46
today I solved my problem myself so i will let the others know the solution
for collision detection :
define lines and pushbutton as QRectFs then use QRectF.intersects(&rect);
and it will magically work:))))