cocos2d-x的CCAffineTransform相关变换实现原理
稍有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 = / (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相关变换实现原理的更多相关文章
- 【Linux】iptables相关实践,原理及参数解释
1.禁止指定IP地址的主机进行连接 iptables -I INPUT -s .***.***. -j DROP 2.解除禁止指定IP地址的主机进行连接 iptables -D INPUT -s .* ...
- Cocos2d入门--1--初涉相关属性或代码
Cocos2d vision: cocos2d-x-3.8.1 万丈高楼,起于累土.对于一个游戏框架的学习,其实在于框架功能的使用积累,学会了如何在cocos2d游戏引擎的基础上使用它提供的各种功能 ...
- 微信测试帐号如何设置URL和Token,以及相关验证的原理
首先说明,本帮助文档是利用javaweb的Servlet来进行“接口配置信息配置信息”认证的. 在学习微信公众号开发的时候,读到填写服务器配置的帮助部分,总是不能理解为啥按照他的步骤做总是设置失败(吐 ...
- 双机相关知识(原理、LVM、Raid技术)
1 双机知识 1.1 预备知识 1.1.1 基本概念 双机热备:双机热备双机管理软件可以根据心跳自动检测环境运行情况,如果发现一个节点挂掉了,会自动切换到另外一个 ...
- EM算法和GMM算法的相关推导及原理
极大似然估计 我们先从极大似然估计说起,来考虑这样的一个问题,在给定的一组样本x1,x2······xn中,已知它们来自于高斯分布N(u, σ),那么我们来试试估计参数u,σ. 首先,对于参数估计的方 ...
- prometheus入门介绍及相关组件、原理讲解
1:介绍 Prometheus 是由 SoundCloud 开源监控告警解决方案. prometheus是由谷歌研发的一款开源的监控软件,目前已经被云计算本地基金会托管,是继k8s托管的第二个项目. ...
- 深度学习中常见的 Normlization 及权重初始化相关知识(原理及公式推导)
Batch Normlization(BN) 为什么要进行 BN 防止深度神经网络,每一层得参数更新会导致上层的输入数据发生变化,通过层层叠加,高层的输入分布变化会十分剧烈,这就使得高层需要不断去重新 ...
- OpenCV2马拉松第22圈——Hough变换直线检測原理与实现
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/27220445 收入囊中 Hough变换 概率Ho ...
- opencv —— HoughLines、HoughLinesP 霍夫线变换原理(标准霍夫线变换、多尺度霍夫线变换、累积概率霍夫线变换)及直线检测
霍夫线变换的原理 一条直线在图像二维空间可由两个变量表示,有以下两种情况: ① 在笛卡尔坐标系中:可由参数斜率和截距(k,b)表示. ② 在极坐标系中:可由参数极经和极角(r,θ)表示. 对于霍夫线变 ...
随机推荐
- joson返回数据库的时间格式在前台用js转换
function ChangeDateFormat(val) { if (val != null) { var date = new Date(parseInt(val.replace("/ ...
- 缓解 SQL Server has encountered 727 occurrence(s) of I/O requests taking longer than 15 seconds
sql server 会记录IO等待时间超过15 seconds的请求,这时application会有 time out 现象,dba需要判断是workload,concurrecy 所致还是sql ...
- 乐1S 5.8(Android 6.0) 刷第三方recovery并刷入root权限
说明 我的是 乐视1S, 系统为EUI 5.8 (powered by Android 6.0) 使用的是 superSu来root, 你可以到这里下载 文件 *下载 Recovery Flashab ...
- WPF中通过代码设置控件的坐标
用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...
- asp.net timer viewstate
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- java反射类内容获取
private void DtoReflect(Object obj, MqDto mqDto) throws Exception { Map map = getMap(mqDto); if(obj= ...
- AndroidManifest.xml详解(下)
本文编辑整理自:http://blog.163.com/hero_213/blog/static/39891214201242835410742/ 八.第三层<activity-alias> ...
- Other linker flags
-ObjC:加了这个参数后,链接器就会把静态库中所有的Objective-C类和分类都加载到最后的可执行文件中-all_load:会让链接器把所有找到的目标文件都加载到可执行文件中,但是千万不要随便使 ...
- DbnBase64加密处理
package com.dbn.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; impo ...
- CentOS 6.6安装配置LAMP服务器(Apache+PHP5+MySQL)
准备篇: CentOS 6.6系统安装配置图解教程 http://www.osyunwei.com/archives/8398.html 1.配置防火墙,开启80端口.3306端口 vi /etc/s ...