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

[pygame] Re: Hexagonal collision detection?



This looks like a wonderful function, especially since it appears your
doing it by not traversing each pixel line. But I am having alot of
diffuculty understanding how your doing it.

I understand that, it takes a list of x,y points. It then goes through
reads these points into a another list of lines. this seems like the
left side and the right side of the object.

And then your somehow testing to see how far the click was from these
lines.

But this right here
test = (y - y0)*(x1 - x0) - (x - x0)*(y1 - y0)

How is that working to produce per pixel collison detection when the
for loop is only going through the Lines list which only has 5 entries
of 2 lists (for a pentagon)
I would expect you would have to go through each pixel line and use a
linear equation to interpolate between the points, but you appear not
to be doing that.

How is that working?

On Sep 15, 7:35 am, Ian Mallett <geometr...@xxxxxxxxx> wrote:
>  As used in my projecthttp://www.pygame.org/project/649/.
>     def pointtest(self,point):
>         #drawpoints is a list containing points defining your polygon
>         #point is the mouse position
>         #if it doesn't work, list them in opposite order.
>         #works for arbitrary convex geometry
>         x = point[0]
>         y = point[1]
>         Lines = []
>         index = 0
>         for index in xrange(len(drawpoints)):
>             p0 = drawpoints[index]
>             try: p1 = drawpoints[index+1]
>             except: p1 = drawpoints[0]
>             Lines.append([p0,p1])
>         for l in Lines:
>             p0 = l[0]
>             p1 = l[1]
>             x0 = p0[0]; y0 = p0[1]
>             x1 = p1[0]; y1 = p1[1]
>             test = (y - y0)*(x1 - x0) - (x - x0)*(y1 - y0)
>             if test < 0: return False
>         return True
> Ian