Introduction of Different Coordinate Systems

Cartesian Coordinate System

You probably have known "Cartesian Coordinate System" from school where it's heavily used in geometry lessons. If you have forgotten, these image will remind you quickly:

There're 3 types of coordinate system that you will meet in mobile games development.

UI Coordinate System

In common UI Coordinates on iOS/Android/Windows SDK:

  • The origin (x=0, y=0) is at the top-left corner.
  • X axis starts at the left side of the screen and increase to the right;
  • Y coordinates start at the top of the screen and increase downward,

looks like this

Direct3D Coordinate System

DirectX uses Left-handed Cartesian Coordinate.

OpenGL and Cocos2d Coordinate System

Cocos2d-x/-html5/-iphone uses the same coordinate system as OpenGL, which is so called "Right-handed Cartesian Coordinate System".

We only use x-axis & y-axis in 2D world. So in your cocos2d games:

  • The origin (x=0, y=0) is in the bottom-left corner of screen, which means the screen is in the first quartile of right-handed cartesian coordinate system,
  • X axis starts at the left side of the screen and increase to the right;
  • Y axis starts at the bottom of the screen and increase upwards.

And here’s a picture that helps illustrate Cocos2d-x Coordinates a bit better:

Note that it's different from common UI coordinate systems and DirectX coordinate systems.

Parent and Childrens

Every class derived from CCNode (Ultimate cocos2d-x class) will have a anchorPoint property.
When determining where to draw the object (sprite, layer, whatever), cocos2d-x will combine both properties position and anchorPoint. Also, when rotating an object, cocos2d-x will rotate it around its anchorPoint.

We create a sprite as a gray parent and another sprite as blue child.Set parent's position to ccp(100,100),child's anchor point in the center of circle .

 1    CCSprite* parent = CCSprite::create("parent.png");
2 parent->setAnchorPoint(ccp(0, 0));// Anchor Point
3 parent->setPosition(ccp(100, 100));
4 parent->setAnchorPoint(ccp(0, 0));
5 addChild(parent);
6
7 //create child
8 CCSprite* child = CCSprite::create("child.png");
9 child->setAnchorPoint(ccp(0.5, 0.5));
10 child->setPosition(ccp(0, 0));
11 parent->addChild(child);//add child sprite into parent sprite.

Although we set child's position of ccp(0,0),parent's position is ccp(100,100).Therefore,child's position is :

Anchor Point

The anchor point is used for both positioning and rotating an object. The anchor point coordinates are relative coordinates which usually correspond to a point within an object. For example, the anchor point ccp(0.5, 0.5) corresponds to the center of the object. When setting the position of the object, the object is positioned such that the anchor point will be at the coordinates specified in the setPosition() call. Similarly, when rotating the object, it is rotated about the anchor point.

For example, this sprite has an anchorPoint of ccp(0,0) and a position of ccp(0,0):

As a result, the sprite is positioned such that its anchor point (which is located at the bottom-left corner) is placed at the bottom left corner of its parent layer.

1// create sprite
2CCSprite* sprite = CCSprite::create("bottomleft.png");
3sprite->setAnchorPoint(ccp(0, 0));// Anchor Point
4sprite->setPosition(ccp(0,0));
5addChild(sprite);

In the following example, the relative nature of the anchor co-ordinates is demonstrated. The anchor point is assigned to be ccp(0.5, 0.5), which corresponds to the center of the sprite.

1// create sprite
2CCSprite* sprite = CCSprite::create("center.png");
3sprite->setAnchorPoint(ccp(0.5, 0.5));// Anchor Point
4sprite->setPosition(ccp(0,0));
5addChild(sprite);

As you can see, the center of the sprite is positioned at ccp(0,0). It can also be seen that the anchor point is not a pixel value. The X and Y values of the anchor point are relative to the size of the node.

getVisibleSize, getVisibleOrigin vs getWinSize

VisibleSize returns visible size of the OpenGL view in points.The value is equal to getWinSize if don't invoke CCEGLView::setDesignResolutionSize().

getVisibleOrigin returns visible origin of the OpenGL view in points. Please refer to Multi resolution support for more details

How to convert co-ordinates

convertToNodeSpace:

convertToNodeSpace will be used in, for example, tile-based games, where you have a big map. convertToNodeSpace will convert your openGL touch co-ordinates to the co-ordinates of the .tmx map or anything similar.

Example:

The following picture shows that we have node1 with anchor point (0,0) and node2 with anchor point (1,1).

We invoke CCPoint point = node1->convertToNodeSpace(node2->getPosition()); convert node2's SCREEN coords to node1's local.As the result,node2 with the position of (-25,-60).

convertToWorldSpace:

convertToWorldSpace(const CCPoint& nodePoint) converts on-node coords to SCREEN coordinates.convertToWorldSpace will always return SCREEN position of our sprite, might be very useful if you want to capture taps on your sprite but need to move/scale your layer.

Generally, the parent node call this method with the child node position, return the world's postion of child's as a result. It seems make no sense calling this method if the caller isn't the parent...

Example:

1CCPoint point = node1->convertToWorldSpace(node2->getPosition());

the above code will convert the node2‘s coordinates to the coordinates on the screen.

For example if the position of node2 is (0, 0) which will be the bottom left corner of the node1, but not necessarily on the screen. This will convert (0, 0) of the node2 to the position of the screen(in this case is (15,20)). The result shows in the following picture:

convertToWorldSpaceAR,

convertToWorldSpaceAR will return the position relatevely to anchor point: so if our scene - root layer has Anchor Point of ccp(0.5f, 0.5f) - default, convertToWorldSpaceAR should return position relatively to screen center.

convertToNodeSpaceAR - the same logic as for .convertToWorldSpaceAR

Sample code:
 1
2CCSprite *sprite1 = CCSprite::create("CloseNormal.png");
3
4sprite1->setPosition(ccp(20,40));
5
6sprite1->setAnchorPoint(ccp(0,0));
7
8this->addChild(sprite1);
9
10CCSprite *sprite2 = CCSprite::create("CloseNormal.png");
11
12sprite2->setPosition(ccp(-5,-20));
13
14sprite2->setAnchorPoint(ccp(1,1));
15
16this->addChild(sprite2);
17
18CCPoint point1 = sprite1->convertToNodeSpace(sprite2->getPosition());
19
20CCPoint point2 = sprite1->convertToWorldSpace(sprite2->getPosition());
21
22CCPoint point3 = sprite1->convertToNodeSpaceAR(sprite2->getPosition());
23
24CCPoint point4 = sprite1->convertToWorldSpaceAR(sprite2->getPosition());
25
26CCLog("position = (%f,%f)",point1.x,point1.y);
27
28CCLog("position = (%f,%f)",point2.x,point2.y);
29
30CCLog("position = (%f,%f)",point3.x,point3.y);
31
32CCLog("position = (%f,%f)",point4.x,point4.y);
33

Result:

 1
2position = (-25.000000,-60.000000)
3
4position = (15.000000,20.000000)
5
6position = (-25.000000,-60.000000)
7
8position = (15.000000,20.000000)
9

References

Coordinate System的更多相关文章

  1. IOS深入学习(4)之Coordinate System

    1 前言 在IOS中相信大家会经常跟一些bounds,frame之类的打交道,这不免会涉及坐标系统,今天我们就来介绍一下Coordinate System(坐标系). 2 详述 坐标系统是定位,大小, ...

  2. ArcGIS坐标系转换出错:Error 999999执行函数出错 invalid extent for output coordinate system

    本文主要介绍在用ArcGIS做坐标系转换过程中可能会遇到的一个问题,并分析其原因和解决方案. 如下图,对一份数据做坐标系转换: 过了一会儿,转换失败了.错误消息如下: “消息”中提示,“执行函数出错 ...

  3. SVG viewBox & coordinate system

    SVG viewBox & coordinate system https://codepen.io/xgqfrms/pen/abOOrjp <html> <body> ...

  4. Silverlight & Blend动画设计系列十:Silverlight中的坐标系统(Coordinate System)与向量(Vector)运动

    如果我们习惯于数学坐标系,那么对于Silverlight中的坐标系可能会有些不习惯.因为在Silverlight中的坐标系与Flash中的坐标系一样,一切都的颠倒的.在标准的数学坐标系中,X轴表示水平 ...

  5. OpenCASCADE Coordinate Transforms

    OpenCASCADE Coordinate Transforms eryar@163.com Abstract. The purpose of the OpenGL graphics process ...

  6. HoloLens开发手记-世界坐标系 Coordinate systems

    坐标系 Coordinate systems 全息的核心是,全息应用可以在真实世界中放置全息图形并使得它们看起来和听起来像真实的物体.这涉及到了物体在真实世界中的定位和方向的确定,这对用户来说很重要. ...

  7. Chromosome coordinate systems: 0-based, 1-based

    From: https://arnaudceol.wordpress.com/2014/09/18/chromosome-coordinate-systems-0-based-1-based/ I’v ...

  8. Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points

    Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple ...

  9. 什么是极坐标? —— 一点微小的想法 What is Polar Coordinate ? - Some Naive Thoughts about It

    Can you answer these three questions? The answer seems to be trivial, since we can use our eyes to o ...

随机推荐

  1. 《Linux命令行大全》系列(二、导航)

    文件系统的导航,是一个不断访问树形结构中节点的过程. 文件系统树 Linux只有一个倒立的文件系统树 不同设备可以挂载到这同一个树上 文件和子目录是此树的组成部分,最顶层的即根目录 目录 根据树节点间 ...

  2. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】

    题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...

  3. Altium Designer10 如何导出Gerber文件

    版本:AD10.818 目的:Gerber文件导出备忘 http://blog.sina.com.cn/s/blog_9b9a51990100zyyv.html 目录: Step1:设置原点 Step ...

  4. Windows.document对象

    一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:var a =docunment.getElementById("i ...

  5. 【HDOJ】1242 Rescue

    BFS+优先级队列. #include <iostream> #include <cstdio> #include <cstring> #include <q ...

  6. bzoj2342

    shoi题目好坑爹 首先自己测发现这道题如果用后缀数组+rmq处理每个点回文串能延伸长度的话会TLE (当然我用的是倍增+ST的方法,如果用三分构建后缀数组+笛卡尔树处理rmq我就不知道了): 关于最 ...

  7. (转载)Flash Number 数据类型

    (转载)http://www.g168.net/txt/flash/learningactionscript/00001183.html Number 数据类型 Number 数据类型是双精度浮点数. ...

  8. LoadRunner中的参数与变量

    在LoadRunner脚本开发中,经常会遇到参数与变量相互转换的情况,本文对常见的转换情形进行了方法总结. 1.变量的赋值 //将字符串赋值给变量 ]; strcpy(strTemp, "H ...

  9. suse系统FTP问题

    1.修改FTP端口问题 在 这个配置文件中vi /etc/vsftpd.conf 添加 Listen_port=34 vi /etc/services ftp        21/tcp    # F ...

  10. Wildfly 中支持jersey,并websocket的默认配置修改。

    以下为在jboss安装相对路径来写的.1.\domain\configuration\domain.xml修改内容: 注释关键字jaxrs存在的四行.修改后如下: <!--<extensi ...