pradyunsg
pradyunsg

Reputation: 19406

python pygame physics rectange - circle collision

Again this question is on PyParticles4.
Link to last question for reference

Comment if unclear...

I am working on a Shooter game, much like this, but on a flat land with a wall that varies it's height on every turn(something for fun in the game) and with 2 players,each with a cannon that can move some distance (there's a limit, and they can't move beyond a certain amount from their start position) on each turn(the player decides if he wishes to move).

My code so far(for the Bullet and Shooter)

class Bullet(PyParticles.Particle):
    def hit(self,shooterlist):
        for shoot in shooterlist:
            #confusion
            dist = math.hypot(dx,dy)
    # other funcs to added
class Shooter:
    def __init__(self,pos,size):
        self.rect = pygame.Rect(pos,size)
    # other funcs to added

My Problems

Collision of the bullet with the Shooter. Any Ideas on how to know when the bullet collides with the rect?
I have been advised by someone to look at all the points on the edge of the rect, and see if it is within the circle but it seems to be very slow.
I think something faster would be better..
..
..
Update:
The circle can have a rect around it, which if collides with the rect, I now know when the rect is close to the circle, maybe even touching it.. How do i move forward??(Thx to PygameNerd)

Upvotes: 0

Views: 1205

Answers (3)

dank.game
dank.game

Reputation: 4719

You can run another test every time colliderect returns true. There are a few options for what that test can be. One is similar to the advice you already received but with the wall and circle switched. Check the points on the circumference of the circle to see if one of them collides with the wall using Rect.collidepoint. In pygame, you can get a list of circumference points by creating a Mask from the circle surface and running Mask.outline. You probably won't need every point, so you could get just every Nth point. In fact, you may only need the midpoints of the edges of the circle's rect, which you can get from the Rect object itself.

You're asking for precise collision, and that would give you pixel perfect collision detection between a circle and a rectangle. You may not need it in your game, though, so I suggest trying with just colliderect first. If you're interested in collision detection in pygame in general, there are a lot of options that are directly implemented in or based on functions described in the Mask and Sprite documentation.

Upvotes: 1

alexpinho98
alexpinho98

Reputation: 909

You can use Rect.collidepoint with the center of the circle but make rect bigger

collide_rect = Rect(x - p.radius,y - p.radius,w + 2 * p.radius,h + 2 * p.radius)
if collide_rect.collide_point(p.pos):
    # Collision Resolution

Upvotes: 0

ThisIsAQuestion
ThisIsAQuestion

Reputation: 1967

I am not sure what you mean by your question, but I thingk that you need the colliderect function.

rect.colliderect(rect): Return bool

Put this in the code somewhere, and if it returns true, have the ball explode.

Upvotes: 1

Related Questions