pygame: Pixel perfect collision against lines?

1.9k views Asked by At

I have one or two issues questions. I am using Pygame to create a drawing game.

We have balls bouncing around inside on a Surface, against each other and the walls. With the mouse you can draw straight lines that the balls bounce also bounce against.

The Balls are drawn using pygame.image, since we have a ball.png covering the Surface and Rect of the balls. But for the lines we are simply using pygame.draw.line() which returns simple rectangles.

And my current biggest problem:

I have opted to draw each line on its own Surface object and then blit these onto the bigger 'game canvas'. This is because I wanted to use the pixel perfect collision detection provided here, as well as surfarray.array_colorkey(), and for that I need an underlying Surface (the balls have the image Surface returned by pygame.image). I want a Surface for the lines so that I can use the transparency aspect of the collision algorithm, otherwise the balls will bounce off the Rects wrapping the lines.

Q1) Is there no better way to draw lines? I would like to use something similar to image or Sprite. Is there such a thing?

Q2) If no, how should I create the Surface for each line so that drawing the line onto the smaller Surface using the coordinates from the 'game canvas' is relatively easy, the line is not cut off and the pixel perfect collision described above works?

Q3) If im completely off/there are waaaay better ways to do collisions, detection, drawing, etc etc. Please, I am all ears.

1

There are 1 answers

0
ninMonkey On

Per-pixel collision for line on circle is overkill.

Info on 2d collision of circle and line: 2D collision between a moving circle and a fixed line segment

And, euclid has Circle.intersect( Line2 ) collision: http://partiallydisassembled.net/euclid/point2.html#SECTION002530000000000000000

numpy may have a better solution.