cocos2dx physics collision

https://www.cnblogs.com/JiaoQing/p/3906780.html

Four response functions

1 EventListenerPhysicsContact* evContact = EventListenerPhysicsContact::create(); // Create a collision event for the physics world 
2      evContact->onContactBegin = [](PhysicsContact& contact)
 3      {
 4          CCLOG( " Begin!\n " );
 5          return  true ;
 6      };
 7      evContact->onContactPreSolve = [](PhysicsContact& contact,PhysicsContactPreSolve& solve)
 8      {
 9          CCLOG( " PreSolve!\n " );
 10          return  true ;
 11     };
 12      evContact->onContactPostSolve = [](PhysicsContact& contact, const PhysicsContactPostSolve& solve)
 13      {
 14          CCLOG( " PostSolve!\n " );
 15      };
 16      evContact->onContactSeperate = [=](PhysicsContact& contact) // this The function is called after the two colliding rigid bodies are separated 
17      {
 18          CCLOG( " Seperate!\n " );
 19          auto bodyA = (Sprite*)(contact.getShapeA()->getBody()->getNode()); / / A 20 of the nodes corresponding to the two colliding rigid bodies
         auto bodyB = (Sprite*)(contact.getShapeB()->getBody()->getNode()); // B 21 of the corresponding nodes of the two colliding rigid bodies 
if (!bodyA||!bodyB) // It stands to reason After the collision occurs, there will not be a situation where a rigid body node does not exist, but the actual test found that bodyA or bodyB is NULL, so we make a judgment here to exclude the case where the node is empty. 22 return ;
 23 int tagA = bodyA-> getTag();
 24 int tagB = bodyB-> getTag();
 25 if (tagA == 3 )
 26         {
 27              bodyA->removeFromParentAndCleanup( true );
 28         }
 29 if (tagB == 3 )
         
                                                   30         {
31             bodyB->removeFromParentAndCleanup(true);
32         }
33     //    prop->setVisible(true);
34     };
35     _eventDispatcher->addEventListenerWithSceneGraphPriority(evContact,this);//注册碰撞事件

 

 

1: Contact test mask ContactTestBitmask (default: 0xFFFFFFFF)

2: Category mask CategoryBitmask (default: 0x00000000)

3: collision mask CollisionBitmask (default: 0xFFFFFFFF)

 

    Only the contact test masks of the two perform a "logical AND" operation. If the result is a non-zero value, it means that the two objects will trigger a collision detection event.

That is, the onContactBegin and onContactSeperate functions will be executed.

    Assuming there are three objects (body1, body2, body3), set the contact test mask as follows:

body1->setContactTestBitmask(0x01)  //0001

body2->setContactTestBitmask(0x03)  //0011

body3->setContactTestBitmask(0x02)  //0010

body1 and body2, as well as body2 and body3 can trigger collision detection events, which will execute the onContactBegin and onContactSeperate functions.

And body1 and body3 cannot, and the above four functions will not be executed.

 

   For class masks and collision masks, their role is to detect whether a "collision reaction" occurs when two objects touch. A "collision reaction" will appear as an object being hit by another

Objects collide and change the direction of motion. Since the two bodies are "rigid bodies", the two bodies will not intersect when colliding.

   Category mask: defines the category to which an object belongs, and each object can be assigned up to 32 different categories in the scene.

   Collision mask: When two objects are in contact, perform a "logical AND" operation between the collision mask of the object and the class mask of the other object. If the result is a non-zero value,

The object is able to react to collisions with another object. The onContactPostSolve and onContactSeperate functions will be executed (provided that the collision detection event will trigger,

That is, the ContactTestBitmask mentioned above is logically AND is not 0),

 

Tested:

1: The logical AND of ContactTestBitmask is not 0 The logical AND of ContactTestBitmask and ContactTestBitmask is not 0

      The above four functions will all trigger (provided that the first two functions return true), and will also generate a "collision reaction":

2: Logical AND of ContactTestBitmask: 0 Logical AND of ContactTestBitmask and ContactTestBitmask is not 0

      None of the above four functions will trigger and will produce a "collision response":     

3: The logical AND of ContactTestBitmask is not 0, the logical AND of ContactTestBitmask and ContactTestBitmask is 0

      The above four functions will be triggered (provided that the first two functions return true), and will not produce a "collision reaction" (that is, it will directly pass through, cross);

4: The logical AND of ContactTestBitmask is 0, and the logical AND of ContactTestBitmask and ContactTestBitmask is 0

      None of the above four functions will be triggered and will not produce a "collision response" (that is, it will directly pass through and cross);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324965213&siteId=291194637