Results 1 to 10 of 10

Thread: Collision handling 101

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Collision handling 101

    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)

    Qt Code:
    1. void Army::move()
    2. {
    3. while ( movement path not empty)
    4. {
    5. setPos( next position in path);
    6. list = collidingItems();
    7. foreach (item,list)
    8. {
    9. if (item == enemy) do stuff etc.
    10. else if ....
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Collision handling 101

    Quote Originally Posted by Morea View Post
    Is there any betterway of putting a pixmap as background in a scene?
    How about using QGraphicsScene::setBackgroundBrush() or QGraphicsScene::drawBackground()?

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision handling 101

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Collision handling 101

    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.

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision handling 101

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Collision handling 101

    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?

  7. #7
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision handling 101

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Collision handling 101

    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".

  9. #9
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision handling 101

    I only have one big pixmap that should fill up the entire background. The scene has the same size as the pixmap.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Collision handling 101

    So just draw the pixmap.

    Qt Code:
    1. painter->drawPixmap(0,0, pixmap);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. need help for tree Handling concept
    By jyoti in forum Qt Programming
    Replies: 5
    Last Post: 24th November 2006, 05:52
  2. Painting and collision detection
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2006, 09:57
  3. qmouseEvent handling
    By nass in forum Qt Tools
    Replies: 9
    Last Post: 13th October 2006, 09:55
  4. Keyboard Handling
    By ToddAtWSU in forum Qt Programming
    Replies: 4
    Last Post: 5th July 2006, 14:25
  5. Handling multiple UDP sockets
    By ecphora in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 21:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.