Results 1 to 7 of 7

Thread: Collision detect in QGraphicsScene

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Collision detect in QGraphicsScene

    Hello,
    I am using QGraphicsScene , QGraphicsView and QGraphicsPixmapItem to write a game like Tank.
    I have wrote a class Tank inherriting from QGraphicsPixmapItem, and when I press some key, like Space, it's function shoot() is called. And I want to new some bullet in this function, then let the bullet go straight along the current direction.
    The class Bullet is inherriting from QGraphicsPixmapItem too.
    The problem is, I do not know how to detect whether the bullet reaches the border or hit the enemy tank, if it hits some enemy tank, how to destroy that tank.

    I am using Qt4.5
    Thanks in advance!!

  2. The following user says thank you to sophister for this useful post:


  3. #2
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Collision detect in QGraphicsScene


  4. The following user says thank you to jano_alex_es for this useful post:


  5. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision detect in QGraphicsScene

    You can also have a look at colliding mice example in Qt Demos under Graphics View section

  6. The following user says thank you to aamer4yu for this useful post:


  7. #4
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default

    Yes, I have read the mice example.
    It uses a QTimerto check the collisions among the mice.
    But I think the case is a little different, in my case, if I use a QTimer, and if many tanks are shooting, I can not imagine how to write those codes. And even if I use a QTimer, I haven't found any way to destroy the tank hit by one bullet.
    It seems a long way to go...

    You are right, but these two functions need to be called every time I want to know whether the bullet hit some tank during its lifetime.
    If there is someway like, event loop, i.e, when the bullet hits a tank, an event is sent to both the bullet and that tank, then I can destroy the bullet and the tank.
    But, it seems there is no such event as far as I can see.
    Last edited by wysota; 4th December 2009 at 18:35.

  8. The following user says thank you to sophister for this useful post:


  9. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Collision detect in QGraphicsScene

    But, it seems there is no such event as far as I can see.
    Even if there was such event, it would internally need to monitor items movement at regular intervals...
    By the way in 4.6 theres also sub attack example...but I gues thats done using animation framework..dont know how collision detection is used in it..
    may be u can check and let us know

  10. The following 2 users say thank you to aamer4yu for this useful post:

    sophister (5th December 2009)

  11. #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 detect in QGraphicsScene

    The easiest way is to reimplement QGraphicsScene::advance(). It's called periodically (if you make it to be called of course) and you can easily detect collisions there, provided you have pointers to appropriate items.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following 2 users say thank you to wysota for this useful post:

    sophister (5th December 2009)

  13. #7
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default

    Ok, I downloaded the QT4.6 just now.
    I will study that sub attack example.
    Thanks a lot!!

    Hmm, I think I will set up a timer in my view and call the QGraphicsScene::advance()periodically.
    Thanks a lot!!
    Last edited by wysota; 5th December 2009 at 09:04.

  14. The following user says thank you to sophister for this useful post:


Similar Threads

  1. how to connect events with signals in QGraphicsScene?
    By nataly in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2009, 15:20
  2. Replies: 1
    Last Post: 30th October 2009, 08:52
  3. Replies: 0
    Last Post: 5th March 2009, 06:54
  4. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27
  5. 2D Race Car collision detection
    By neonnds in forum Qt Programming
    Replies: 0
    Last Post: 6th July 2008, 08:10

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.