Cocos2d-x tutorial (30)-Use of the 1.x physical Engine

Source: Internet
Author: User

Cocos2d-x tutorial (30)-Use of the 1.x physical Engine

 

 

 

In Cocos2d-x 2.x, developers can directly use Box2d or chipmunk to simulate the physical world.

We have also introduced the use of the Box2d physical engine in 17th tutorials (http://blog.csdn.net/u012945598/article/details/17787243), but the APIs in Box2d are still very complex for developers who are new to the physical engine.

At Cocos2d-x 3. in Version x, the Cocos2d-x encapsulates the two physical engines, so that they are integrated into 2d-x, avoiding developers directly using the more complex interface of the physical engine, it makes the use of physical engines very simple.

 

In 2.x, it takes the following steps to simulate a physical world and make the genie affected by physical effects:
(1) create a physical world.

(2) create an object (Body ).

(3) create a Fixture/framework (Fixture ).

(4) create a joint (Joints ).

(5) The fourth part can be selected as needed, and finally set user interaction.

In this way, a physical world can be simulated in version 2.x. Collision detection can be achieved by inheriting b2ContactListener and rewriting the BeginContact function.

 

In version 3.x, since the api has been encapsulated into the engine components, it does not need to be so troublesome to use, but it still takes several steps to use the physical engine.

(1) Use the factory method createWithPhysics () provided by the Scene class to create a scenario with a physical world.

(2) create an object using the PhysicsBody class, and bind the fixture and shape of the object.

(3) Use the setPhysicsBody () method of the Node class to bind the Node to the object, that is, set user interaction (Sprite, ImageView, and so on all belong to the Node derived class. You can access this method ).

(4) If necessary, use the PhysicsJointLimit class to create a joint, connect the object, and add the joint to the world.

(5) The collision detection event is monitored by EventListenerPhysicsContact. After creating an instance object, set the corresponding callback function.

 

Although the steps do not seem to reduce, the complexity of using physical engine code in version 3.x is much smaller than that in version 2.x.

Below is a simple Demo to simulate this process.

 

First, create a scenario that supports the physical world.

 

 



 

Create a border below to prevent the rigid body on the screen from being affected by gravity. By default, EdgeBox is not subject to gravity images. in the physical world, If you want objects not to be subject to images, you can use setGravityEnable (false) method implementation.

 

 


Next, create two more objects in the physical world and connect them with the joints.

At this time, you can see the effect of running the project, and finally add collision detection events.


Run the project and you can see:


 

In the above process, the collision bodies we use are all rule shapes. However, when performing precise collision detection, most of the images we encounter are irregular images, the following describes how to create an irregular object. A tool called VextexHelper needs to be used here

(: Http://download.csdn.net/detail/u012945598/7725475)

Download and decompress the package. This tool is actually a project under Xcode. Open the project file and run it as follows:

 

The green border is the edge drawn by the author, and then the program will generate an array based on each vertex. What we really need is the coordinates in the red rectangle. Of course, this format is somewhat different from our actual format. In fact, this format can be modified in the code of this tool, converts the output type to Point.

Copy the data to our project to use:

 

 


 

The running effect is as follows:

 

Finally, although Cocos encapsulates the physical engine, there are many points to note when using it.

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.