Cocos2DX Physical Collision

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

Four response functions

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

Share a picture

1: Contact Test Bitmask (Default value: 0xFFFFFFFF)

2: category mask CategoryBitmask (default value: 0x00000000)

3: collision mask CollisionBitmask (default value: 0xFFFFFFFF)

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

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, Will execute the onContactBegin and onContactSeperate functions.

While body1 and body3 are not possible, the above four functions will not be executed.

For category masks and collision masks, their function is to detect whether a “collision reaction” occurs when two objects are in contact. The “collision reaction” will appear as an object being collided by another

object and changing the direction of movement. Since the two objects are “rigid bodies”, the two objects will not cross when they collide.

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

Collision mask: When two objects are in contact, change the collision mask of the object and the category mask of the other object to perform a “logical AND” operation. If the result is a non-zero value,

The object can react to the collision of another object. The onContactPostSolve and onContactSeperate functions will be executed (provided that the collision detection event will be triggered,

that is, the logical AND of the ContactTestBitmask mentioned above is non-zero),

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 be triggered (provided that the first two functions return true) , Will also produce a “collision response”:

2: ContactTestBitmask logical AND: 0 ContactTestBitmask and ContactTestBitmask logical AND is not 0

None of the above four functions will be triggered, and 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 all be triggered (provided that the first two functions return true) , There will be no “collision reaction” (that is, it will directly pass through and cross);

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

The above four functions Neither will be triggered, and there will be no “collision response” (that is, it will pass through and cross);

 1

 1

 1

 1

span> EventListenerPhysicsContact* evContact = EventListenerPhysicsContact::create();//Create a collision event in the physical world span>
2 evContact->onContactBegin = [](PhysicsContact& contact)< br /> 3 {
4 CCLOG("Begin! ");
5 < span style="color: #0000ff">return true;
6 };
7 evContact->onContactPreSolve = [](PhysicsContact& contact,PhysicsContactPreSolve& solve)
8< /span> {
9 CCLOG("PreSolve! "< /span>);
10 return true;
11 };
12 evContact->onContactPostSolve = [](PhysicsContact& contact,const PhysicsContactPostSolve& solve)
13 {
14 CCLOG("PostSolve! "< span style="color: #000000">);
15 };< br />16 evContact->onContactSeperate = [=](Phy sicsContact& contact)//This function is called after the two colliding rigid bodies are separated
17 {
18 span> CCLOG("Seperate! ");
19 auto bodyA = (Sprite*)( contact.getShapeA()->getBody()->getNode());//Two colliding rigid bodies Corresponding node A
20 auto bodyB = (Sprite*)(contact.getShapeB()->getBody()->getNode ());//B of the corresponding node of two colliding rigid bodies
< span style="color: #008080">21
if(!bodyA||!bodyB) //It stands to reason that a rigid body node does not exist after the collision, but in the actual test, it is found that bodyA or bodyB is NULL, so we make a judgment here to exclude the situation where the node is empty
22 return;< br />23 int tagA = bodyA->getTag();
24 int tagB = bodyB->getTag();
25 if(tagA == 3)
< span style="color: #008080">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);// Register for collision events

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3679 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.