Saturday, January 24, 2015

[Tutorial] Cocos2d-x: Create physics body of the complex objects by using Physical Editor software

We know how to create physics in Cocos2d-x V3.0, but in the case that is a circle, so creating physics body is rather easy. if our object has complex boundary, how do we create?

This lesson has two parts:
+ Create PhysicsBody Polygons from images by Box2d-Editor software
+ Import into game and make Physics of Engine process through analysis functions.

Part 1: Create PhysicsBody Polygons

1/ First, download PhysicBody Editor, here PhysicsBody Editor, version 2.9.2
Extract and run file physics-body-editor.jar. You must install JDK to run it.

2/ You dowload image file here

View video here

But in video clip, if you don't use feature: Auto- Trace, you can choose by hand, but it takes more time

Tasks in this part as follow:
+ Study a bit about how to analyze .JSON file
+ Create physics body from JSON and import into game.

Now let's start!

First, download sourc file here, you create yourself JSON file as in previous lesson. Here only upload class file.

Step 1 - Analyze file .JSON

Open file MyBodyParser.h , here declare a class MyBodyParser with some essential classes to analyze file JSON, basically functions that read string, i.e.
bool parseJsonFile(const std::string& pFile); // read information from pFile

bool parse(unsigned char* buffer, long length); // read information into a buffer with length

And here is the most important function

PhysicsBody* bodyFormJson(Node* pNode, const std::string& name); // Create a physic body and attach to some Node ( usually Sprite).

MyBodyParser.cpp will define the above functions. 

Step 2 - How to import into Game

Open file HelloWorleScene.h and view it
+ Here declare some functions to catch Touch events
+ Declare a pointer - Sprite*, a pointer - LabelTTF*
+ Declare a function - nodeUnderTouch() to specify the location of Node when touching on the screen

Open file HelloWorleScene.cpp
+ Inlcude part has #include "MyBodyParser.h"
+ Notice from this part
sp_2dx = Sprite::create("2dx.png"); // Add an image Sprite
Then is the familiar command - setPosition

And we should focus the following important command block:

//Import file bodies.JSON to analyze

// Create a physic body by command bodyFormJson of MyBodyParser class. This function takes two params: 1 is the above sprite, 2 is the name of project created by Physicbody Editor ( Remeber when we use software, there is a project named "2dx")

auto _body = MyBodyParser::getInstance()->bodyFormJson(sp_2dx, "2dx");
// Check the availability
    if (_body != nullptr) {
        _body->setDynamic(false); // Set static body, when object is collided, it will stand.
        _body->setCollisionBitmask(0x000000); // Set no collision with other objects
        sp_2dx->setPhysicsBody(_body); // Place body into sprite

// Place Sprite into layer of Scene
this->addChild(sp_2dx, 0);

The below command block is to create the Listener to listen to touchscreen event.

Now we know how to set a physic body from a JSON file into Sprite object, it's simple! ( If not consider MyBodyParser class). Now, we come to advanced to check whether physic body is fit with the edge of sprite or not.

+ View the function - Node* HelloWorld::nodeUnderTouch(cocos2d::Touch *touch)

   Node* node = nullptr;
    auto location = this->convertTouchToNodeSpace(touch); // Get the location
    auto scene = Director::getInstance()->getRunningScene(); // Get the running Scene
    auto arr = scene->getPhysicsWorld()->getShapes(location); // Return an array <PhysicsShape*> of the physical block shapes with location

    for (auto& obj : arr) // The loop that check in that array has Node whether is Sprite sp_2dx or not
        // If any
        if ( obj->getBody()->getNode() == sp_2dx)
            node = obj->getBody()->getNode(); // Save node
            break; // End the loop if seen
    return node; // Return the value that is node for function

+ View the function onTouchBegan()

auto current_node = nodeUnderTouch(touch); // Call function nodeUnderTouch to return a node

    // If that node is sp_2dx or not sp_2dx,  it will appear as follow:
    if (current_node == sp_2dx)
        status_label->setColor(Color3B::GREEN); // đặt màu xanh
        status_label->setString("Ohoo, DONT TOUCH ME!");
        status_label->setString("Haha, OUT OF ME!");

    return true; // must return true

And the function onTouchEnded, onTouchMoved are simple. You can youself study.

Now compile and run

Besides the way how to import from JSON to create physics body for complex objects, there is also another way that is to import from PLIST file created by PHYSICS EDITOR or Cocos Studio software

See you later!

[ translated]


Post a Comment

Choose an Android item