Example introduction to box2d physical engine in Cocos2d-x: Using Joints

Source: Internet
Author: User

Next we will use box2d physical engine technology for refactoring. This allows you to understand how to use joint constraints in box2d.
The code for using joints in helloworldscene. cpp is as follows:

[HTML]View plaincopy
  1. Void helloworld: addnewspriteatposition (vec2 P)
  2. {
  3. Log ("add sprite % 0.2f X % 02.f", p. x, P. y );
  4. // Create a physical engine genie object
  5. Auto spritea = sprite: Create ("boxa2.png"); ①
  6. Spritea-> setposition (P );
  7. This-> addchild (spritea );
  8. // Dynamic object a definition
  9. B2bodydef bodydef;
  10. Bodydef. type = b2_dynamicbody;
  11. Bodydef. position. Set (p. x/ptm_ratio, P. Y/ptm_ratio );
  12. B2body * bodya = World-> createbody (& bodydef );
  13. Bodya-> setuserdata (spritea); ②
  14. // Create physical engine genie object B
  15. Auto spriteb = sprite: Create ("boxb2.png"); ③
  16. Spriteb-> setposition (p + vec2 (100,-100); ④
  17. This-> addchild (spriteb );
  18. // Dynamic object B definition
  19. Bodydef. type = b2_dynamicbody;
  20. Vec2 POSB = spriteb-> getposition ();
  21. Bodydef. position. Set (POSB. X/ptm_ratio, POSB. Y/ptm_ratio); ⑤
  22. B2body * bodyb = World-> createbody (& bodydef );
  23. Bodyb-> setuserdata (spriteb); ⑥
  24. // Define the shape of a 2-meter box
  25. B2polygonshape dynamicbox;
  26. Dynamicbox. setasbox (1, 1 );
  27. // Dynamic Object fixture Definition
  28. B2fixturedef fixturedef;
  29. // Set the shape of the fixture
  30. Fixturedef. Shape = & dynamicbox;
  31. // Set the density
  32. Fixturedef. density = 1.0f;
  33. // Set the friction coefficient
  34. Fixturedef. Friction = 0.3f;
  35. // Use the jigs to fix the shape of the object
  36. Bodya-> createfixture (& fixturedef); 7
  37. Bodyb-> createfixture (& fixturedef); then
  38. // Distance joint definition
  39. B2distancejointdef jointdef; required
  40. Jointdef. initialize (bodya, bodyb, bodya-> getworldcenter (),
  41. Bodyb-> getworldcenter (); Centers
  42. Jointdef. collideconnected = true ;?
  43. Bodya-> getworld ()-> createjoint (& jointdef );?
  44. }



Code ① ~ (2) rows are used to create object A and object. ③ ~ Row 6 is used to create sprite object B and object B. Line 4 of the Code sets the position of Sprite B, P is the position of the touch point, P + vec2 (100,-100) indicates the position at the bottom right of the touch point (100, -100 ). The fifth line of code is to set the position of object B, which is measured in meters. Therefore, divide the position of genie B by ptm_ratio.
Line 7 and the first line of code are fixed by a fixture (A 2-meter box shape) to object A and object B. Since the shapes are the same, use the same fixture to define fixturedef to fix the objects respectively.
Nth region ~? The line of code is to add the distance joint. The line of code b2distancejointdef In the first line is to declare the definition of the distance joint, and the line of code in the second line is jointdef. initialize (bodya, bodyb, bodya-> getworldcenter (), bodyb-> getworldcenter () is the definition of the initialization distance joint, the first and second parameters are the anchor points of object A and object B. The third and fourth parameters are the world coordinates of object A and object B. bodya-> getworldcenter () the function is used to obtain the world coordinates of the center a of an object. Number? Line Code jointdef. collideconnected = true is used to allow collision of connected objects. Number? Line Code bodya-> getworld ()-> createjoint (& jointdef) is used to create a joint based on jointdef (joint definition) through a physical world object.
In the above example, we only use the distance joint, while many joints are defined in box2d V2. These joints include:
? Distance joint. Keep a fixed Distance between two objects. Points on each object are called anchor points. The joint is defined as b2distancejointdef.
? Rotate the joint. Allows an object to rotate around a public point. The joint is defined as b2revolutejointdef.
? Translate the joint. The relative rotation between two objects is fixed, and they can be translated along one coordinate axis. The joint is defined as b2prismaticjointdef.
? Weld joints. You can fix an object in the same direction. The joint is defined as b2weldjointdef.
? Pulley joint. The pulley joint is used to create an ideal pulley. Two objects are located at both ends of the rope, and the rope connects two objects through a fixed point (the pulley position. In this way, when an object rises, the other object will fall. The length of the rope at both ends of the pulley remains unchanged. The joint is defined as b2pulleyjointdef.
? Friction joint. Reduces the relative motion between two objects. The joint definition is b2frictionjointdef.
? Gear joint. Control the other two joints (rotating joint or moving joint), one of which affects the other. The joint is defined as b2gearjointdef.
? Mouse joint. Click any point on the object to drag around the world. The joint is defined as b2mousejointdef.

These joint definitions have different parameters during initialization, but the parameters are similar. In addition to the joints listed above, there are also some other joints, which are not very common and will not be described here.

 

 

 

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

Example introduction to box2d physical engine in Cocos2d-x: Using Joints

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.