[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re: Hexagonal collision detection?



If your hexagons are individual sprites you could use mask-based collision detection. E.g. have your hexagonal button on a rectangular sprite with the surrounding parts transparent using colour-key transparency. The transparency is then used as the mask for pygame.sprite.spritecollide(), using pygame.sprite.collide_mask() as the comparison.

The trick is to draw a small "invisible" 1x1 px sprite at the position of the mouse cursor before doing a quick rect-based collision detection against the list of tiles. This will (usually) give you a small list of tiles against which you can do a second collision detection with the collide_mask method. That ought to return only one sprite which will equate to the button you're clicking on. If you get more than one thing back then just select the first one (e.g. the highest, or nearest to the "camera").

If your button isn't a separate sprite then this won't work ofc.


On Tue, Sep 15, 2009 at 10:24 AM, rygoody <rygoody@...> wrote:
Hello, I need to make a clickable hexagonal button. The clickable area
needs to actually be hexagonal, it's an essential part of the gameplay
dynamic. So I can't just put a hexagon picture over a square button.
The clickable button itself needs to be a hexagon.

I was just gonna use a linear equation to define the diagonal sides of
the hexagon, then go through a for loop to test on each line if the
click was in or out of the hexagon. But this seems so very inefficient
to do in python.

So I just thought I'd ask, is there any class, or any functionality in
anything that would be more ideal for something like this?