/**
* Converts a Point to node (local) space coordinates. The result is in Points.
*/
CCPoint convertToNodeSpace(const CCPoint& worldPoint);
/**
* Converts a Point to world space coordinates. The result is in Points.
*/
CCPoint convertToWorldSpace(const CCPoint& nodePoint);
/**
* Converts a Point to node (local) space coordinates. The result is in Points.
* treating the returned/received node point as anchor relative.
*/
CCPoint convertToNodeSpaceAR(const CCPoint& worldPoint);
/**
* Converts a local Point to world space coordinates.The result is in Points.
* treating the returned/received node point as anchor relative.
*/
CCPoint convertToWorldSpaceAR(const CCPoint& nodePoint);
按照cocos2d-x的注释,可能很难理解出这几个方法有什么不同,特别是有没有AR后缀的。这里先给出结论:有AR的后缀的方法<convertToNodeSpaceAR,convertToWorldSpaceAR>在转化的时候,和参照物的锚点有关系,而没有AR后缀的方法<convertToNodeSpace,convertToWorldSpace>跟参照物的锚点没有关系,也就是永远都是以<0,0>作为锚点,换句话说如果参照物的描点不是<0,0>的话,要先假定参照物的锚点是<0,0>,得出在锚点为<0,0>的时候的坐标,然后以这个坐标参与转化计算
假设创建了两个CCSprite,node1锚点为(0,0),位置为(20,40),size为(40,40),node2锚点为(1,1)位置为(-5,-20),size为(40,40)
CCPoint point6 = sprite1->convertToWorldSpace(sprite2->getPosition());
CCPoint point7 = sprite1->convertToNodeSpaceAR(sprite2->getPosition());
CCPoint point8 = sprite1->convertToWorldSpaceAR(sprite2->getPosition());
CCLog("position = (%f,point5.x,point5.y);
CCLog("position = (%f,point6.x,point6.y);
CCLog("position = (%f,point7.x,point7.y);
CCLog("position = (%f,point8.x,point8.y);
Cocos2d: position = (-5.000000,0.000000)
Cocos2d: position = (-25.000000,-60.000000)
Cocos2d: position = (15.000000,20.000000)
总之,没有AR后缀在转化的时候,实际上是以参照物的左下角,向上,向右来建立坐标系的,而有AR后缀在转化的时候,以参照物的锚点,向上向右来建立坐标系的,可以通过实际运行下,更加容易理解
* Converts a Point to node (local) space coordinates. The result is in Points.
*/
CCPoint convertToNodeSpace(const CCPoint& worldPoint);
/**
* Converts a Point to world space coordinates. The result is in Points.
*/
CCPoint convertToWorldSpace(const CCPoint& nodePoint);
/**
* Converts a Point to node (local) space coordinates. The result is in Points.
* treating the returned/received node point as anchor relative.
*/
CCPoint convertToNodeSpaceAR(const CCPoint& worldPoint);
/**
* Converts a local Point to world space coordinates.The result is in Points.
* treating the returned/received node point as anchor relative.
*/
CCPoint convertToWorldSpaceAR(const CCPoint& nodePoint);
按照cocos2d-x的注释,可能很难理解出这几个方法有什么不同,特别是有没有AR后缀的。这里先给出结论:有AR的后缀的方法<convertToNodeSpaceAR,convertToWorldSpaceAR>在转化的时候,和参照物的锚点有关系,而没有AR后缀的方法<convertToNodeSpace,convertToWorldSpace>跟参照物的锚点没有关系,也就是永远都是以<0,0>作为锚点,换句话说如果参照物的描点不是<0,0>的话,要先假定参照物的锚点是<0,0>,得出在锚点为<0,0>的时候的坐标,然后以这个坐标参与转化计算
假设创建了两个CCSprite,node1锚点为(0,0),位置为(20,40),size为(40,40),node2锚点为(1,1)位置为(-5,-20),size为(40,40)
接下来,convertToNodeSpace:调用CCPoint point = node1->convertToNodeSpace(node2->getPosition());
将node2的坐标转化成相对于node1的本地坐标
node1的锚点为(0,0)位置(20,40),node2的锚点为(1,1),位置(-5,-20),上面说过convertToNodeSpace作为参照物时,锚点的位置是(0,0),程序中设置的描点不起作用,碰巧,node1的锚点为(0,0),所以以(20,40)这个点做为参照物,方向为向右,向上为正方向,(-5,-20)相对于这个参考点就是(-25,-60)
而convertToWorldSpace:调用CCPoint point = node1->convertToWorldSpace(node2->getPosition());
是将node2的坐标转化成相对于node1的世界坐标,这个可以这样理解,因为世界坐标是固定的,node1的世界坐标只是在固定世界坐标的基础上做了平移,这边是(20,40),而node2相对于node1的世界坐标也只是做了同样大小的平移而已,也就是(-5,-20)通过(20,40)做了平移,结果(15,20)
convertToNodeSpaceAR,就是把node1的坐标系原点设置在锚点的位置,这里的锚点是(0,0)所以转化之后和上面的convertToNodeSpace一样,结果也是一样的,convertToWorldSpaceAR同理
测试:
CCSprite *sprite1 = CCSprite::spriteWithFile("CloseNormal.png");
sprite1->setPosition(ccp(20,40));
sprite1->setAnchorPoint(ccp(0,0));
this->addChild(sprite1);
CCSprite *sprite2 = CCSprite::spriteWithFile("CloseNormal.png");
sprite2->setPosition(ccp(-5,-20));
sprite2->setAnchorPoint(ccp(1,1));
this->addChild(sprite2);
CCPoint point1 = sprite1->convertToNodeSpace(sprite2->getPosition());
CCPoint point2 = sprite1->convertToWorldSpace(sprite2->getPosition());
CCPoint point3 = sprite1->convertToNodeSpaceAR(sprite2->getPosition());
CCPoint point4 = sprite1->convertToWorldSpaceAR(sprite2->getPosition());
CCLog("position = (%f,%f)",point1.x,point1.y);
运行结果:
position = (-25.000000,-60.000000)
position = (15.000000,20.000000)
和预算的一样
如果这时将sprite1的锚点设置成(0.5,0.5),对哪两个方法的转化会有影响呢?上面我们提到,convertToNodeSpace,convertToWorldSpace这两个方法会有影响的,他们在转化的时候,跟程序设置的锚点没有关系,横用(0,0)作为描点,所以在这两个方法进行转化的时候,我们要先计算出,以锚点(0,0)的坐标。这里实际上是以(0,20)这个坐标点在进行转化的。上面说过了,sprite的size为(40,40)。从上上面的分析,我们得出,convertToNodeSpace转化后为(-5,-40),convertToWorldSpace转化后为(-5,0) convertToNodeSpaceAR和convertToWorldSpaceAR则没有受到影响
sprite1->setAnchorPoint(ccp(0.5,0.5));
CCPoint point5 = sprite1->convertToNodeSpace(sprite2->getPosition());CCPoint point6 = sprite1->convertToWorldSpace(sprite2->getPosition());
CCPoint point7 = sprite1->convertToNodeSpaceAR(sprite2->getPosition());
CCPoint point8 = sprite1->convertToWorldSpaceAR(sprite2->getPosition());
CCLog("position = (%f,point5.x,point5.y);
CCLog("position = (%f,point6.x,point6.y);
CCLog("position = (%f,point7.x,point7.y);
CCLog("position = (%f,point8.x,point8.y);
运算结果:
Cocos2d: position = (-5.000000,-40.000000)Cocos2d: position = (-5.000000,0.000000)
Cocos2d: position = (-25.000000,-60.000000)
Cocos2d: position = (15.000000,20.000000)
总之,没有AR后缀在转化的时候,实际上是以参照物的左下角,向上,向右来建立坐标系的,而有AR后缀在转化的时候,以参照物的锚点,向上向右来建立坐标系的,可以通过实际运行下,更加容易理解