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. 工作总结:文件对话框的分类(C++)

    原文地址:http://www.jizhuomi.com/software/173.html 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见到这两种文件对话框.例如 ...

  2. 【网络流24题】No.18 分配问题 (二分图最佳匹配 费用流|KM)

    [题意] 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为 cij . 试设计一个将n 件工作分配给 n 个人做的分配方案, 使产生的总效益最大. 输入文件示例input. ...

  3. 使用HttpClient向服务器发送restful post请求

    直接上代码: public class RestClient { public static void main(String[] args) { String url = "http:// ...

  4. USB 2.0 A型、B型、Mini和Micro接口定义及封装

    USB全称Universal Serial Bus(通用串行总线),目前USB 2.0接口分为四种类型A型.B型.Mini型还有后来补充的Micro型接口,每种接口都分插头和插座两个部分,Micro还 ...

  5. bzoj2004

    反正N<=10^9肯定是矩阵乘法反正p<=10肯定是状压dp首先有一个非常重要的性质是任意连续P个站,必须保证K辆车必须停在其中的一个站我们设f[i,S]表示到第i个站搞定了后,这K辆公交 ...

  6. BZOJ_1036_[ZJOI2008]_树的统计Conut_(树链剖分)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1036 给出一棵树以及各点的权值,对数进行如下三种操作: 1.改变某一节点u的值为t; 2.求节 ...

  7. PHP 'ext/soap/php_xml.c'不完整修复多个任意文件泄露漏洞

    漏洞版本: PHP 5.4.1 PHP 5.3.13 PHP 5.3.12 PHP 5.3.11 PHP 5.3.10 PHP 5.3.1 PHP 5.3 漏洞描述: BUGTRAQ ID: 6237 ...

  8. 安装scrapy

  9. DevExpress控件汉化类 z

    更新了一些字段,VER9.3.3 using System; using DevExpress.XtraEditors.Controls; using DevExpress.XtraGrid.Loca ...

  10. HDU-2975 Billboard

    Billboard Time Limit : 20000/8000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...