Home > Database > Mysql Tutorial > body text

ocos2d

WBOY
Release: 2016-06-07 15:09:53
Original
1099 people have browsed it

原文出自:http://www.cnblogs.com/lyout/p/3292702.html 首先对于初学的,带大家认识 cocos2d-x 中坐标系的几个概念,参考 http://blog.csdn.net/tskyfree/article/details/8292544。其他的往下看。 弄懂坐标系是开始开发的重要的一步,为了不让大家头晕,

原文出自:http://www.cnblogs.com/lyout/p/3292702.html

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

弄懂坐标系是开始开发的重要的一步,为了不让大家头晕,现在里沃特

首先我们添加两个测试精灵(宽: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方向,废话了:)):

ocos2d

在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());


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!