[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Collision Detection



Thanx a lot for your response.

I understand that I need a seperate 'Hitbox' rectangle to check collisions
with. I probably take only the size of the body of the tank to take away the
turret area.

I figure I also need to rotate this Hitbox allong with the sprite, but I'm
not really sure how to do this. Maybe I need a seperate sprite for the
turret, but the complexity frightens me a little. Maybe it's not that
difficult as I imagine now, I'll have to trie first now :)

thanks,

Guyon

----- Original Message -----
From: "Pete Shinners" <pete@shinners.org>
To: <pygame-users@seul.org>
Sent: Thursday, January 30, 2003 11:26 AM
Subject: Re: [pygame] Collision Detection


> gumuz@looze.net wrote:
> > I'm having a small arena where 4 or 5 'robottanks' are driving around.
The
> > sprite-rotations are generated for each tank upfront, before the game
> > starts. I attached the sprite, so yu can see what it looks likes.
Ofcourse
> > these rotated sprites are still rectangular images.
> >
> > I have no experience really in writing games and I don't have a clue how
> > to go about creating some descent collision detection for my little
battle
> > tanks :)
>
> looking at your tank sprite (nice) it looks like you will be fine doing
> straight 'rectangle' collisions. The Rect.colliderect() method is
> probably the easiest way to do this, it just tests if two Rect objects
> overlap.
>
> because the tanks will be rotating, it is better to keep a separate Rect
> in the tank object used for collisions. best to keep this Rect slightly
> smaller than the visible area of the tank. each time you update the tank
> position, you just center this collision Rect on the image Rect and you
> should be set to go.
>
> class Tank:
>     def __init__(self, ...):
>         ...
>         self.crashrect = Rect(0, 0, 20, 20)
>     def update(self, ...):
>         ...
>         self.crashrect.center = self.rect.center
>
> if all the objects in your game have this "crashrect" attribute, it
> should be easy to compare any two objects to see if they are 'touching'.
> different sized objects will likely need different sized crashrects.
> bullets would likely only be a couple pixels.
>
> that works good for 'projectile' style shooting. if you are going for
> something more like 'hitscan' shooting (as they are called in FPS) you
> will need to move into the realm of line intersections against the
> 'crashrect' attributes.
>
>
> ____________________________________
> pygame mailing list
> pygame-users@seul.org
> http://pygame.seul.org
>


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org