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

Re: [pygame] fast sqrt? and profiling



You don't need to use sqrt in your collide_circle

If you use complex numbers to represent 2D positions (which simplifies
a lot of operations), you can do something like this:

def sqrlen(vector):
    "Returns the square of a vector (that's represented as a complex number)"
    return vector.real**2 + vector.imag**2

def collide_circle(loc1, loc2, rad1, rad2):
    return sqrlen(loc2-loc1) < (rad1+rad2)**2

(maybe Knapp is proposing something similar, I didn't read all his code :P)

Emile

On Wed, Jan 21, 2009 at 6:27 AM, Jake b <ninmonkeys@xxxxxxxxx> wrote:
> [...]
>
> 1) Is there a suggested fast sqrt module thats portable?
>
> [...]
> def calc_local(self):
>         """re-calc 'who are my neighbors' to cache for later."""
>         l = [] #first, get list of neighbors
>
>         for u in self.game.units.list_type(Unit):
>             if u == self: continue # don't collide self.
>             if collide_circle( self.loc, u.loc, self.rad+self.local, u.rad):
>                 l.append(u)
>         self.local_cache = l