Collision of Two Rect

Discussion in 'Python' started by Alex Gardner, May 3, 2013.

  1. Alex Gardner

    Alex Gardner Guest

    When rect A collides with rect B they stick when I am wanting A to bounce off of B. I have tried different methods, but none seem to work. My source is here: http://pastebin.com/CBYPcubL

    The collision code itself is below:
    ------
    # Bounce off of the paddle
    if paddle_rect.colliderect(ball_rect):
    y_vel*=-1
    x_vel*=-1
     
    Alex Gardner, May 3, 2013
    #1
    1. Advertising

  2. Alex Gardner

    MRAB Guest

    On 03/05/2013 23:23, Alex Gardner wrote:
    > When rect A collides with rect B they stick when I am wanting A to bounce off of B. I have tried different methods, but none seem to work. My source is here: http://pastebin.com/CBYPcubL
    >
    > The collision code itself is below:
    > ------
    > # Bounce off of the paddle
    > if paddle_rect.colliderect(ball_rect):
    > y_vel*=-1
    > x_vel*=-1
    >

    Apart from the other comments, I'm not sure about this line:

    y_vel = (math.fabs(2 - x_vel)**2) ** .5

    Firstly, instead of math.fabs you could use abs:

    y_vel = (abs(2 - x_vel)**2) ** .5

    Then again, you're squaring the result, and the square of (2 - x_vel)
    will always be positive anyway:

    y_vel = ((2 - x_vel)**2) ** .5

    You're also calculating getting the square root, for which math.sqrt
    would be simpler:

    y_vel = math.sqrt((2 - x_vel)**2)

    Then again, you're calculating the square root of a squared number, so
    you might as well just write:

    y_vel = abs(2 - x_vel)
     
    MRAB, May 4, 2013
    #2
    1. Advertising

  3. On 4 May 2013 00:42, Ian Kelly <> wrote:

    > The other thing that is suspicious about the code you posted is that
    > it has two different notions of the ball's position that are not
    > necessarily in agreement. There is the ball_rect, and there are also
    > the x and y variables.


    <snip>

    > You should be careful to make sure these
    > variables agree at all times -- or better yet, get rid of x and y
    > entirely, so that you only have one notion of the ball's position to
    > worry about.



    Pygame uses integers for its Rects and thus I advise much the opposite:
    *never* store position in Rects.

    Sorry, but it's true. You'll need to keep x and y around and try to use
    Rects only when representing pixels on the screen. Pygame has a lot of
    deficiencies with its default set of objects and functions, so it's
    something to get used to.
     
    Joshua Landau, May 6, 2013
    #3
  4. Alex Gardner

    Ian Kelly Guest

    On May 6, 2013 10:39 AM, "Joshua Landau" <> wrote:
    >
    > On 4 May 2013 00:42, Ian Kelly <> wrote:
    >>
    >> The other thing that is suspicious about the code you posted is that
    >> it has two different notions of the ball's position that are not
    >> necessarily in agreement. There is the ball_rect, and there are also
    >> the x and y variables.

    >
    > <snip>
    >>
    >> You should be careful to make sure these
    >> variables agree at all times -- or better yet, get rid of x and y
    >> entirely, so that you only have one notion of the ball's position to
    >> worry about.

    >
    >
    > Pygame uses integers for its Rects and thus I advise much the opposite:

    *never* store position in Rects.
    >
    > Sorry, but it's true. You'll need to keep x and y around and try to use

    Rects only when representing pixels on the screen. Pygame has a lot of
    deficiencies with its default set of objects and functions, so it's
    something to get used to.

    You don't need subpixel positioning for many games -- arguably including
    Pong, although I suppose the argument would be stronger if the OP were not
    using a ludicrously high frame rate of 200 fps, which is going to limit the
    number of reasonable integer velocities available. For games where logical
    coordinates and screen coordinates need to be separated though, I agree.
     
    Ian Kelly, May 6, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bunty

    RECt Structure Problem

    bunty, Jul 6, 2006, in forum: C++
    Replies:
    9
    Views:
    417
    Jerry Coffin
    Jul 14, 2006
  2. Replies:
    2
    Views:
    266
    Thomas J. Gritzan
    Feb 13, 2007
  3. globalrev

    pygame: rect moveto?

    globalrev, May 3, 2008, in forum: Python
    Replies:
    1
    Views:
    337
    Marc 'BlackJack' Rintsch
    May 3, 2008
  4. Chris Riesbeck

    getting computed clip rect

    Chris Riesbeck, Jul 15, 2008, in forum: Javascript
    Replies:
    31
    Views:
    347
    Laurent vilday
    Jul 16, 2008
  5. Scott Siegler

    pygame.Rect question

    Scott Siegler, Apr 9, 2012, in forum: Python
    Replies:
    1
    Views:
    313
    Peter Pearson
    Apr 9, 2012
Loading...

Share This Page