void MainWindow::resizeItem() { for (int i = 0; i < 10; i++) { QGraphicsRectItem* theFrame = item1; QPointF P1(100 - (7.0 * i)/3.0, 50 -(5.0*i)/3.0); graphicsSheet->addPoint("P1", P1); graphicsSheet->addPoint("pos", theFrame->pos()); QPointF P2i = QPointF(theFrame->rect().width(), theFrame->rect().height()); QPointF P2 = theFrame->mapToScene(P2i); qDebug() << "P1:" << P1; qDebug() << "P2:" << P2; graphicsSheet->addPoint("P2", P2); QTransform t; t.rotate(-theFrame->rotation()); t.translate(-P1.x(), -P1.y()); QPointF P2t = t.map(P2); // qDebug() << "P2t:" << P2t; // OK /*************************/ QPointF P3(P2t.x()/2, P2t.y()/2); // qDebug() << "P3:" << P3; QTransform t2; t2.translate(P1.x(), P1.y()); t2.rotate(theFrame->rotation()); QPointF P3t = t2.map(P3); //// qDebug() << "P3t:" << P3t; // OK graphicsSheet->addPoint("P3t", P3t); QTransform t3; t3.translate(P3t.x(), P3t.y()); t3.rotate(-theFrame->rotation()); t3.translate(-(P3t.x()), -(P3t.y())); QPointF newPos = t3.map(P1); QSizeF newSize = QSizeF(P2t.x(), P2t.y()); graphicsSheet->addPoint("newPos", newPos); RectItem* item1 = new RectItem(QRectF(newPos.x(), newPos.y(), newSize.width(), newSize.height())); qreal angle = 30; QPointF center2 = QPointF(item1->rect().width() / 2, item1->rect().height() / 2); item1->setTransformOriginPoint(center2); item1->setRotation(angle); graphicsSheet->scene()->addItem(item1); } }
QList<Manifold *> Element::detectCollidingObjects(void) { collidingItemsList = scene()->collidingItems(this); QList<Manifold *> mList = * new QList<Manifold *>(); for (int j = 0; j < collidingItemsList.size(); j++) { mList.append(new Manifold()); } int i = 0; // While detect colliding items while (!collidingItemsList.isEmpty()) { // Pop the first item from the list QGraphicsRectItem * rectB = dynamic_cast<QGraphicsRectItem*>(collidingItemsList.takeFirst()); mList[i]->second_object = dynamic_cast<Object2d*>(rectB); // Vector between two colliding objects A and B QPointF dist_AB = rectB->mapToScene(rectB->rect().center()) - this->mapToScene(this->rect().center()); // x overlap between two colliding objects qreal overlap_x = std::min<qreal>(mapToScene(rect().topLeft()).x() + rect().width(), rectB->mapToScene(rectB->rect().topLeft()).x() + rectB->rect().width()) - std::max<qreal>(mapToScene(rect().topLeft()).x(), rectB->mapToScene(rectB->rect().topLeft()).x()); if (overlap_x > 0) { // y overlap between two colliding objects qreal overlap_y = std::min<qreal>(mapToScene(rect().topLeft()).y() + rect().height(), rectB->mapToScene(rectB->rect().topLeft()).y() + rectB->rect().height()) - std::max<qreal>(mapToScene(rect().topLeft()).y(), rectB->mapToScene(rectB->rect().topLeft()).y()); if (overlap_y > 0) { if (overlap_y > overlap_x) { if (dist_AB.x() < 0) { mList[i]->normal_x = -1; mList[i]->normal_y = 0; } else { mList[i]->normal_x = 1; mList[i]->normal_y = 0; } mList[i]->penetration = overlap_x; } else { if (dist_AB.y() < 0) { mList[i]->normal_x = 0; mList[i]->normal_y = -1; } else { mList[i]->normal_x = 0; mList[i]->normal_y = 1; } mList[i]->penetration = overlap_y; } } } ++i; } return mList; }