Recently, there was a small demand to achieve the results of the Fruit Ninja blade. I found a lot of instance code on the internet, checked a lot of information, and finally found it.
As follows:
Figure 1:
Figure 2:
The algorithm is relatively simple. Let's talk about the process first:
The collection track points mainly use cctouchesbegan and cctouchesmoved in cocos2dx.
Cctouchesbegan contains the function to refresh the latest trajectory point.
Cctouchesmoved contains the function of adding trajectory points.
In this case, an array is required to record the ccpoint of the trajectory. I name it mpointarray to store these trajectory points. Each time you press your finger, add the position of the touch point as the start of the next computing rendering, and set it
The last digit of mpointarray. Then, get the relevant vertex through cctouchesmoved and add it to mpointarray. Generally, 16 points are enough.
[Track point calculation] This is the core part of the algorithm (compare with the relevant DEMO code and find a similar practice, that is, to build a polygon). In fact, the Trajectory follows this shape:
Here, the Triangle ABC (f) is not necessary because it is used as the head of the track and serves as a modifier. The focus is the Quadrilateral composed of A, C, D, E, F, and G. According to the OpenGL vertex rendering rules, we need four triangles, that is, 12 vertices, therefore, we can split these vertices. Note that this polygon is symmetric, so the algorithm is not too complex.
These vertices can be divided into an intermediate vertex array, a top vertex array, and a bottom vertex array. The core algorithm is as follows:
Ccpoint * P = mpointarray;
// An array of intermediate vertices
Ccpoint * centervertex = new ccpoint [mindex];
Memcpy (centervertex, P, sizeof (ccpoint) * mindex );
// Top vertex Array
Ccpoint pt1 = ccpsub (P [I], p [I-1]);
Float angle1 = ccptoangle (pt1 );
Topvertex [count1]. x = sinf (angle1) * w + P [I]. X;
Topvertex [count1]. Y = cosf (angle1) * w + P [I]. Y;
// Bottom vertex Array
Ccpoint pt2 = ccpsub (P [I], p [I-1]);
Float angle2 = ccptoangle (pt2 );
Bottomvertex [count2]. x = sinf (angle2) * w + P [I]. X;
Bottomvertex [count2]. Y = cosf (angle2) *-W + P [I]. Y;
Then, at the end of the algorithm, you need to arrange these vertices (used to render triangles) and fill in colors.
In addition, we need to know that the order of OpenGL rendered vertices is counter-clockwise and the clockwise order is eliminated.
Rendering is very simple. It is a fixed syntax, so put it with the source code.
Http://pan.baidu.com/s/1mg1F9lY
Password ozk7