Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44697

Re: Collision of Two Rect

Date 2013-05-04 01:51 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Collision of Two Rect
References <9083ecbc-5666-41aa-aacb-3f08f062feed@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1281.1367628664.3114.python-list@python.org> (permalink)

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Collision of Two Rect Alex Gardner <agardner210@gmail.com> - 2013-05-03 15:23 -0700
  Re: Collision of Two Rect Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-03 17:42 -0600
  Re: Collision of Two Rect MRAB <python@mrabarnett.plus.com> - 2013-05-04 01:51 +0100
  Re: Collision of Two Rect Tim Roberts <timr@probo.com> - 2013-05-04 22:28 -0700
  Re: Collision of Two Rect Joshua Landau <joshua.landau.ws@gmail.com> - 2013-05-06 17:38 +0100
  Re: Collision of Two Rect Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-06 14:18 -0600

csiph-web