Slightly opengl or 3d basic know the translation/rotation/scaling of the Basic Model view transformation of the implementation principle, recently looked at the next cocos2d-x related parts of the implementation, to understand the implementation of those various coordinate transformations basically, cocos2d-x itself is relatively simple engine.
1. Ccaffinetransform
struct ccaffinetransform { float A, B, C, D; float tx, Ty;};
Represents a transformation matrix:
Construct Ccaffinetransform Structure
Ccaffinetransform __ccaffinetransformmake floatfloat float float float float ty) { ccaffinetransform t; = A; t.b = b; T.C = C; T.D = D; T.tx = TX; T.ty = ty; return t;}
2. Unit matrix
ccaffinetransform ccaffinetransformmakeidentity () { return __ccaffinetransformmake (1.0 0.00.01.00.00.0);}
Construct the Ccaffinetransform as a unit matrix:
3. Panning
Ccaffinetransform ccaffinetransformtranslate (constfloatfloat ty) { Return __ccaffinetransformmake (T.A, t.b, t.c, T.D, T.tx + t.a * tx + t.c * ty, T.ty + t.b * TX + T.D * ty);}
The result of the Ccaffinetransform matrix and the shift matrix:
4. Rotate
Ccaffinetransform ccaffinetransformrotate (constfloat anangle) { float fsin = sin (anangle); float fcos = cos (anangle); return __ccaffinetransformmake ( t.a * fcos + t.c * fsin, * fcos + t.d * fsin,
* fcos-t.a * fsin, * fcos-t.b * fsin, t.tx, t.ty);}
Right times the rotation matrix around the z axis multiplied by the transformation matrix:
5. Zooming
Ccaffinetransform Ccaffinetransformscale (constfloatfloat sy) { return __ccaffinetransformmake (T.A * SX, T.B * SX, T.C * sy, T.D * sy, T.TX, t.ty);}
6. Concate
/*concatenate ' T2 ' to ' T1 ' and return the result:t ' = T1 * T2*/ccaffinetransform Ccaffinetransformconcat (Constccaffinetransform& T1,Constccaffinetransform&T2) { return__ccaffinetransformmake (t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d,//A, bt1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d,//c,dT1.TX * t2.a + t1.ty * t2.c + T2.TX,//TXT1.TX * t2.b + t1.ty * t2.d + t2.ty);//Ty}
The result is equivalent to T2. T1
7. Ccpointapplyaffinetransform
Ccpoint __ccpointapplyaffinetransform (constconst ccaffinetransform& t) { ccpoint p; = (float) ((double) t.a * point.x + (double) t.c * point.y + t.tx); = (float) ((double) t.b * point.x + (double) t.d * point.y + t.ty); return p;}
8. Ccaffinetransforminvert
Ccaffinetransform Ccaffinetransforminvert (const ccaffinetransform& t) { float 1 /(T.A * t.d-t.b * t.c); return __ccaffinetransformmake (determinant * t.d,-determinant * t.b,-determinant * t.c, determinant * t.a,< c10/>* (T.C * T.TY-T.D * t.tx), determinant * (T.B * t.tx-t.a * t.ty));}
The inverse matrix of the matrix is computed by Mathematica:
The realization principle of ccaffinetransform correlation transformation of Cocos2d-x