Introduction to Box2D physical engine in Cocos2d-x: Using joints, cocos2d-xbox2d

Source: Internet
Author: User

Introduction to Box2D physical engine in Cocos2d-x: Using joints, cocos2d-xbox2d
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:

Void HelloWorld: addNewSpriteAtPosition (Vec2 p) {log ("Add sprite % 0.2f x % 02.f", p. x, p. y); // create the physical engine genie object Aauto spriteA = Sprite: create ("BoxA2.png"); ① spriteA-> setPosition (p); this-> addChild (spriteA ); // Dynamic Object A defines 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 the physical engine genie object Bauto spriteB = Sprite :: create ("BoxB2.png"); ③ spriteB-> setPosition (p + Vec2 (100,-100); ④ this-> addChild (spriteB ); // dynamic object B defines 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); ⑥ // defines the 2-meter box shape b2PolygonShape dynamicBox; dynamicBox. setAsBox (1, 1); // The Dynamic Object fixture defines b2FixtureDef fixtureDef; // sets the fixture shape fixtureDef. shape = & dynamicBox; // set the density fixtureDef. density = 1.0f; // sets the friction coefficient fixtureDef. friction = 0.3f; // use a fixture to fix the bodyA-> CreateFixture (& fixtureDef); 7bodyb-> CreateFixture (& fixtureDef ); distance // defines b2DistanceJointDef jointDef; distance jointDef. initialize (bodyA, bodyB, bodyA-> GetWorldCenter (), bodyB-> GetWorldCenter (); required jointDef. collideConnected = true; inclubodya-> GetWorld ()-> CreateJoint (& jointDef); success}


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 ~ The second line of code is to add the distance joint. The second line of code b2DistanceJointDef jointDef is to declare the distance joint definition, and the second line of code 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. The first line of code jointDef. collideConnected = true is to allow the collision of connected objects. The first line of 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 in Zhijie class: http: // v.51wOrk6.com
Welcome to Cocos2d-x Technology Discussion Group: 257760386

Welcome to Zhijie iOS public classroom Platform



How to Use Xcode tools to add Box2D physical engine to the cocos2d-x framework?

Can't find file error? I have been entangled in this error for a long time. All the settings in the Search Path are set. m files are all changed. mm, that is, the error is always reported. If you have searched for N posts, you can use this setting, but I have not successfully built it. Thank you very much for your help.
 
What is the difference between cocos2d and box2d?

Cocos2d and box2d are completely different from each other. cocos2d is an open-source framework that can be used to build a 2D environment, including CCScene, CCLayer, and CCSprite. It can be used to build a good 2d environment.
Box2d is a physical engine that simulates a real physical environment with the concept of gravity acceleration, friction, and physical fitness. In this environment, as long as the corresponding rigid body and gravity are defined, friction and other external environments, they can handle the collision themselves.
Box2d is only a physical engine, so it can be used in many places, such as flash, ios development, and window development. Cocos2d is written in Objective-c and can only be used in Apple development. If you want to use cocos2d in other environments, you need to use a cocos2d-x.
Box2d is only a physical engine and cannot be used to build a scenario. Therefore, in ios development, if you want to create a box2d project, you must also build the scenario. Therefore, the project also has a cocos2d library attached to you.

Related Article

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.