Collision detection works
authorHeli Hyvättinen <heli@pantteri.(none)>
Thu, 12 May 2011 08:15:10 +0000 (11:15 +0300)
committerHeli Hyvättinen <heli@pantteri.(none)>
Thu, 12 May 2011 08:15:10 +0000 (11:15 +0300)
Now stops at rocks.

aave.png [new file with mode: 0644]
kari.png [new file with mode: 0644]
mainwindow.cpp
meri.png [new file with mode: 0644]
orientationcontrol2pix.qrc
orientationcontrolledgraphicspixmapobject.cpp
orientationcontrolledgraphicspixmapobject.h
tursas.png [new file with mode: 0644]

diff --git a/aave.png b/aave.png
new file mode 100644 (file)
index 0000000..233e6e9
Binary files /dev/null and b/aave.png differ
diff --git a/kari.png b/kari.png
new file mode 100644 (file)
index 0000000..ae98a8c
Binary files /dev/null and b/kari.png differ
index 8502b60..9c8605f 100644 (file)
@@ -27,7 +27,7 @@ MainWindow::MainWindow(QWidget *parent)
 
     //the boundaries of the scene are set to match the size of the view window, which is not
     //available in the constructor --> timer needed
-    QTimer::singleShot(60,this,SLOT(initializeBoundaries()));
+    QTimer::singleShot(100,this,SLOT(initializeBoundaries()));
 
 
     QAction * pPauseAction = new QAction(tr("Pause"),this);
@@ -36,6 +36,16 @@ MainWindow::MainWindow(QWidget *parent)
     connect(pPauseAction,SIGNAL(triggered(bool)),this,SLOT(pause(bool)));
     menuBar()->addAction(pPauseAction);
 
+    QGraphicsPixmapItem * pGhost = pScene_->addPixmap(QPixmap(":/pix/aave.png"));
+    pGhost->setData(0,"ghost");
+    QGraphicsPixmapItem * pRock =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
+        QGraphicsPixmapItem * pRock2 =  pScene_->addPixmap(QPixmap(":/pix/kari.png"));
+    pRock->moveBy(40,40);
+    pRock->setData(0,"rock");
+    pRock2->moveBy(80,80);
+    pRock2->setData(0,"rock");
+
+
 }
 
 MainWindow::~MainWindow()
diff --git a/meri.png b/meri.png
new file mode 100644 (file)
index 0000000..9aa7e31
Binary files /dev/null and b/meri.png differ
index b40ac7f..a47e75d 100644 (file)
@@ -2,5 +2,7 @@
     <qresource prefix="/pix">
         <file>meri.png</file>
         <file>tursas.png</file>
+        <file>kari.png</file>
+        <file>aave.png</file>
     </qresource>
 </RCC>
index 5b3b4b1..cff72a6 100644 (file)
@@ -48,6 +48,9 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor()
 
  //   qDebug() << deltax << " " << deltay;
 
+    int oldx = x();
+    int oldy = y();
+
     int newx = x() + deltax/15;
     int newy = y() + deltay/15;
 
@@ -58,6 +61,17 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor()
     setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width()));
     setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height()));
 
+    QList<QGraphicsItem*>  collidesList = collidingItems();
+    if (!collidesList.isEmpty())
+    {
+        qDebug() << collidesList.at(0)->data(0);
+        if (collidesList.at(0)->data(0) == "rock")
+        {
+            setX(oldx);
+            setY(oldy);
+        }
+    }
+
 }
 
 
@@ -67,5 +81,11 @@ void OrientationControlledGraphicsPixmapObject::setBoundaries(QRectF boundaryrec
 
 }
 
+void OrientationControlledGraphicsPixmapObject::setObstacles(int key, QList<QVariant> values)
+{
+    obstacleKey_ = key;
+    obstacleValues_ = values;
+}
+
 
 
index da7c3ed..22435c5 100644 (file)
@@ -7,6 +7,7 @@
 QTM_USE_NAMESPACE
 
 
+
 class OrientationControlledGraphicsPixmapObject : public QObject, public QGraphicsPixmapItem
 {
     Q_OBJECT
@@ -22,6 +23,12 @@ public slots:
     void readRotationSensor();
     void setBoundaries(QRectF boundaryrect);
 
+    /*! sets what QGraphicsItems to treat as obstacles not to move on top of
+        @param key The key to be used with QGraphicsItem::data() to find the correct value to compare
+        @param values The values received from QGraphicsItem::data() to treat as obstacles
+    */
+    void setObstacles(int key, QList<QVariant> values);
+
 
 private:
 
@@ -29,6 +36,9 @@ private:
 
     QRectF boundaryrect_;
 
+    int obstacleKey_;
+    QList<QVariant> obstacleValues_;
+
 };
 
 #endif // ORIENTATIONCONTROLLEDGRAPHICSPIXMAPOBJECT_H
diff --git a/tursas.png b/tursas.png
new file mode 100644 (file)
index 0000000..1529d41
Binary files /dev/null and b/tursas.png differ