Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.051 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'root': 0.05; 'result,': 0.07; 'calculating': 0.09; 'line:': 0.09; 'methods,': 0.09; 'root,': 0.09; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'squared': 0.16; 'subject:Two': 0.16; 'wrote:': 0.18; 'alex': 0.19; 'header:User-Agent:1': 0.23; 'stick': 0.24; 'source': 0.25; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'skip:p 30': 0.29; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'getting': 0.31; 'comments,': 0.31; 'could': 0.34; 'but': 0.35; 'positive': 0.37; 'to:addr:python-list': 0.38; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'number,': 0.60; "you're": 0.61; 'here:': 0.62; 'different': 0.65; 'header:Reply-To:1': 0.67; 'below:': 0.68; 'reply-to:no real name:2**0': 0.71; 'apart': 0.72; 'square': 0.74; 'collision': 0.84; 'reply-to:addr:python.org': 0.84; 'bounce': 0.91; 'write:': 0.91; 'wanting': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=TZ6v63gh c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=7AxPfEIvyrUA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=RvTypnA-Qq0A:10 a=fGO4tVQLAAAA:8 a=03z2ofBG_3AEhOee_UkA:9 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Sat, 04 May 2013 01:51:01 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Collision of Two Rect References: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> In-Reply-To: <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367628664 news.xs4all.nl 15906 [2001:888:2000:d::a6]:59148 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44697 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)