惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
量子位
aimingoo的专栏
aimingoo的专栏
V
V2EX
Vercel News
Vercel News
B
Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
U
Unit 42
博客园 - 叶小钗
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
W
WeLiveSecurity
博客园 - Franky
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Schneier on Security
Schneier on Security
I
InfoQ
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
Last Week in AI
Last Week in AI
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
Lohrmann on Cybersecurity
SecWiki News
SecWiki News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
J
Java Code Geeks

博客园 - 里沃特

XR806开发板环境搭建记录 谈谈把网站迁移到阿里云的一些感想和其中遇到的一些问题 HTML5+JS 《五子飞》游戏实现(八)人机对战 HTML5+JS 《五子飞》游戏实现(七)游戏试玩 HTML5+JS 《五子飞》游戏实现(六)鼠标响应与多重选择 HTML5+JS 《五子飞》游戏实现(五)移动棋子 HTML5+JS 《五子飞》游戏实现(四)夹一个和挑一对 HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子 HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备 HTML5+JS 《五子飞》游戏实现(一)规则 cocos2dx-2.2.1 免 Cygwin 环境搭建(Win8+VS2013+ADT Bundle+android-ndk-r9c) 在VS2010 下编译 cocos2d-x-2.1.4 FFmpeg 1.2 for Android 生成一个动态库 FFmpeg 1.2 for Android 编译动态库 Linq 批量更新数据 C# 统一对 try...catch 的调用,方便保存错误日志。 手机:由全触摸屏失效所想到的 愚人节奉献给大家的礼物,敬请收下 C# 让控件全屏显示(WinForm)
深入理解 cocos2d-x 坐标系
里沃特 · 2013-08-31 · via 博客园 - 里沃特

首先对于初学的,带大家认识 cocos2d-x 中坐标系的几个概念,参考 http://blog.csdn.net/tskyfree/article/details/8292544。其他的往下看。

弄懂坐标系是开始开发的重要的一步,为了不让大家头晕,现在里沃特深入的为大家讲解一下,本人原文地址:http://www.cnblogs.com/lyout/p/3292702.html。

首先我们添加两个测试精灵(宽:27,高:40)到场景里面:

		CCSprite *sprite1 = CCSprite::create("player.png");
		sprite1->setPosition(ccp(20, 40));
		sprite1->setAnchorPoint(ccp(0, 0));
		this->addChild(sprite1);

		CCSprite *sprite2 = CCSprite::create("player.png");
		sprite2->setPosition(ccp(-15, -30));
		sprite2->setAnchorPoint(ccp(1, 1));
		this->addChild(sprite2);

然后调试,在场景中大概是下图这样显示(以左下角为坐标原点,从左到右为x方向,从下到上为y方向,废话了:)):

在cocos2d-x中,每个精灵都有一个锚点,以后对精灵的操作(比如旋转)都会围绕锚点进行,我们暂且可以看作是精灵的中心位置,一般来说有每个方向有三种可能的值:0,0.5,1。上图中红色圆点即为各自的锚点,sprite1 锚点为 (0,0) 左下角,sprite2锚点为(1,1)在右上角。

现在我们来看看坐标系转换,同样地,我们先写点测试代码:

		CCPoint p1 = sprite2->convertToNodeSpace(sprite1->getPosition());
		CCPoint p2 = sprite2->convertToWorldSpace(sprite1->getPosition());
		CCPoint p3 = sprite2->convertToNodeSpaceAR(sprite1->getPosition());
		CCPoint p4 = sprite2->convertToWorldSpaceAR(sprite1->getPosition());

接着,再打印出各点的x,y值:

		CCLog("p1:%f,%f", p1.x, p1.y);
		CCLog("p2:%f,%f", p2.x, p2.y);
		CCLog("p3:%f,%f", p3.x, p3.y);
		CCLog("p4:%f,%f", p4.x, p4.y);

现在开始分析这四个常用坐标系转换函数转换后的值(有兴趣的同学可以先算一算)。

由于cocos2d-x的坐标系(本地坐标系)是以左下角为坐标原点的,所以 sprite1和sprite2的坐标原点在上图的位置分别是(20,40)、(-42,-70),那么很明显的:

p1就是sprite1锚点相对于sprite2原点来说在sprite2坐标系中的位置,经过对比上图,我们可以得到(20-(-42),40-(-70))即(62,110)

p2就是sprite1锚点相对于sprite2原点来说在上图坐标系中的位置,这样我们可以计算出sprite1在sprite2坐标系中的位置:(20+(-42),40+(-70)),即(-22,-30)

p3就是sprite1锚点相对于sprite2锚点来说在sprite2坐标系中的位置,也就是(20-(-15),40-(-30)),即(35,70)

p4就是sprite1锚点相对于sprite2锚点来说在上图坐标系中的位置,也就是(20+(-15),40+(-30)),即(5,10)

现在我们可以知道,计算方法都是用sprite1的坐标去加减sprite2的坐标,针对本地坐标系就用减法,针对世界坐标系就用加法

好了,方法出来了,有兴趣的可以做计算一下以下几个坐标的值(先不要上机调试),然后回复我,差不多10个回复后我会贴出正确答案:

		CCPoint p1 = sprite1->convertToNodeSpace(sprite2->getPosition());
		CCPoint p2 = sprite1->convertToWorldSpace(sprite2->getPosition());
		CCPoint p3 = sprite1->convertToNodeSpaceAR(sprite2->getPosition());
		CCPoint p4 = sprite1->convertToWorldSpaceAR(sprite2->getPosition());