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

Re: [pygame] Python and Speed



On Thu, Apr 17, 2008 at 11:59 AM, Ian Mallett <geometrian@xxxxxxxxx> wrote:
> This is precisely the problem I have run into in one of my in-dev
> games--iterating over large arrays once per frame.  Actually, it is
> basically a collision detection algorithm--I have two arrays, both
> containing 3D points.  The points in one array must be tested with the
> points in the other to see how close they are.  If they are close enough,
> there is a collision.  Naturally, this means that for every point in one
> array, the other array must be iterated through and the 3D pythagorean
> theorem performed to each tested point.

Even in a faster language, checking every point against every other
point is a recipe for disaster.  Various culling techniques exist to
eliminate the need to do this, and this can help your python program
as well.  Basically you subdivide your points into zones, and only
check points against other points in the same or adjacent zones.  As
long as all of the objects don't get lumped in the same zone, you will
have a much faster algorithm.  Also, if you are using sqrt for your
distance check, you are likely wasting cpu cycles, if all you need to
know is whether they are "close enough."

Numpy also may help in this situation.