PDA

View Full Version : Collision handling 101



Morea
24th November 2006, 12:51
Hi.
I'm writing a game, and now is the time for discovering when armies bumps into each other.

So with a lot of Army objects (subclass of QGraphicsItems) moving around, I thought about doing like this (psudocodish)


void Army::move()
{
while ( movement path not empty)
{
setPos( next position in path);
list = collidingItems();
foreach (item,list)
{
if (item == enemy) do stuff etc.
else if ....
}
}
}

Any other way of doing this that I haven't thought about?

I should add that there will always be atleast one item that I collide with, and that is the background map that is inserted as an QGraphicsPixmap, and that might slow things down? What do you think?
Is there any betterway of putting a pixmap as background in a scene?

wysota
24th November 2006, 13:04
Is there any betterway of putting a pixmap as background in a scene?

How about using QGraphicsScene::setBackgroundBrush() or QGraphicsScene::drawBackground()?

Morea
24th November 2006, 13:17
That would be a very bad suggestion since then I would have to change a lot of stuff in my code and make it better. ;)

wysota
24th November 2006, 17:56
A lot of stuff just to replace a pixmap item with a single call to the scene?

BTW. The suggestion is not bad just because you did something else incorrectly. If something is bad, then it's the design of your code, not a simple function call which you might use to solve your problem. Redesigning your code, even if it means rewriting a major part of the application, might result in many benefits and might overcome many future (or past) problems.

Morea
24th November 2006, 18:47
No, I was actually lucky. It wasn't so much work. The problem I had was that I had checks for how many items it would find at a position, now this number had to be lowered with 1. In fact, the code looks better now. And I have started to think about other things that I perhaps should do.
This is the problem I always seem to have. I have to start to get something working quickly, otherwise I tend to loose interest in it. At the same time I tend to introduce designflaws that has to be corrected later on.

I hope you noticed the smiley that was menat to indicate some kind of irony. You have never been wrong in your suggestions, I really appreaciate all the help people are giving me here. Thank you all.

wysota
24th November 2006, 22:13
Maybe you should prototype your code (for example with PyQt) so that you can release a preview quickly to maintain the level of interest and them implement it correctly with C++ once the major design issues are solved?

Morea
25th November 2006, 14:14
If I use the QGraphicsScene::drawBackground()as was suggested. Then what should that look like?

void Gameboard::drawBackground ( QPainter * painter, const QRectF & rect )
{
painter->drawPixmap( .............. , background_pic , ....... );
}

The dotted lines should probably contain rect in some way, but I'm not sure how.

wysota
25th November 2006, 15:47
It depends how do you want that background to behave. Should it repeat itself or should it scale to the size of the scene, etc. You might need to make a for loop that draws the pixmap multiple times to fill the "rect".

Morea
25th November 2006, 16:06
I only have one big pixmap that should fill up the entire background. The scene has the same size as the pixmap.

wysota
25th November 2006, 20:17
So just draw the pixmap.


painter->drawPixmap(0,0, pixmap);