Explanation of new physical engine of cocos2d-x3.0: setCategoryBitmask (), setContactTestBitmask (), setCollisionBitmask ()

Source: Internet
Author: User
Tags bitmask

Reprinted please indicate the source: Game Development Laboratory http://blog.csdn.net/u010019717/article/details/32942641

I encountered this problem when I was writing a game. The other content of the physical engine was quite understandable. I didn't find a detailed explanation of these three functions. I don't know how the game was made. Then I will not vomit. All the content below is my personal inference. I do not know whether it is correct. That's what I understand.


Let's take a look at the definitions of the three functions:

/**
* A mask that defines which categories this physics body belongs.
* Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. you define the mask values used in your game. in conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
* The default value is 0 xFFFFFFFF (all bits set ).
*/

/** Defines the mask category of the physical rigid body. In a single scenario, each physical rigid body can be assigned to 32 different categories (the int bitmask parameter is four bytes of the int type and 32 bits). Each pair should have a one-bit mask of 32 bits. The mask value that you define in your game. The attributes of collisionBitMask and contactTestBitMask are associated to define which physical rigid bodies interact with each other and when you receive notifications of these interactions. The default value is 0 xFFFFFFFF (all bits are set ).
*/
Inline void setCategoryBitmask (int bitmask) {_ categoryBitmask = bitmask ;}
Inline int getCategoryBitmask () const {return _ categoryBitmask ;}
/**
* A mask that defines which categories of bodies cause intersection configurations with this physics body.
* When two bodies share the same space, each body's category mask is tested against the other body's contact mask by creating a logical AND operation. if either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world's delegate. for best performance, only set bits in the contacts mask for interactions you are interested in.
* The default value is 0x00000000 (all bits cleared ).
*/

/**
A mask that defines the categories of rigid bodies that generate an intersection (interaction) notification with this physical rigid body. When two rigid bodies share the same space, the class mask of each rigid body is detected and tested against the contact mask of other rigid bodies by executing logic and operations. If any comparison result is in a non-zero value, a PhysicsContact object is created and passed to the physical world delegate. To achieve the best performance, set only the contact mask bits for the interaction you are interested in. The default value is 0x00000000 (all bits are cleared ).
*/
Inline void setContactTestBitmask (int bitmask) {_ contactTestBitmask = bitmask ;}
Inline int getContactTestBitmask () const {return _ contactTestBitmask ;}
/**
* A mask that defines which categories of physics bodies can collide with this physics body.
* When two physics bodies contact each other, a collision may occur. this body's collision mask is compared to the other body's category mask by grouping a logical AND operation. if the result is a non-zero value, then this body is affected by the collision. each body independently chooses whether it wants to be affected by the other body. for example, you might use this to avoid collision calculations that wocould make negligible changes to a body's velocity.
* The default value is 0 xFFFFFFFF (all bits set ).
*/

/**

A mask that defines the categories of physical rigid bodies that can collide with this physical rigid body. Two physical rigid bodies may conflict with each other. The collision mask of this rigid body is compared to the category mask of other rigid bodies by performing bitwise logic and operations. If the result is a non-zero value, the rigid body is collided. Each rigid body selects whether it is willing to be affected by other rigid bodies independently. For example, you may use this to avoid collision calculation so that changes in the speed of the rigid body can be ignored. The default value is 0 xFFFFFFFF (all bits ).
*/
Inline void setCollisionBitmask (int bitmask) {_ collisionBitmask = bitmask ;}
Inline int getCollisionBitmask () const {return _ collisionBitmask ;}


So what does each function mean? Take a look at the following example:

Box1-> getPhysicsBody ()-> setCategoryBitmask (0x01); // 0001
Box1-> getPhysicsBody ()-> setContactTestBitmask (0x04); // 0100
Box1-> getPhysicsBody ()-> setCollisionBitmask (0x03); // 0011

box2->getPhysicsBody()->setCategoryBitmask(0x02);    // 0010box2->getPhysicsBody()->setContactTestBitmask(0x08); // 1000box2->getPhysicsBody()->setCollisionBitmask(0x01);   // 0001box3->getPhysicsBody()->setCategoryBitmask(0x04);    // 0100box3->getPhysicsBody()->setContactTestBitmask(0x01); // 0001box3->getPhysicsBody()->setCollisionBitmask(0x06);   // 0110

Box1 and box2 collide
Box1 box3 won't
Box2 box3 neither

Why? Explanation:

Box1 category mask 00000000 00000000 00000000 00000001

Notification 00000000 00000000 00000000 00000100

Allow me to hit 00000000 00000000 00000000 00000011


Box2 category mask 00000000 00000000 00000000 00000010

Notification 00000000 00000000 00000000 00001000

Allow me to hit 00000000 00000000 00000000 00000001


Box3 category mask 00000000 00000000 00000000 00000100

Notification 00000000 00000000 00000000 00000001

Allow me to hit 00000000 00000000 00000000 00000110


Perform the following operations:

Box1 allows hit me 00000000 00000000 00000000 00000011 and box2 category mask for bitwise AND Computation

Box2 category mask 00000000 00000000 00000000 00000010

Result: 00000000 00000000 00000000 00000010 is not 0, and box1 hits box2.

Likewise:

Box2 allows hitting me 00000000 00000000 00000000 00000001 and box1 class mask for bitwise AND Computation

Box1 category mask 00000000 00000000 00000000 00000001

Result: 00000000 00000000 00000000 00000001 is not 0, and box2 hits box1.

They will be hit by each other.


Box2 allows hit me and box3 class mask bitwise AND operation is 0;

The bitwise and operation of the box3 class mask between me and box2 is 0;


The bitwise and operation of the box1 class mask between me and box3 is 0;

The bitwise and operation of the box3 class mask between me and box1 is 0;

Therefore:

Box1 and box2 collision
But the box1 box3 won't
The box2 box3 won't





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.