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
- Void helloworld: addnewspriteatposition (vec2 P)
- {
- Log ("add sprite % 0.2f X % 02.f", p. x, P. y );
- // Create a physical engine genie object
- Auto spritea = sprite: Create ("boxa2.png"); ①
- Spritea-> setposition (P );
- This-> addchild (spritea );
- // Dynamic object a definition
- B2bodydef bodydef;
- Bodydef. type = b2_dynamicbody;
- Bodydef. position. Set (p. x/ptm_ratio, P. Y/ptm_ratio );
- B2body * bodya = World-> createbody (& bodydef );
- Bodya-> setuserdata (spritea); ②
- // Create physical engine genie object B
- Auto spriteb = sprite: Create ("boxb2.png"); ③
- Spriteb-> setposition (p + vec2 (100,-100); ④
- This-> addchild (spriteb );
- // Dynamic object B definition
- Bodydef. type = b2_dynamicbody;
- Vec2 POSB = spriteb-> getposition ();
- Bodydef. position. Set (POSB. X/ptm_ratio, POSB. Y/ptm_ratio); ⑤
- B2body * bodyb = World-> createbody (& bodydef );
- Bodyb-> setuserdata (spriteb); ⑥
- // Define the shape of a 2-meter box
- B2polygonshape dynamicbox;
- Dynamicbox. setasbox (1, 1 );
- // Dynamic Object fixture Definition
- B2fixturedef fixturedef;
- // Set the shape of the fixture
- Fixturedef. Shape = & dynamicbox;
- // Set the density
- Fixturedef. density = 1.0f;
- // Set the friction coefficient
- Fixturedef. Friction = 0.3f;
- // Use the jigs to fix the shape of the object
- Bodya-> createfixture (& fixturedef); 7
- Bodyb-> createfixture (& fixturedef); then
- // Distance joint definition
- B2distancejointdef jointdef; required
- Jointdef. initialize (bodya, bodyb, bodya-> getworldcenter (),
- Bodyb-> getworldcenter (); Centers
- Jointdef. collideconnected = true ;?
- Bodya-> getworld ()-> createjoint (& jointdef );?
- }
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