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. JAVA与编译语言及解释语言的关系

    转自JAVA结合了编译和解释执行的优点 编译型语言是一次性编译成机器码,脱离开发环境独立运行,所以运行效率较高,但是由于编译成的是特定平台上机器码,所以可移植性差. 编译型语言的典型代表有C.C++. ...

  2. windows下DOS命令中查看被占用端口的进程

    今天在用tomcat 运行项目时报错:   java.net.BindException: Address already in use: JVM_Bind这个错误   刚开始有点怀疑是javaw.e ...

  3. LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the contex异常的原因

    eclipse里面启动tomcat报这个错误的原因是由于jar包冲突了,在我的项目里面引入了jar包,但是我的工程里面有这个jar包的两个工程,都被导入到同一个项目里面了,导致不知道该去用哪一个类,所 ...

  4. 【CF】223 Div.1 C Sereja and Brackets

    水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...

  5. Python urllib2写爬虫时候每次request open以后一定要关闭

    最近用python urllib2写一个爬虫工具,碰到运行一会程序后就会出现scoket connection peer reset错误.经过多次试验发现原来是在每次request open以后没有及 ...

  6. bzoj2257

    不难发现其实这个捣鼓过程就是一个辗转相减所以最小燃料是瓶容量的最小公约数然后穷举约数即可 ..] of longint; n,k,x,i,m,j,t:longint; procedure sort(l ...

  7. svn提交时强制注释

    不少开发员提交修改的时候都不写注释,导致查看历史时很费劲,也不太符合规范.有的公司要求每次提交修改时都写上bug号或者任务描述,那么如何在工具上防止开发员们不写注释呢? 利用svn的pre-commi ...

  8. 动态规划(DP计数):HDU 5117 Fluorescent

    Matt, a famous adventurer who once defeated a pack of dire wolves alone, found a lost court. Matt fi ...

  9. HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))

    扫雷 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  10. Angular.js vs Ember.js

    Angular.js 拥抱 HTML/CSS Misko Hevery(Angular.js的开发者之一)回答了这一问题,他的主要观点如下: 在HTML中加入太多逻辑不是好做法.Angular.js只 ...