Introduction to box2d physical engine: Collision Detection in Cocos2d-x

Source: Internet
Author: User

In box2d, collision events are implemented by implementing b2contactlistener class functions. b2contactlistener is an abstract class provided by box2d, and its abstract function:
Virtual void begincontact (b2contact * contact ). The two objects will respond when they begin to contact each other, but they are called only once.
Virtual void endcontact (b2contact * contact ). Response when splitting. But it is called only once.
Virtual void presolve (b2contact * contact, const b2manifold * oldmanifold ). Response during continuous contact, which is called multiple times.
Virtual void postsolve (b2contact * contact, const b2contactimpulse * impulse ). Response during continuous contact, called after presolve is called.
The following describes how to detect collision in the box2d physical engine by restructuring the instance in section 12.2.3 using box2d technology.
First, we need to add a new class in the project. To add a new class to Visual Studio 2012, you must add the C ++ source file and header file respectively. For more information, right-click the classes folder under the hellobox2d project, right-click the folder, and choose "add"> "new project" from the shortcut menu ". The add new project dialog box is displayed, as shown in the following figure. In the dialog box, select the file type, enter the file name contactlistener in "name", and click "add" to add the file.

 

Add a new class in Visual Studio 2012

 

Add new project dialog box

After adding the new class contactlistener, we also need to modify its code. The contactlistener. h file code is as follows:

[HTML]View plaincopy
  1. # Include "cocos2d. H"
  2. # Include "box2d/box2d. H"
  3. Using_ns_cc;
  4. Class contactlistener: Public b2contactlistener
  5. {
  6. PRIVATE:
  7. // Two objects will respond when they begin to contact
  8. Virtual void begincontact (b2contact * contact );
  9. // Response during continuous contact
  10. Virtual void presolve (b2contact * contact, const b2manifold * oldmanifold );
  11. // Response during continuous contact, called after presolve is called
  12. Virtual void postsolve (b2contact * contact, const b2contactimpulse * impulse );
  13. // Response when splitting
  14. Virtual void endcontact (b2contact * contact );
  15. };
  16. The header files cocos2d. h and box2d/box2d. h must be introduced to the header files. Otherwise, compilation errors may occur. Contactlistener inherits b2contactlistener.
  17. The contactlistener. cpp file code is as follows:
  18. # Include "contactlistener. H"
  19. Void contactlistener: begincontact (b2contact * contact) ①
  20. {
  21. Log ("begincontact ");
  22. B2body * bodya = contact-> getfixturea ()-> getbody (); ②
  23. B2body * bodyb = contact-> getfixtureb ()-> getbody (); ③
  24. Auto spritea = (sprite *) bodya-> getuserdata (); ④
  25. Auto spriteb = (sprite *) bodyb-> getuserdata (); ⑤
  26. If (spritea! = Nullptr & spriteb! = Nullptr) ⑥
  27. {
  28. Spritea-> setcolor (color3b: yellow );
  29. Spriteb-> setcolor (color3b: yellow );
  30. }
  31. }
  32. Void contactlistener: endcontact (b2contact * contact) 7
  33. {
  34. Log ("endcontact ");
  35. B2body * bodya = contact-> getfixturea ()-> getbody ();
  36. B2body * bodyb = contact-> getfixtureb ()-> getbody ();
  37. Auto spritea = (sprite *) bodya-> getuserdata ();
  38. Auto spriteb = (sprite *) bodyb-> getuserdata ();
  39. If (spritea! = Nullptr & spriteb! = Nullptr)
  40. {
  41. Spritea-> setcolor (color3b: White );
  42. Spriteb-> setcolor (color3b: White );
  43. }
  44. }
  45. Void contactlistener: presolve (b2contact * contact, const b2manifold * oldmanifold) Listener
  46. {
  47. Log ("presolve ");
  48. }
  49. Void contactlistener: postsolve (b2contact * contact, const b2contactimpulse * impulse) Listener
  50. {
  51. Log ("postsolve ");
  52. }



The first line of the above Code implements the incontact function of the collision event. The second line of code and the third line of code are used to obtain objects that are in contact with both parties. Code ④ and line ⑤ determine the genie object from the userdata attribute of the object. The userdata attribute can be used to place any object. here we can use bodya-> getuserdata () when defining an object, you can use the body-> setuserdata (sprite) statement to put the sprite into the userdata attribute of the object. The sixth line of code is to determine whether two elves exist.
Line 7 of the Code implements the collision event endcontact function, which is similar to the begincontact function. The code in the second and second rows implements the collision event presolve and postsolve functions, which are usually not used much.
We also need to add the relevant collision detection code in the layer to listen to the event. The code in helloworld. H is as follows:

[HTML]View plaincopy
  1. # Ifndef _ helloworld_scene_h __
  2. # DEFINE _ helloworld_scene_h __
  3. # Include "cocos2d. H"
  4. # Include "box2d/box2d. H"
  5. # Include "contactlistener. H" ①
  6. # Define ptm_ratio 32
  7. Class helloworld: Public cocos2d: Layer
  8. {
  9. B2world * World;
  10. Contactlistener * contactlistener; ②
  11. Public:
  12. ~ Helloworld ();
  13. Static cocos2d: Scene * createscene ();
  14. Virtual bool Init ();
  15. Virtual void Update (float DT );
  16. Virtual bool ontouchbegan (cocos2d: Touch * touch, cocos2d: Event * event );
  17. Create_func (helloworld );
  18. Void initphysics ();
  19. Void addnewspriteatposition (cocos2d: vec2 P );
  20. };
  21. # Endif/_ helloworld_scene_h __



The first line of the above Code is to introduce the header file contactlistener. h. The Code in line ② declares the member variable contactlistener of the contactlistener type.
We also need to modify the helloworld: initphysics () code in helloworld. cpp as follows:

[HTML]View plaincopy
  1. Void helloworld: initphysics ()
  2. {
  3. ... ...
  4. Contactlistener = new contactlistener ();
  5. World-> setcontactlistener (contactlistener );
  6. ... ...
  7. }



The contactlistener = new contactlistener () Statement in the function is used to create a contactlistener object. The new keyword is used to allocate memory. To create a member variable contactlistener, you must release the contactlistener object by yourself. The release process is performed in the destructor. Its destructor code is as follows:
Helloworld ::~ Helloworld ()
{
Cc_safe_delete (World );
Cc_safe_delete (contactlistener );
}

Use cc_safe_delete (contactlistener) to safely release the memory of the contactlistener member variable.

 

 

More content please pay attention to the first domestic Cocos2d-x 3.2 version of the book "Cocos2d-x practice: C ++ volume" book exchange discussion site: http://www.cocoagame.net
For more exciting video courses, please follow the Cocos Course: http://v.51work6.com welcome to join the Cocos2d-x Technology Discussion Group: 257760386

 

 

 

 

 

 

Welcome to Zhijie IOS public classroom Platform

Introduction to box2d physical engine: Collision Detection in Cocos2d-x

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.