Box2D collision filtering rules

Source: Internet
Author: User

First, pay attention to this function:
[Cpp]
Virtual bool ShouldCollide (b2Fixture * fixtureA, b2Fixture * fixtureB );

Function implementation is in b2WorldCallbacks. cpp line: 24
[Cpp]
Bool b2ContactFilter: ShouldCollide (b2Fixture * fixtureA, b2Fixture * fixtureB)
{
Const b2Filter & Filiter = fixtureA-> GetFilterData ();
Const b2Filter & filterB = fixtureB-> GetFilterData ();
 
If (filelist. groupIndex = filterB. groupIndex & filelist. groupIndex! = 0)
{
Return filelist. groupIndex> 0;
}
 
Bool collide = (filelist. maskBits & filterB. categoryBits )! = 0 & (filelist. categoryBits & filterB. maskBits )! = 0;
Return collide;
}

This is the default collision filtering callback function.
From the implementation of the function, we can see that when filelist. groupIndex = filterB. groupIndex, if the groupIndex is greater than 0, the two fixture will collide. If the groupIndex is less than 0, the two will not collide.

If the groupIndex of the two fixture is not equal, then determine whether either of the two's maskBits contains the categoryBits of the other, that is, the "& Operation" in the Code is not zero -- If yes, returns true; otherwise, returns false.

We can see that:
1. The default collision tag priority is: groupIndex is higher than maskBits + categoryBits
2. (FileMaker. maskBits & filterB. categoryBits )! = 0 & (filelist. categoryBits & filterB. maskBits )! = 0 indicates the maskBits and categoryBits of A and B. The function returns true only when each of them contains the other party. Example: If filelist. categoryBits = 1, filterB. categoryBits = 2, filelist. maskBits = 3, filterB. maskBits = 2, although (3 & 2 )! = 0, but (2 & 1) = 0, so AB will not collide. Assuming that A and B represent "one" rather than "one" fixture, we can determine that A and B collide with each other.

========================================================== ==============
So how can we use the custom ShouldCollide function?
A simple demonstration.
1. Define a subclass of b2ContactFilter:
[Cpp]
Class MyContactFilter: public b2ContactFilter {
Public:
Virtual bool ShouldCollide (b2Fixture * fixtureA, b2Fixture * fixtureB ){
Return true; // no matter who you are
}
};

2. Add:
 

[Cpp]
MyContactFilter * cf = new MyContactFilter;
_ World-> SetContactFilter (cf );

In this way, no matter how you set the fixture groupIndex, maskBits, and categoryBits, all the bodies will collide.
[Note] In box2d, b2World, b2ContactFilter, and b2ContactListener are a few classes that are not managed by the engine and need to be new by themselves. Remember to delete them in the destructor. If the custom contactFilter is not a member variable, you can release it in the Destructor as follows:
[Cpp]
Delete _ world-> GetContactManager (). m_contactFilter;

========================================================== ====================
Let's take a look at an example of an engine: CollisionFiltering. h In TestCpp-Box2DTestBed (function implementation is included in this header file)
Let's just look at the constants defined in the file:
[Cpp]
Const int16 k_smallGroup = 1;
Const int16 k_largeGroup =-1;
 
Const uint16 k_defaultCategory = 0x0001;
Const uint16 k_triangleCategory = 0x0002;
Const uint16 k_boxCategory = 0x0004;
Const uint16 k_circleCategory = 0x0008;
 
Const uint16 k_triangleMask = 0 xFFFF;
Const uint16 k_boxMask = 0 xFFFF ^ k_triangleCategory;
Const uint16 k_circleMask = 0 xFFFF;

Suppose we didn't overwrite ShouldCollide, we use the default function of the engine. It can be seen that smallGroup (small objects in the corresponding scenario) must collide, because k_smallGroup = 1 is greater than 0; k_largeGroup (large objects) must not collide with each other, because-1 <0.

For large and small objects
K_triangleMask = 0 xFFFF;
K_boxMask = 0 xFFFF ^ k_triangleCategory; // as a result, boxMask does not "include" triangleCategory.
K_circleMask = 0 xFFFF;

Therefore, the triangle does not conflict with the big block, and the big triangle does not conflict with the small block. Other large objects and small objects will collide.

{OVER }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.