Using the box2d physical engine for cocos2dx learning ------ creating a dynamic object

Source: Internet
Author: User

1. Create a physical world

First introduce a header file # include "box2d \ box2d. H"

Then create an object using b2word and specify the acceleration direction in the physical world.

WORD = new b2world (b2vec2 (0,-10); // specifies the acceleration of the physical world

At last, we need to rewrite the UPDATE function. This function has already been mentioned during timer learning. Every frame change will automatically execute this function. So we need to use this function to refresh the created physical world.

2. Create a moving object

We will implement a function to add an object in the physical world and call createbody to create an object. This function accepts a parameter. This parameter is the basic information about the created object, such as location, dynamic or static.

b2BodyDef def;def.position = b2Vec2(10,10);def.type = b2_dynamicBody;word->CreateBody(&def);

Then, in order to display the motion of the created object, we can use a loop in update to facilitate the list of objects in the whole physical world, find the Defined Object and output its coordinate changes in the physical world.

void HelloWorld::update(float dt){word->Step(dt, 8, 3);for(b2Body *b = word->GetBodyList(); b; b = b->GetNext()){if(b->GetType() == b2_dynamicBody){log("x:%f, y:%f", b->GetPosition().x, b->GetPosition().y);}}}

Finally, use scheduleupdate () in the init function to start update.

The change of coordinates may not be obvious, so this time we add a graph.

First, you must first create a Sprite.

Then, bind the created object to the sprite.

b2Body *b =  word->CreateBody(&def);auto sprite = Sprite::create();addChild(sprite);sprite->setTextureRect(Rect(0,0,80,80));b->SetUserData(sprite);

In the physical world of box2d, its position is measured by meters rather than pixels. Box2d has a precise simulation range of 10 m. Therefore, we need to map the units of pixels and meters to find the corresponding proportional relationship to determine the location to be specified for the created object.

Therefore, in the appdelegate: applicationdidfinishlaunching () function, we will set the program resolution glview-> setdesignresolutionsize (800,600, resolutionpolicy: show_all); set a resolution of 800*600, that is to say, 800 of the vertical axis corresponds to 10 m in the physical world, and their proportion is 80.

That is to say, the above operation has formed such an effect, turning the entire screen into a resolution of 800*600. in the physical world, the maximum ordinate range is 10. scale down the range of coordinate systems.

The position of the created object in the physical world can be specified through def. Position = b2vec2 (3, 5);. Def is an object of the object type information. However, if you want to connect a graph, the graph is not in the physical world. It is something in cocos2dx and a part of the game engine. Therefore, you need to set the sprite position through the ratio just now.

S-> setposition (B-> getposition (). x * ratio, B-> getposition (). y * ratio );

S is a Sprite pointer.

Although the above work binds the physical world objects to the graphics, it is only a static thing. The created object falls due to gravity in the physical world, so the bound image must be adjusted accordingly. Therefore, the position of the image must be updated in real time in the update function.

void HelloWorld::update(float dt){word->Step(dt, 8, 3);Sprite *s;for(b2Body *b = word->GetBodyList(); b; b = b->GetNext()){if(b->GetType() == b2_dynamicBody){if(b->GetUserData()){s = (Sprite*)b->GetUserData();s->setPosition(b->GetPosition().x * RATIO, b->GetPosition().y*RATIO);}}}}

Therefore, in the update function, you must check whether the body object is bound to a graph, obtain the bound graph, and reset the current coordinate of the graph.

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: 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.