cocos2d-x的CCAffineTransform相關變換實現原理

來源:互聯網
上載者:User

標籤:

  稍有opengl或3d基礎的都知道平移/旋轉/縮放這幾個基本模型視圖變換的實現原理, 最近看了下cocos2d-x相關部分的實現, 瞭解了這些實現那些各種座標變換基本不在話下了, cocos2d-x本身還是相對簡單的引擎.

1. CCAffineTransform

struct CCAffineTransform {    float a, b, c, d;    float tx, ty;};

表示變換矩陣:

構造CCAffineTransform結構

CCAffineTransform __CCAffineTransformMake(float a, float b, float c, float d, float tx, float ty){  CCAffineTransform t;  t.a = a; t.b = b; t.c = c; t.d = d; t.tx = tx; t.ty = ty;  return t;}

 

2. 單位矩陣

CCAffineTransform CCAffineTransformMakeIdentity(){    return __CCAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);}

將CCAffineTransform構造為單位矩陣:

 

3. 平移

CCAffineTransform CCAffineTransformTranslate(const CCAffineTransform& t, float tx, float 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);}

將CCAffineTransform矩陣和平移矩陣的結果:

 

4. 旋轉

CCAffineTransform CCAffineTransformRotate(const CCAffineTransform& t, float anAngle){    float fSin = sin(anAngle);    float fCos = cos(anAngle);    return __CCAffineTransformMake(    t.a * fCos + t.c * fSin,                                    t.b * fCos + t.d * fSin,                                    t.c * fCos - t.a * fSin,                                    t.d * fCos - t.b * fSin,                                    t.tx,                                    t.ty);}

繞Z軸旋轉矩陣右乘以變換矩陣:

 

5. 縮放

CCAffineTransform CCAffineTransformScale(const CCAffineTransform& t, float sx, float 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(const CCAffineTransform& t1, const CCAffineTransform& t2){    return __CCAffineTransformMake(    t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b                                    t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d                                    t1.tx * t2.a + t1.ty * t2.c + t2.tx,                  //tx                                    t1.tx * t2.b + t1.ty * t2.d + t2.ty);                  //ty}

結果相當於t2 . t1

 

7. CCPointApplyAffineTransform

CCPoint __CCPointApplyAffineTransform(const CCPoint& point, const CCAffineTransform& t){  CCPoint p;  p.x = (float)((double)t.a * point.x + (double)t.c * point.y + t.tx);  p.y = (float)((double)t.b * point.x + (double)t.d * point.y + t.ty);  return p;}

 

8. CCAffineTransformInvert

CCAffineTransform CCAffineTransformInvert(const CCAffineTransform& t){    float determinant = 1 / (t.a * t.d - t.b * t.c);    return __CCAffineTransformMake(determinant * t.d, -determinant * t.b, -determinant * t.c, determinant * t.a,                            determinant * (t.c * t.ty - t.d * t.tx), determinant * (t.b * t.tx - t.a * t.ty) );}

求矩陣的逆矩陣,通過Mathematica計算得:

 

cocos2d-x的CCAffineTransform相關變換實現原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.