[Cocos2d-x] physical engine entry, cocos2d-x physical Engine

Source: Internet
Author: User

[Cocos2d-x] physical engine entry, cocos2d-x physical Engine
Related Concepts


What is a physical engine?


Scientific model: the rational simplification of things in scientific research.

A physical engine is a computer program that simulates the Newton mechanics model and uses variables such as quality, speed, friction, and air resistance.
It can be used to predict the effect in different situations. It is mainly used in scientific simulation and electronic games.

Generally, the physical engine is only responsible for physical computing without rendering the image.


About box2d


Box2D is a free open-source two-dimensional physical engine written by Erin Catto using C ++. It has been used for development of games such as crayon physics, Angry Birds, and the border of hell.

The Cocos2d-x itself has been integrated with box2d.

The above explanation is from Wikipedia.


Box2D Manual: http://pan.baidu.com/s/1o63MI
Box2D source download: https://code.google.com/p/box2d/downloads/list

Box2D home: http://box2d.org/


Rigid body)

In box2d, an object is a rigid body.

A rigid body is a hard object and collision does not produce deformation.


There are three types of objects:
Static objects: collision will not move. The quality is 0. Such as borders and walls.
Dynamic Object: the collision will move.
Platform object: an object that keeps moving. For example, an elevator keeps moving up and down.


Shape)


Attached to the 2D collision ry of a rigid body, the shape has the properties of friction and restitution.


There are two main shapes in Box2D:
1. Circle
2. Polygon
The shape of a rigid body can be composed of multiple shapes. For example, a person has a Circular head, and his hands, feet, and body are rectangles.


Constraint (constraint)


A constraint is a physical connection to eliminate the degree of freedom of an object. In 2D, an object has three degrees of freedom. If we pin an object to a wall (like a pendulum), we constrain it to the wall, and this object can only rotate around this nail, therefore, this constraint eliminates two degrees of freedom.

Contact constraint)
A special constraint that prevents Rigid Body penetration and is used to simulate friction and restitution. You never have to create a contact constraint. They are automatically created by Box2D.


Joint (joint)


It is a constraint used to fix two or more objects together. Box2D supports the following joint types: rotation, prism, distance, and so on. Joints support limits and motors ).


Joint limit (joint limit)
A joint limit (joint limit) limits the motion range of a joint. For example, the human elbow can only be used for a certain range of operations.
.


Joint motor (joint motor)
A joint Trojan can drive the connected object according to the degree of freedom of the joint. For example, you can use a motor to drive an elbow
Rotate.


Fixture)

Encapsulation of physical information of a rigid body (such as type, density coefficient, and friction coefficient.

A rigid body can have multiple jigs.


Friction

Friction allows objects to slide along other objects realistically. Box2D supports Static and Dynamic Friction, but uses the same parameters. The friction is correctly simulated in Box2D, and the intensity of the friction is proportional to the orthogonal force (referred to as the colon friction. The friction parameter is often set between 0 and 1. 0 means no friction. 1 will produce strong friction.

Restore

Restoration can cause objects to pop up. Imagine dropping a ball above the desktop. The recovery value is usually set between 0 and 1. 0 means that the ball won't pop up, which is called non-elastic collision. 1 means that the speed of the ball will get a precise reflection, this is called a fully elastic collision.

Density

Box2D can calculate the object quality and the moment of inertia based on the mass distribution of the attached shape. Specifying the object quality directly often leads to uncoordinated simulation. Therefore, we recommend that you use b2Body: SetMassFromShape to set the quality based on the shape.


World)


The world is a closed polygon. In general, the size of the world is the same as that of the screen. Only objects in the world can be computed to improve efficiency.

A physical world is a collection of objects, shapes, and constraints.

The preceding overview is from the Box2D Chinese manual.


Use Box2D in Cocos2d-x


Description

Development Environment Description:

Engine version: Cocos2d-x2.2.1

Development Tool: VS2012

System: win7


Project Configuration

1. Enable Box2D: Right-click the project -- C/C ++ -- Preprocessor -- pre-processor definition -- edit -- add macro (CC_ENABLE_BOX2D_INTEGRATION)

Note: Box2D and Chipmunk are integrated in the Cocos2d-x, but these two physical engines cannot be used at the same time. Enable chipmunk macro definition (CC_ENABLE_CHIPMUNK_INTEGRATION)

2. Modify the pre-processing command CC_ENABLE_CHIPMUNK_INTEGRATION of the libExternsions project to CC_ENABLE_BOX2D_INTEGRATION.


3. Link to the box2d Library (libBox2d. lib)



A bug in Cocos2d-x2.2.6

When running Box2dTest in TestCpp, the following error is reported when several blocks are added.



Scaling Factor


In Box2D, the unit is meter, kg, and second. However, for Image Rendering, the unit is pixel.
For example, how do we render a 32-pixel-wide genie on the screen, but how do we express its width in physical space?
In this case, you need to define a scaling factor to convert the pixel value to the physical space value.
Setting the pixel/meter ratio to 32 is a suitable value. Therefore, the value of the scaling factor is defined as 32.

// Define the Scaling Factor # define PTM_RATIO 32


Create a physical world


During CCLayer initialization, the physical world is initialized.


Binding a rigid body to CCSprite


The Cocos2d-x has encapsulated a CCSprite for the physical engine, the CCPhysicsSprite, which is defined in the extension library. # Include "cocos-ext.h" can be used. Use the setB2Body method to associate with a rigid body.


Debug drawing


B2Draw defines interfaces used to draw related physical information. You only need to implement these interfaces. And call the SetDebugDraw method of world to associate with DebugDraw.


DebugDraw has been associated with implementation in the Cocos2d-x, cocos2d-x-2.2.1 and GLES-Render.cpp under the GLES-Render.h \ samples \ Cpp \ TestCpp \ Classes \ Box2DTestBed directory.


1. Copy GLES-Render.cpp and GLES-Render.h to the project Classes directory.

2. Include the header file.

#ifdef _DEBUG#include "GLES-Render.h"#endif

3. When creating a world, create DebugDraw, set the Debug tag, and associate it with the world. Rewrite the draw method of CCLayer to draw debugging information.

4. Release the memory occupied by debugDraw In the destructor.

Box2DTest::~Box2DTest(){CC_SAFE_DELETE(pWorld);CC_SAFE_DELETE(m_debugDraw);}




1. When an object is stressed (collision, extrusion, or fall), the color of the collision box will change to pink. The default value is blue.

2. the outermost side is the shape of the rigid body.


Note: It is recommended that you set the zoom factor value to 64 before debugging, so that it is easy to observe, because when the zoom factor is 32, the size of the collision box is the same as that of the sprite.


Example Project address: https://coding.net/u/linchaolong/p/Cocos2d-x_HelloBox2D/git "click to download source code]


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.