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

Re: [pygame] fast sqrt? and profiling



On Wed, Jan 21, 2009 at 12:26 PM, Emile Kroeger <flammifer@xxxxxxxxx> wrote:
> 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
>

My way is simple but all the points in boxes and only look at the
points in the 9 boxes of your area or 27 if we are talking 3d. If they
move put them in the right box using modulus math or was that just a
roll right a few times?? One never remembers such stuff.. :-P

-- 
Douglas E Knapp

Amazon Gift Cards; let them choose!!
http://www.amazon.com/gp/product/B001078FFE?ie=UTF8&tag=seattlebujinkand&linkCode=as2&camp=1789&creative=9325&creativeASIN=B001078FFE