Results 1 to 10 of 10

Thread: determining how to bounce the ball

  1. #1
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default determining how to bounce the ball

    Well first of all im still a newbie at Qt so i hope im not asking anything blatantly obvious. That said let's move to the problem...
    Im writing a game similar to Pong (bouncing the ball around the screen and into blocks) and i dont know how to implement ball's collision detection.
    First of all if using the collidingItems method i dont know how to determine if the ball hit the block or the paddle (i have to treat them differently).
    Second i dont know how to figure where the ball hit the object so i know how to bounce it off it properly (in the right direction).

    Im using QGraphicsItems on a QGraphicsScene.

    Thanks for your help

    P.S. If it makes a difference i use Qt Jambi but it still looked like the right forum for this

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: determining how to bounce the ball

    First of all if using the collidingItems method i dont know how to determine if the ball hit the block or the paddle (i have to treat them differently).
    You are getting a list of items as return value.
    Check the list for either block or paddle.
    Second i dont know how to figure where the ball hit the object so i know how to bounce it off it properly (in the right direction).
    Hmm... for some reason I was sure a signal is emitted, but I don't see that in the docs...
    If you are using custom items (subclasses of QGraphicsItem) then you can build a timer in the item that will periodically check if it is colliding.
    Hmm... not so good, as you just might miss a short collision...
    So have a flag set when colliding, and check the collision flag with the timer maybe...
    If not, you can do that on a higher level with collidingItems().

    EDIT:
    actually, you wont miss collisions with a timer, since you are advancing your items, so you can do that every time you advance them.
    Last edited by high_flyer; 24th December 2007 at 10:56.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #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: determining how to bounce the ball

    When you get a list of items, u can check to which class it belongs using dynamic cast,,, or u can use a extra variable in the class.
    For the graphics item inherit the class something like -
    Qt Code:
    1. class MyGraphicsItem: public QGraphicsItem
    2. {
    3. Q_OBJECT; // in case you need signal slots...
    4.  
    5. enum itemType {type1 = UserType+1,type2,...};
    6. itemType m_type;
    7. int type() const /// this is a virtual function of QGraphicsItem ,,, refer documention.
    8. {
    9. return m_type;
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 

    hope this helps

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

    Binji (26th December 2007)

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

    Default Re: determining how to bounce the ball

    Quote Originally Posted by aamer4yu View Post
    When you get a list of items, u can check to which class it belongs using dynamic cast,,, or u can use a extra variable in the class.
    qgraphicsitem_cast() is the way to go.

  6. #5
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: determining how to bounce the ball

    Thanks i (think i) understand how to detect objects now.
    How about the second half of the question? Any ideas? Maybe if i try translating current position of the ball to the hit item's coordinate system and see if it does? Or is there a better way?

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

    Default Re: determining how to bounce the ball

    QGraphicsItem::collidingItems() returns a list of items colliding with a particular item.

  8. #7
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: determining how to bounce the ball

    Quote Originally Posted by wysota View Post
    QGraphicsItem::collidingItems() returns a list of items colliding with a particular item.
    I meant how to determine how to bounce the ball off the hit object. Or am i missing something?

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

    Default Re: determining how to bounce the ball

    Hmmm... what do you mean "how"? You mean physics?

    BTW. Have you seen this?
    http://doc.trolltech.com/qq/qq20-jambi.html

  10. #9
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: determining how to bounce the ball

    I have seen that example but except for some code for working with the scene there isnt really anything i need.
    And to explain what i need... right now i have a ball object which has x and y velocity. So every time a time trigger triggers i move it by that values. But now i dont know whether the ball hit something by its x axis (where i have to invert the x velocity) or y axis (where i have to invert the y velocity).
    One idea i had is to move the ball only by x or y every time i have a time trigger event and then check for collision but im not sure if its such a good idea.
    So if you have a better idea it would really mean a lot
    Thanks

    edit: damn im dumb.. if i get a list of items colliding with the ball i can simply check which coordinates equals those of the ball.
    Last edited by Binji; 29th December 2007 at 12:10.

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

    Default Re: determining how to bounce the ball

    Quote Originally Posted by Binji View Post
    edit: damn im dumb.. if i get a list of items colliding with the ball i can simply check which coordinates equals those of the ball.
    They might not be "equal". Collision is determined by shape, not by the centre point, so you can hit "by the corner" as well and "harder" in one direction than in the other. But these are all simple calculations.

  12. The following user says thank you to wysota for this useful post:

    Binji (30th December 2007)

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
  •  
Qt is a trademark of The Qt Company.