Thoughts on Cocos2dx3.2 CrazyTetris single-line cropping for judgment and elimination (1)

Source: Internet
Author: User

Thoughts on Cocos2dx3.2 CrazyTetris single-line cropping for judgment and elimination (1)

Because it is not a rule of the Russian square, it is likely to generate irregular graphics during the elimination, so how to determine whether the condition is met, and square pruning will be a key issue for this game.

When I was playing this game, I used the most direct method, that is, the most stupid method, to directly determine the cropping. If you have a good algorithm, I hope you can communicate with me.

First, in the previous article, the rigid body model we created must be a convex polygon. Therefore, each initial square consists of four small square blocks.

 

In this game, the box rotation should be arbitrary (instead of 90 degrees for each rotation in the original game ). Therefore, when the square above is stopped, it is likely that the direction is as follows:


 

Therefore, the square may be eliminated as follows:


 

This requires cropping, and the internal rigid body or convex polygon must be satisfied after cropping. In fact, we only need to crop several small blocks separately. Obviously, after a convex polygon is cut in a straight line, the generated image is still a convex polygon.

 

The next step is how to make the cropping.

Although each elimination eliminates the middle of two straight lines, there is no essential difference between two straight lines and one straight line. Therefore, in this case, only single-line cropping is considered.

 

Now, we start to consider single-line cropping,


 

The cropping line and the image may overlap and do not want to be handed over. Even if the cropping line and the image are intersecting, they may only overlap some internal squares. For one of the squares, you must consider which side of the cropped line and the image are intersecting, therefore, crop the edge.

Because edges are cropped in the end, we do not consider the whole or the whole small square, but consider the several sides that constitute the small square.

Therefore, the following situations may occur: Blue points represent the starting point, and green points represent the ending point (an edge is a vector ):


 

In this case, the two points are on the top of the cropping line, and no cropping is required.


 

In this case, the two points are in the lower part of the cropping line, and no cropping is required.


 

In this case, the cropping line and the edge are intersecting. The two points are divided into the cropping line sides, but the starting point (blue point) is lower and the ending point (Green Point) is above.

In this case, you need to crop the image. The starting point (blue point) and the cropping point (the point where the cropping line and edge InterSect) become the side of the image below after the cropping is completed, the cropping point (the point at which the cropping line and edge InterSect) and the ending point (Green Point) are the edges of the image above after the cropping is completed.


 

In this case, the cropping line and the edge are intersecting. The two points are divided into the cropping line sides, but the starting point (blue point) is above and the ending point (Green Point) is below.

In this case, you need to crop the image. The starting point (blue point) and the cropping point (the point where the cropping line and edge InterSect) are the edges of the image above after the cropping is completed, the crop point (the point at which the cropping line and edge InterSect) and the ending point (Green Point) are the edges of the image below after the cropping is completed.

We do not need to calculate the intersection point one by one and then judge. Therefore, you can use the above method to determine the situation first, and then calculate and crop based on the situation.

The idea here is to generate the upper and lower figures after cropping. The upper part of the cropping line is 0, and the lower part is 1. Take the two points, the start point and the end point in the square clockwise. Determine the encoding value of two points based on the y value of two points.

If the encoding value is the same, it indicates that the two points are on the same side of the cropping line. At this time, the side must not overlap the cropping line. Therefore, no cropping is performed. However, you need to classify the two points into the upper or lower figures in order based on the encoding value.

If the encoding value is different, crop the image to obtain the cropping point. Then according to the starting point position, if the starting point is encoded as 0, in the upper part of the cropping line, the starting point and the cropping point are sequentially classified into the upper part of the graph, and the cropping point and the ending point are sequentially classified into the lower part of the graph; on the contrary, the starting point and the cropping point are sequentially classified into the lower graph, the cropping point and the ending point are sequentially classified into the upper graph.

The code implementation is as follows:

Vector
 
  
* BaseBlock: bottomLineCutting (float y) {// defines the upper and lower polygon set std: vector
  
   
* TopBlock; std: vector
   
    
* BottomBlock; std: vector
    
     
* TopVecsNumber; std: vector
     
      
* BottomVecsNumber; topBlock = new std: vector
      
        (); BottomBlock = new std: vector
       
         (); TopVecsNumber = new std: vector
        
          (); BottomVecsNumber = new std: vector
         
           (); Bool isCutting = false; for (int I = 0; I
          
            * TopShape; std: vector
           
             * BottomShape; topShape = new std: vector
            
              (); BottomShape = new std: vector
             
               (); // Crop side by side for (int j = 0; j
              
                At (I); j ++) {Vec2 startPoint = this-> coordinateSpin (shapeVecs-> at (I) [j]); vec2 endPoint = this-> coordinateSpin (shapeVecs-> at (I) [(j + 1) % shapeVecAmount-> at (I)]); int cStart = 0; int cEnd = 0; if (fabs (startPoint. y-y) <1e-6) & (fabs (endPoint. y-y) <1e-6) {if (topShape-> size ()! = 0) cStart = cEnd = 0; else cStart = cEnd = 1;} else if (fabs (startPoint. y-y) <1e-6) {if (endPoint. y-y <1e-6) cStart = cEnd = 1;} else if (fabs (endPoint. y-y) <1e-6) {if (startPoint. y-y <1e-6) cStart = cEnd = 1;} else {if (startPoint. y-y <1e-6) cStart = 1; if (endPoint. y-y <1e-6) cEnd = 1;} if (cStart = cEnd) {// The two vertices are on the same side, and you do not need to crop if (cStart = 0) {// records the top vertex set topShape-> push_back (coordinateGoBack (star TPoint); // topShape-> push_back (coordinateGoBack (endPoint);} else {// vertex bottom, record to bottom vertex set bottomShape-> push_back (coordinateGoBack (startPoint); // bottomShape-> push_back (coordinateGoBack (endPoint ));}} else {// The two vertices are on different edges and need to be cropped float cutting_x = startPoint. x + (endPoint. x-startPoint. x) * (y-startPoint. y)/(endPoint. y-startPoint. y); isCutting = true; if (cStart = 0) {// the start point is on the top, and the end point is topShape-> push_back (coordinateGoB Ack (startPoint); topShape-> push_back (coordinateGoBack (Vec2 (cutting_x, y); // bottomShape-> push_back (coordinateGoBack (endPoint )); bottomShape-> push_back (coordinateGoBack (Vec2 (cutting_x, y);} the starting point of else {// starting point is lower, and the ending point is bottomShape-> push_back (coordinateGoBack (startPoint )); bottomShape-> push_back (coordinateGoBack (Vec2 (cutting_x, y); // topShape-> push_back (coordinateGoBack (startPoint); topShape-> push_back (coordin AteGoBack (Vec2 (cutting_x, y) ;}}// add to cube set Vec2 * topTempShape = new Vec2 [topShape-> size ()]; vec2 * bottomTempShape = new Vec2 [bottomShape-> size ()]; for (int index = 0; index <topShape-> size (); index ++) {topTempShape [index] = topShape-> at (index) ;}for (int index = 0; index <bottomShape-> size (); index ++) {bottomTempShape [index] = bottomShape-> at (index);} if (topShape-> size ()! = 0) {topBlock-> push_back (topTempShape); topVecsNumber-> push_back (topShape-> size ();} if (bottomShape-> size ()! = 0) {bottomBlock-> push_back (bottomTempShape); bottomVecsNumber-> push_back (bottomShape-> size ();} if (isCutting) {Vector
               
                 * BaseBlockSet = new Vector
                
                  (); DrawNode * draw = DrawNode: create (); Texture2D * texture = this-> getTexture (); RenderTexture * texture1 = RenderTexture :: create (this-> getContentSize (). width, this-> getContentSize (). height); auto base1 = BaseBlock: createWithTexture (this-> getTexture (); auto base2 = BaseBlock: createWithTexture (this-> getTexture ()); base1-> initForm (topBlock, topVecsNumber, topBlock-> size (); base2-> initForm (bottomBlock , BottomVecsNumber, bottomBlock-> size (); base1-> setPosition (this-> getPosition (); base2-> setPosition (this-> getPosition ()); base1-> setRotation (this-> getRotation (); base2-> setRotation (this-> getRotation (); baseBlockSet-> pushBack (base1 ); baseBlockSet-> pushBack (base2); return baseBlockSet;} else {if (bottomBlock-> size ()! = 0) {Vector
                 
                   * BaseBlockSet = new Vector
                  
                    (); Auto base = BaseBlock: createWithTexture (this-> getTexture (); baseBlockSet-> pushBack (base); return baseBlockSet;} else {return NULL ;}}}
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

 

 

Related Article

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.