def add_collisionpair_func(self, a, b, func, data=None): """Register func to be called when a collision is found between shapes with collision_type fields that match a and b. Pass None as func to reject any collision with the given collision type pair. func(shapeA, shapeB, contacts, normal_coef, data) -> bool Parameters shapeA : `Shape` The first shape shapeB : `Shape` The second shape contacts : [`Contact`] A list of contacts normal_coef : float The normal coefficient data : any The data argument sent to the add_collisionpair_func function WARNING: It is not safe for collision pair functions to remove or free shapes or bodies from a space. Doing so will likely end in a segfault as an earlier collision may already be referencing the shape or body. You must wait until after the step() function returns. """ if func is None: cp.cpSpaceAddCollisionPairFunc(self._space, a, b, ct.cast(ct.POINTER(ct.c_int)(), cp.cpCollFunc), None) else: f = self._get_cf(func, data) self._callbacks[(a, b)] = f cp.cpSpaceAddCollisionPairFunc(self._space, a, b, f, None)
def add_collisionpair_func(self, a, b, func, data): """Register func to be called when a collision is found between a shapes with collision_type fields that match a and b. Pass None as func to reject any collision with the given collision type pair. func(shapeA, shapeB, contacts, normal_coef, data) -> bool shapeA, shapeB is the colliding shapes contacts is a list of contacts normal_coef is a float :) data is the data argument sent to the addCollisionPairFunc function WARNING: It is not safe for collision pair functions to remove or free shapes or bodies from a space. Doing so will likely end in a segfault as an earlier collision may already be referencing the shape or body. You must wait until after the cpSpaceStep() function returns. """ def getCF(func): def CF(cpShapeA, cpShapeB, cpContacts, numContacts, normal_coef, _data): ### Translate chipmunk shapes to Shapes. if cpShapeA.contents.id in self._shapes: shapeA = self._shapes[cpShapeA.contents.id] else: shapeA = self._static_shapes[cpShapeA.contents.id] if cpShapeB.contents.id in self._shapes: shapeB = self._shapes[cpShapeB.contents.id] else: shapeB = self._static_shapes[cpShapeB.contents.id] return func( shapeA, shapeB, [Contact(cpContacts[i]) for i in xrange(numContacts)], normal_coef, data) return CF if func is None: cp.cpSpaceAddCollisionPairFunc( self._space, a, b, ct.cast(ct.POINTER(ct.c_int)(), cp.cpCollFunc), None) else: f = cp.cpCollFunc(getCF(func)) self._callbacks[(a, b)] = f cp.cpSpaceAddCollisionPairFunc(self._space, a, b, f, None)
def add_collisionpair_func(self, a, b, func, data): """Register func to be called when a collision is found between a shapes with collision_type fields that match a and b. Pass None as func to reject any collision with the given collision type pair. func(shapeA, shapeB, contacts, normal_coef, data) -> bool shapeA, shapeB is the colliding shapes contacts is a list of contacts normal_coef is a float :) data is the data argument sent to the addCollisionPairFunc function WARNING: It is not safe for collision pair functions to remove or free shapes or bodies from a space. Doing so will likely end in a segfault as an earlier collision may already be referencing the shape or body. You must wait until after the cpSpaceStep() function returns. """ def getCF(func): def CF (cpShapeA, cpShapeB, cpContacts, numContacts, normal_coef, _data): ### Translate chipmunk shapes to Shapes. if cpShapeA.contents.id in self._shapes: shapeA = self._shapes[cpShapeA.contents.id] else: shapeA = self._static_shapes[cpShapeA.contents.id] if cpShapeB.contents.id in self._shapes: shapeB = self._shapes[cpShapeB.contents.id] else: shapeB = self._static_shapes[cpShapeB.contents.id] return func(shapeA, shapeB, [Contact(cpContacts[i]) for i in xrange(numContacts)], normal_coef, data) return CF if func is None: cp.cpSpaceAddCollisionPairFunc(self._space, a, b, ct.cast(ct.POINTER(ct.c_int)(), cp.cpCollFunc), None) else: f = cp.cpCollFunc(getCF(func)) self._callbacks[(a,b)] = f cp.cpSpaceAddCollisionPairFunc(self._space, a, b, f, None)