About cocos2d-X 3.x using the engine's built-in physical engine physics

Source: Internet
Author: User

Cocos2d-x physics physical Engine

Today I was planning to use box2d physical engine, think of some time ago I heard that cocos2d-X in version 3.0 encapsulated a physical EnginePhysicsListening to the name is very domineering. It is actually much easier to use than box2d (after all, it is based on the box2d encapsulation). Well, now let's take a look at how to use the physics physical engine,



Step 1: Create a project...

Part 2: Modify the scenario Creation Method

Replace scene: Create (); with scene: createwithphysics ();

SCENE * helloworld: createscene () {// use the physical world to create a scenario. Auto scene = scene: createwithphysics (); // display the physical world debugging status. A red box is displayed, convenient debugging of scene-> getphysicsworld ()-> setdebugdrawmask (physicsworld: debugdraw_all); Auto layer = helloworld: Create (); scene-> addchild (layer); Return scene ;}


Part 3: create a world box in onenter ()

Void helloworld: onenter () {size visiblesize = Director: getinstance ()-> getvisiblesize (); // create a physical world. The size is the same as the screen size. Use the default material, the width of the debug box is three pixels: Auto Body = physicsbody: createedgebox (visiblesize, physicsbody_material_default, 3); // create a collision Image Auto edgeshape = node: Create (); // bind the image to the created world with edgeshape-> setphysicsbody (body); // set the position of the image in the middle of the screen edgeshape-> setposition (visiblesize. width/2, visiblesize. height/2); // Add the layer addchild (edgeshape );}


Let's see, a physical world of the same size as our screen is successfully created.



Part 4: Add a physical genie

Let's write a function.

Void helloworld: addsprite (cocos2d: vec2 POS) {Auto Body = sprite: Create ("closenormal.png "); // set a square to bind to the sprite body-> setphysicsbody (physicsbody: createbox (body-> getcontentsize (); Body-> setposition (POS ); addchild (body );}



We call this function in Init () and set this genie in the middle of the screen.

addSprite( Vec2( visibleSize.width / 2, visibleSize.height / 2));



Check whether something is falling, and the speed is getting faster and faster.



Yes, this image looks like a circle. Let's create a round collision image.

body->setPhysicsBody( PhysicsBody::createBox( body->getContentSize()));

Change

body->setPhysicsBody( PhysicsBody::createCircle( body->getContentSize().width / 2));

See if the image we created is a circle.





Well, it looks boring. Let's listen to another event and click it to make it more fun.

In Init (),

// Create a single touch event listener auto listener = eventlistenertouchonebyone: Create (); // the listener-> ontouchbegan = [this] (touch * t, event * E) {This-> addball (t-> getlocation (). x, t-> getlocation (). y); Return false ;}; // bind the listening object Director: getinstance ()-> geteventdispatcher ()-> addeventlistenerwithscenegraphpriority (listener, this );



Okay, you're done.



About cocos2d-X 3.x using the engine's built-in physical engine physics

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.