Page 1 of 1

[pymunk] Bounding Box Queries?

Posted: Thu Jul 14, 2011 4:36 pm
by xzzy
In the main chipmunk documentation there's some discussion about using a bounding box query to get all shapes inside the box.

http://code.google.com/p/chipmunk-physics/wiki/Queries

I just so happen have a need to do exactly this, but I am using pymunk, and it appears the cpSpaceBBQuery function has not been implemented. Grepping the module's files I see some mentions of the function, but it doesn't seem to have been made a member of the Space class.

I am not really familiar with how C bindings work with Python, is there some other way to access this function I am missing, or is it not possible?

Re: [pymunk] Bounding Box Queries?

Posted: Mon Jul 18, 2011 10:03 am
by xzzy
Eventually figured this out on my own. Didn't really plan to dig into python modules, but at least I can say I'm a little more educated now.

In case google searches bring someone here in the future. It goes a little something like:

Code: Select all

import pymunk._chipmunk as cp

def bbq(space, bb, layers = -1, groups = 0):
     self.__query_hits = []
     def cf(_shape,data):
          shape = space._shapes[_shape.contents.hashid]
          self.__query_hits.append(shape)

     callback = cp.cpSpaceBBQueryFunc(cf)
     cp.cpSpaceBBQuery(space._space, bb._bb, layers, groups, callback, None)

     return self.__query_hits

bb = pymunk.BB(left, bottom, right, top)
results = bbq(space,bb)
Returns a list of shapes that were inside the bounding box.

I more or less cut and pasted this out of the pymunk source, and shuffled arguments around.

Re: [pymunk] Bounding Box Queries?

Posted: Fri Jul 22, 2011 6:02 pm
by viblo
Nice to hear that you solved it!

I have added the missing bb query method (and shape query) to pymunk trunk, expect it to be included from start in the next release.