Original posts: http://blog.sina.com.cn/s/blog_6a2061a20100n0or.html
Collision screening is a system that prevents certain shapes from colliding. Set what objects collide with those objects and what objects do not collide according to specific requirements.
BOX2D supports collision filtering through population and group indexes.
The group index is simple, set its shapedef groupindex value, for example BoxDef.filter.groupIndex = 1.
Determining that all shapes of the same group always collide (positive) or never collide (negative) by the positive or negative of the Groupindex value, it is important to note that two different pay indexes are still colliding, such as a circle with a groupindex value of-1, A rectangle Groupindex value of-2. Because different groups of indexes are filtered by population and mask, that is to say, the group index has a higher preference.
box2d supports 16 species, so we can specify which species any shape belongs to, and we can also specify the shape and which other populations collide. This process is done by setting the Categorybits value of its shapedef with the maskbits value.
Categorybits is used to define the type of collision to which it belongs, and Maskbits is the specified collision type.
For example, if body1 's boxDef.filter.categoryBits = 0x0002,body2 's boxDef.filter.categoryBits = 0x0004, then if the other body wants to collide with both of them , then its boxDef.filter.maskBits = 0x0006;
Simply put, if a body collides with the body of other species, its maskbits value should be the sum of the categorybits of the body of the other population.
But at the same time is not so simple, if the three body of the categorybits is 0x0001,0x0002,0x0003, the other body maskbits value if it is 0x0003, Then it is with the categorybits value for 0x0001 and 0x0002 two body collision, or alone with the Categorybits value 0x0003 body collision, or with three body has collided.
How many species of a game are there in general?
Now let's do a test, specific requirements: four rigid bodies.
Rectangle: Collision only with itself, circle, triangle.
Circle: Collide only with itself, rectangle, Pentagon.
Triangle: Only with itself, the rectangle collide.
Pentagon: Only with itself, a circular collision.
Categorybits value: Rectangle [0x0001], Circle [0x0002], triangle [0x0003], Pentagon [0x0004].
Maskbits value: Rectangle [1+2+3=6=0x0006]
Round [1+2+4=7=0x0007]
Triangle [1+3=4=0x0004]
Pentagon [2+4=6=0x0006]
What will happen to this collision? The rectangle is the same as the Pentagon. 6 and 7 Less said there are two combinations, or directly look at the demo good.
Results:
Rectangle: Collision only with a circle. What's the relationship between 6 and 2?
Circle: Collide only with itself, rectangle, Pentagon. Meet the requirements. Praise one.
Triangle: Only collide with Pentagon. 4 and 4, is not a single than the combination of precedence.
Pentagon: Only with itself, circle, triangle collide. 6 = 2+3+4. I'll take a walk.
Let's take a look at the right thing first.
All the wrong is categorybits value some value is violated, the Categorybits value must be a multiple of 2.
The following 16 species are available:
0x0000 = 0
0x0001 = 1
0x0002 = 2
0x0004 = 4
0x0008 = 8
0x0010 = 16
0x0020 = 32
0x0040 = 64
0x0080 = 128
0X0100 = 256
0X0200 = 512
0x0400 = 1024
0x0800 = 2048
0x1000 = 4096
0x2000 = 8192
0x4000 = 16384
0x8000 = 32768
This way, 6 can only be paired with 2+4, 8 can only be paired with 8 themselves