Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.140 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.72; '*S*': 0.00; 'variables': 0.07; 'agree,': 0.09; 'methods,': 0.09; 'variables.': 0.09; 'posted': 0.15; '(x,': 0.16; 'ball.': 0.16; 'be:': 0.16; 'detected': 0.16; 'reversed': 0.16; 'subject:Two': 0.16; 'variables,': 0.16; 'accordingly.': 0.16; 'wrote:': 0.18; 'alex': 0.19; 'code,': 0.22; 'rid': 0.24; 'stick': 0.24; 'source': 0.25; 'updating': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'appear': 0.29; 'skip:p 30': 0.29; 'direction': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'code': 0.31; 'about.': 0.31; 'ball': 0.31; 'fri,': 0.33; 'skip:b 30': 0.33; 'screen': 0.34; 'could': 0.34; 'advice': 0.35; 'agree': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'doing': 0.36; 'next': 0.36; 'should': 0.36; 'two': 0.37; 'expressed': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'even': 0.60; 'worry': 0.60; 'simply': 0.61; "you're": 0.61; 'here:': 0.62; 'times': 0.62; 'more': 0.64; 'different': 0.65; 'kept': 0.65; 'movement': 0.65; 'reverse': 0.68; 'below:': 0.68; 'as:': 0.81; 'collision': 0.84; 'detecting': 0.84; 'agreement.': 0.91; 'bounce': 0.91; 'careful': 0.91; 'notion': 0.91; 'suspicious': 0.91; 'wanting': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=l4D3yXC4MkXvK7D97f0pk1s48jbJQ5qlgEqgR+Kplmw=; b=ijmzNNRVtZ78c7ipaswLjTHF53fWUDe/MZHqcDI5lFulIZOCe5rrydUgX32vWBIEg3 1Wr+mRXi9fyEvqMhvBzm2fM3ihZn+lSVpJCfXb60aLB4iDn56X2zckoHUh+LiE2ndqjv Uxcwf0OivM2GH28APVtm3XoavkRM1SDXSOl1/9lcoZkcTlxgkQtvNCr/S1RlikBo3fN4 SxH3dozzhWg4kJ0Mt2jx4QBIX9zgIDWqnpIbvIHAfPna6yPlF57YK9Nf0k1nIHJWpddp q461mZ6129+KzP0ug28kE493C+RNH57ej9Ms7p8wsfUfOVBNZppVF1gz1jr8Xatilzih E3wQ== X-Received: by 10.66.248.228 with SMTP id yp4mr16723986pac.158.1367624590662; Fri, 03 May 2013 16:43:10 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> References: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> From: Ian Kelly Date: Fri, 3 May 2013 17:42:30 -0600 Subject: Re: Collision of Two Rect To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367624595 news.xs4all.nl 15935 [2001:888:2000:d::a6]:36355 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44694 On Fri, May 3, 2013 at 4:23 PM, 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 What may be happening is that even though you reversed the direction of the ball's movement, in the next frame the paddle and ball are still detected as colliding, so they get reversed again. If that kept happening over and over again, then the ball would appear to be "sticking". You should check the current direction of movement after detecting the collision and only reverse it if it is not already going the proper direction. 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. You center the ball_rect from the x and y variables (somewhat awkwardly; see below), so at that point they would agree, but then you update the x and y variables without updating the rect accordingly. So before you get to the collision detection code, the position of the ball on screen and the one that is used for the collision detection may be somewhat different from the *actual* position of the ball. 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. By the way, this code: boundsball_rect = pygame.Rect(x,y,0,0) ball_rect.clamp_ip(boundsball_rect) could more simply be expressed as: ball_rect.center = (x, y) Because that's all you're doing here: updating the position of the rect. If you take my advice and do away with the x and y variables, then your position updating code: x += x_vel y += y_vel could simply be: ball_rect.move_ip(x_vel, y_vel)