PDA

View Full Version : Collision between QGraphicsItem -



been_1990
16th November 2010, 23:17
Does it matter if I check collision of a GraphicsItem to every other item in a scene or if I check collision of an item against the parent of these items?

Ex:
Option 1:

QGraphicsItem generic1;
QGraphicsItem generic2;
[...]
QGraphicsItem generic30;


QGraphicsItem mainItem;

mainItem.collidesWithItem(generic1);
mainItem.collidesWithItem(generic2);
[...]
mainItem.collidesWithItem(generic30);

Option2:

QGraphicsItem genericItemParent;
QGraphicsItem generic1.setParent(genericItemParent);
QGraphicsItem generic2.setParent(genericItemParent);
[...]
QGraphicsItem generic30.setParent(genericItemParent);


QGraphicsItem mainItem;

mainItem.collidesWithItem(genericItemParent);

Which one is faster?

Timoteo
17th November 2010, 14:44
I would partition my scene to minimize the number of objects I am tracking for collision.

been_1990
17th November 2010, 21:45
How do I partion the scene? Never heard of that...

Timoteo
18th November 2010, 00:56
Did a quick google for you:

http://ubuntu-gamedev.wikispaces.com/2D+Collision+Detection

and

http://tinyurl.com/3yu6r3n

been_1990
18th November 2010, 02:07
Sorry, "how to do it in QT?" should be the question.
So, if I mapToScene() my graphicsView viewport rect, then check any colliding items, then check if those items collide with the "mainItem"?

Example (pseudo-code):

QGraphicsItem generic1;
QGraphicsItem generic2;
[...]
QGraphicsItem generic30;


QGraphicsItem mainItem;
//-------------------------------------//
QGraphicsView view;
QGraphicsScene scene;
void addSceneItems(){/* add all items to scene */ }
view.setScene(scene);

QPolygonF polygon;
polygon = view.mapToScene(0,0,view.width(),view.height);

//--> make poligon "invisible" somehow...

void itemVSpoli()
{
for(int i=0;i<scene.items().count();i++)
{
if(scene.itemAt(i).collidesWithItem(polygon))
//send item to function that checks if
// it collides with mainItem.
}
}

been_1990
19th November 2010, 01:51
So it was simpler than that:


QPolygonF poli = sceneView->mapToScene(0,0,sceneView->width(),sceneView->height());
QList<QGraphicsItem *> sceneItems;
sceneItems = scene->items(poli); //this only returns items that are drawn in the screen

wysota
19th November 2010, 12:32
And why not just do this?

QGraphicsItem *myItem = ...
//...
QList<QGraphicsItem*> collisions = scene->collidingItems(myItem);