Collision of Two Rect
MRAB
python at mrabarnett.plus.com
Fri May 3 20:51:01 EDT 2013
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)
More information about the Python-list
mailing list