Cocos2d-x tutorial (17)-Box2D physical Engine

Source: Internet
Author: User

Welcome to the Cocos2d-x exchange group: 193411763 reprint, please note the original address: http://blog.csdn.net/u012945598/article/details/17787243

The physical engine is not a necessity for every game, but it can better simulate the reality of the real world.

This article will explain how to use Box2D.

It is not easy to master Box2D, but it is not difficult to use Box2D. First, we need to understand several common concepts:

1. All physical effects of the world b2World must be simulated in the physical world. The physical world is a large collection that can accommodate all rigid bodies, jigs, and shapes.

2. The b2Body of an object is the rigid body in our physics. Once created, the form will not change.

3. Shape b2Shape is attached to the jigs and has attributes such as friction and restoration. It is mainly used for collision detection.

4. the clamp b2Fixture is a relationship that attaches a shape to an object. By using the shape, you can create a fixture and add the fixture to the object, so that the object has the collision ability.

5. Joint b2Join can fix multiple objects together and limit an activity range. common attributes include Limits and Motors)


Note:

1. the physical world is based on Newton's law and independent from the normal rendering of the world. The two worlds are responsible for game screen rendering, the other is responsible for physical simulation, and there is data interaction between them.

2. do not try to change the state of an object in the issue of collision detection. We can save the object to be deleted in a container and process the unnecessary data asynchronously after the driving method is executed.

3. The optimal object size in the Box2D world is 0.1 ~ 10 objects in length, so we should try to set the object size within this range. We should not directly use the pixel value, but a conversion rate. In the file, we declare a macro definition "# define PTM_RATIO 32" in pixels/meters.


Steps for using the physical engine:

1. initialize a physical world
2. Create an genie
3. Set jigs for the genie
4. Set the body for the genie

Then this genie will be able to be affected by the physical effect.

Create a project and a new class named Box2dHandler. We will manage all the content related to the physical engine in this class.

We are used to creating a derived class of the genie class as the genie object in the physical world. Although this step is not required, you will find that such a class exists, it is much easier to process data later. The header file is introduced in the Box2dHandler. h file below.

# Include "cocos2d. h"

# Include "Box2D. h"

USING_NS_CC;

# Define PTM_RATIO 32

Add the following code:


When we create an genie in the physical world, we can use the B2Sprite class to create it. The premise is that you want this object to be affected by the physical effect. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 5E/Lqsy/nT0M/gudi1xM7vwO2y2df3oaM8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.bkjia.com/uploadfile/Collfiles/20140103/20140103134353167.jpg" alt = "\">

Note line 2, we created a GLESDebugDraw object, the class encapsulation is located in the "GLES-Render.h" and "GLES-Render.cpp" files, these two files need to be manually introduced, the file directory is: cocos2d-x-2.2.1/samples/Cpp/TestCpp/Classes/Box2DTestBed

We will implement these methods one by one:











In this way, all the basic methods have been completed. Of course, the function body of the update function and the collision detection function varies according to the requirements. We have not added much content here, next let's take a look at how to use the Box2dHandler class and open HelloWorld in the project. cpp file, introduce the header file of the Box2dHandler class, and add the following code to the Init () method:


Since we set the y-axis gravity to-10 in the physical world, after running the project, we will see two more genie falling from the screen, one at the center of the screen, and the other at the top, the two genie are surrounded by Red translucent rectangles. When an object falls to the bottom, the console will output "begin Collision", and the rectangle will become gray, indicating that it is already in sleep state, it is activated when the physical effect is affected again.



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.