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

Re: [pygame] AI Module (was: Perlin Noise Function)





On Wed, Jul 30, 2008 at 6:51 PM, Hugo Arts <hugo.yoshi@xxxxxxxxx> wrote:
> a function to find what is close to something (This is very doable)

def close(something, other_things, distance):
   r = something.rect.inflate(distance, distance)
   return filter(lambda x: something.colliderect(x.rect), other_things)

There you go, that's the first one. ;-)
There is some talk on the list of integrating networking already, so
this is a good candidate for future inclusion.

That works unless you game is about space and which star out of 30,000 is close. Then the idea of finding the distance to everything gets a bit slow. To solve this you can take the position and divide by some distance that you think of as close in the X,Y,Z.  and take the integer quotient. If it is 0 then the object is is your close box.

1,1,1
5,5,5
20,20,20

close is a cube 10,10,10 so you get
0,0,0
0,0,0
2,2,2

the first 2 are close.

You can do this for are large bunch of objects and keep the result so that you only need to check small subsets of the whole and it is pre-calculated. If only a few are moving it is a quick check to see if they moved to a new cell.

--
Douglas E Knapp

http://sf-journey-creations.wikispot.org/Front_Page