Cocos2d-x tutorial (30)-3. X version number physical engine usage

Source: Internet
Author: User

Welcome to Cocos2d-x chat group: 193411763

Reprinted please indicate the original source: http://blog.csdn.net/u012945598/article/details/38417333



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

We have also previously 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 just new to the physical engine.

In the Cocos2d-x 3.0 version, the Cocos2d-x encapsulates the physical engine, so that it is integrated into 2d-x, to avoid developers directly use the more complex interface of the physical engine, it makes the use of the physical engine very simple (currently, version 3.0 encapsulates the chipmunk physical engine, which is less functional than box2d, but is easy to use ).


In 2.x, to simulate a physical world and make the genie affected by physical effects, take the following steps:
(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, the 2.x version number can simulate a physical world. Collision detection can be implemented 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 requires 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 at the same time.

(3) Use the setphysicsbody () method of the node class to bind the node to the object, that is, set user interaction (sprite, imageview, etc. all belong to the node derived class. You can refer to this method ).

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

(5) The collision detection event is monitored by eventlistenerphysicscontact. You can set the corresponding callback function after creating an instance object.


Although the steps do not seem to decrease, 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 from exiting the screen. By default, edgebox is not subject to gravity images (static rigid bodies ), you can use setgravityenable (false) to remove an object from an image in the physical world.



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

At this time, the project is actually able to see the effect, and finally join? Collision detection events.


In this case, the project can be viewed as follows:



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 example shows how to create an irregular object. A tool called vextexhelper needs to be used here

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

After downloading and decompressing, this tool is actually a project under xcode. You can open the project file and execute it, for example:



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 changed in the code of this tool, converts the output type to point.

Copy the data to our project to use:



The execution result is as follows:



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

Cocos2d-x tutorial (30)-3. X version number physical engine usage

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.