box2d collision filtering

hello forum

does anyone have experience with collision filters ?

I have a custom shape, than i place a bunch of boxes inside it.
the boxes accumulate and stay inside the custom shape.

but now i want to know when and which of the boxes are colliding.

i found this great example on the forum:±of006/1603/38

but it also registers the collision with my custom shape, which is my boundary. which i don’t want to know about.

i found some information on it here:

which describes the use of .categoryBits and .groupIndex, but i have no clue on how to implement this with OF.

some other code i came across:



hi stephan,

your in luck…
i had to work out filtering in box2d a few weeks back and have this example.


there are two sets of circles in the example (red and blue), all exist within the same box2d environment although their filter settings are set so that only blue collide with blue and only red collide with red.
i also had to create to set two sets of bounds (rectangles that stop the balls going off screen), not ideal but does the job for this example.

you could also figure out what two shapes are colliding and decide if to ignore that collision.
so in your case, check if one of those shapes is your boundary and if it is then ignore the collision.

i do this using a little method i put inside ofxBox2dBaseShape class,

bool isShape ( b2Shape *shape )  
	if( body == NULL )  
		return false;  
	for( b2Shape* s=body->GetShapeList(); s; s=s->GetNext() )  
		if( shape == s )  
			return true;  
	return false;  

Thanks for the post.
This is really good stuff to know.

Now I am wondering about intersection points.

Well here is what i am trying to do.
as you can see in the image on top of this thread i have an outer shape and want to fill it with rectangles.
but i have to make sure these rectangles don’t intersect each other. if some do i will reduce the number of rectangles.

all i need as a final result is the positions of all the rectangles. i don’t need an animation just a static image + values at the end.

so will the contact listener and filter helps , it does not help to find such perfect distribution.


Sorry to revive an old thread, but the zip file with the example is no longer available, could you upload it anywhere else?


Nevermind, solved it using b2FIlter.

For anyone interested in this, I have a custom class extending ofxBox2dCircle which i want to collide with the rest of the Box2d shapes but not between them. I added a collision filter in the set up like this

 void setup(b2World * b2dworld, float x, float y, float r, int l) {
        ofxBox2dCircle::setup(b2dworld, x, y, r);
        life = l;
        b2Filter f;
        f.categoryBits = 0x0002; // new category
        f.maskBits =0x0001;       // collide with default mask only
1 Like