Cocos2dx3.2 Crazy RIS: A simple Convex Polygon Rigid Body
Now we need to consider how to add a rigid body to the blocks in the game.
For Tetris, we can know that there are the following types of squares:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1eLW1s7Ss8bWrs6qv + mjqgjsb2nro6nqztwvcd4kpgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141204/20141204085957118.png" alt = "\">
This is what I call line.
This is what I call bulge.
This is what I call positive_L.
This is called the anti-L (negative_L) type.
This is what I call positive_Z.
This is what I call the anti-Z (negative_Z) type.
In short, square is the main problem nowadays is to add a rigid body for each of them. At first, I directly used the static creation method createPolygon of PhysicsBody to create a polygon, but reported an error. Then I learned that only convex polygon can be created. Among the blocks above, only block and line are convex polygon, while others are concave polygon. (The definition of a convex polygon can be Baidu. Simply put, there is no inner angle that exceeds 180 degrees)
Therefore, we must try to piece together each square with a convex polygon. My solution is to use a shape to form this PhysicsBody, and each shape is like a small square in it. As follows (the idea of customizing the block-type BaseBlock ):
First, declare the variable in the header file:
Private: int shapeAmount; // Number of shapes contained in an object std: vector
* ShapeVecs; // shape vertex set std: vector
* ShapeVecAmount; // Number of vertices in each shape cocos2d: Color4B color; // graphic color
The meanings of these variables are described in the annotations. Among them, the vertex set of shape is a vector composed of Vec2 (two-dimensional vector, which can be regarded as a point structure), because Vec2 is actually a simple array pointer of points, therefore, a corresponding vector is defined. To store the number of points in each shape. (Considering that the number of vertices may be cropped later, there are not necessarily four vertices)
Then, the function that declares and defines the initialization block is as follows:
void initForm(std::vector
* shapeVecs, std::vector
* shapeVecAmount, int shapeAmount, cocos2d::Color4B color);
In the function, as long:
auto body = PhysicsBody::create();for(int i=0; i
at(i), shapeVecAmount->at(i));shape->setRestitution(0.5);body->addShape(shape);}this->setPhysicsBody(body);
You can.
Finally, when calling this function in GameView, the vertex data of the square is required (in this case, the center point of the graph is (0, 0 )):
// Add the BaseBlock * GameView: addCubeBlock () {Vec2 * form1 = new Vec2 [4]; Vec2 * form2 = new Vec2 [4]; vec2 * form3 = new Vec2 [4]; Vec2 * form4 = new Vec2 [4]; std: vector
* Vec = new std: vector
[4]; std: vector
* VecNumber = new std: vector
[4]; form1 [0]. setPoint (-250000f,-250000f); form1 [1]. setPoint (-25366f, 0.0f); form1 [2]. setPoint (0.0f, 0.0f); form1 [3]. setPoint (0.0f,-25366f); form2 [0]. setPoint (-25366f,-0.0f); form2 [1]. setPoint (-250000f, 250000f); form2 [2]. setPoint (0.0f, 25366f); form2 [3]. setPoint (0.0f, 0.0f); form3 [0]. setPoint (0.0f, 0.0f); form3 [1]. setPoint (0.0f, 25366f); form3 [2]. setPoint (25366f, 25366f); form3 [3]. setPoint (25366f, 0.0f); form4 [0]. setPoint (0.0f,-25366f); form4 [1]. setPoint (0.0f, 0.0f); form4 [2]. setPoint (25366f, 0.0f); form4 [3]. setPoint (25366f,-25366f); vec-> push_back (form1); vec-> push_back (form2); vec-> push_back (form3); vec-> push_back (form4 ); vecNumber-> push_back (4); vecNumber-> push_back (4); vecNumber-> push_back (4); vecNumber-> push_back (4); auto sprite = BaseBlock :: create ("Img/block/cube.png"); sprite-> initForm (vec, vecNumber, 4); sprite-> setTag (1); sprite-> setVertexRect (Rect (0, 0, 50, 50); // this-> addChild (sprite); return sprite ;}
More comprehensive methods can be used to store data in resource files and then encapsulate the loading blocks in a unified manner.
Enable PhysicsWorld: DEBUGDRAW_ALL test mode. The running effect is as follows:
I am using Cocos2dx3.2 to create directories for other game-related blogs to create the gravity version of Tetris (Crazy RIS)