Example introduction Cocos2d-x physical engine: Using joints, cocos2d-x Engine

Source: Internet
Author: User

Example introduction Cocos2d-x physical engine: Using joints, cocos2d-x Engine

In the game, we can constrain the motion of two objects by joint. We use a distance joint instance to describe how to use the joint.

In the running scenario of this instance, when the scenario is started, the player can touch and click the screen. Each time a touch is made, two new genie will be generated at the touch point and nearby, their operation is free-body movement, and the distance between them is fixed. The figure below shows that the drawing and debugging masks are enabled. As you can see, the debugging masks not only display objects but also joints.

 

Use distance joint instance

 

 

Use a distance instance (draw a debugging mask)

In the code section, the HelloWorldScene. h file is very similar to the instance in the previous section. HelloWorldScene. the boundary statements used to create the physical world and the specified world in cpp are in the HelloWorld: createScene () and HelloWorld: init () functions. These two functions are similar to the previous example, the function code will not be explained here.

The code for using joints in HelloWorldScene. cpp is as follows:

bool HelloWorld::onTouchBegan(Touch* touch, Event* event){Vec2 location = touch->getLocation();addNewSpriteAtPosition(location);return false;} void HelloWorld::addNewSpriteAtPosition(Vec2 p){  Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto boxA = Sprite::create("BoxA2.png"); ①boxA->setPosition(origin+ p); ② auto boxABody = PhysicsBody::createBox(boxA->getContentSize()); ③boxA->setPhysicsBody(boxABody); ④addChild(boxA, 10, 100); ⑤ auto boxB = Sprite::create("BoxB2.png");boxB->setPosition(origin + p + Vec2(0, -120));auto boxBBody = PhysicsBody::createBox(boxB->getContentSize());boxB->setPhysicsBody(boxBBody);addChild(boxB, 20, 101); auto world = this->getScene()->getPhysicsWorld(); ⑥ PhysicsJointDistance* joint = PhysicsJointDistance::construct(boxABody, boxBBody, Vec2(0, 0), Vec2(0, boxB->getContentSize().width / 2));  ⑦ world->addJoint(joint); ⑧ }

The onTouchBegan function in the code above is a touch response function, which calls the addNewSpriteAtPosition function in onTouchBegan. Create two genie in addNewSpriteAtPosition, create two objects, and set joint constraints between them. Line ① Of the Code creates the genie boxA and line ② sets its location. Line ③ code PhysicsBody: createBox (boxA-> getContentSize () is used to create a Rectangular Box object. Line ③ boxA-> setPhysicsBody (boxABody) is used to set object objects related to the genie. The fifth line is to add the genie to the current layer.

After boxA and boxABody are created, the following create boxB and boxBBody objects. After they are created, you can add joint constraints. Line 6 code: auto world = this-> getScene ()-> getPhysicsWorld () is to obtain the physical world (PhysicsWorld) from the scenario) object. Line 7 uses the static function construct of PhysicsJointDistance to create a distance joint object. The coordinate of the anchor uses the model coordinate (local coordinate). If the obtained coordinate is not the model coordinate, the coordinate transformation can be performed. PhysicsBody provides two coordinate functions:

Vec2 world2Local (const Vec2 & point ). The world coordinates are the model coordinates.

Vec2 local2World (const Vec2 & point ). The model coordinates are the world coordinates.

The last line of code world-> addJoint (joint) is to add the creation joint to the physical world.



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




Chipmunk physical engine an example of an object following the mouse movement, preferably under the cocos2d-x-js

I have never studied this physical engine, but I guess it may be that you have a problem calling a function when writing it. According to what you said, It is visually tested whether you have called the function by clicking the left mouse button, didn't I call the left-mouse lift function? If you only call the mouse-pressed function, the physical operation is triggered when you click an object. I'm not sure. I have never used it.

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.
 

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.