Cocos2dx坐标转换

这段时间加班有点猛,没有太多时间来写博客了,ok,继续完成任务;

前言

这里将会重点介绍四个函数:

  1. convertToNodeSpace
  2. convertToNodeSpaceAR
  3. convertToWorldSpace
  4. convertToWorldSpaceAR
  5. convertToWindowSpace

前面两个针对的是转化成相对于结点的坐标,后面的则是转化成世界坐标下的位置;

所以需要理解Cocos2dx里面的坐标系,这里面包括OpenGL坐标系,屏幕坐标系;

坐标系

  1. OpenGL坐标系:坐标系原点在屏幕左下角,x轴向右,y轴向上,Cocos2dx以该坐标系为基础;

  2. 屏幕坐标系:和上面不同的是,其原点在于左上角,x轴向右,y轴向下,iOS的屏幕触摸事件传入的位置信息使用的是该坐标系,因此Cocos2dx需要将其转化成OpenGL坐标系,这就需要我们上面提到的几种函数;

  3. 绝对坐标系/世界坐标系:结点设置位置的时候,使用的是相对于其父结点的本地坐标系,而不是世界坐标系,因此Cocos2dx在绘制的时候,会把这些元素的本地坐标系映射成世界坐标系坐标,世界坐标和OpenGL坐标系方向一致;

  4. 结点坐标系统:Cocos2dx中的元素是有父子关系的层级结构,每个结点都有独立的坐标系,其设置位置使用的是相对于其父结点的本地坐标系,它和OpenGL坐标系中的方向是一致的,原点位置在父结点的左下角;(注意:结点坐标系的原点默认是其左下角位置)

    如果父亲结点使用的是场景树中的顶层结点,也就是说其没有父结点,那么就和世界坐标系重合了;

在CCTouch中已经封装了一些获取位置坐标的函数:

  1. /** Returns the current touch location in OpenGL coordinates.
  2. *
  3. * @return The current touch location in OpenGL coordinates.
  4. */
  5. Vec2 getLocation() const;
  6. /** Returns the previous touch location in OpenGL coordinates.
  7. *
  8. * @return The previous touch location in OpenGL coordinates.
  9. */
  10. Vec2 getPreviousLocation() const;
  11. /** Returns the start touch location in OpenGL coordinates.
  12. *
  13. * @return The start touch location in OpenGL coordinates.
  14. */
  15. Vec2 getStartLocation() const;
  16. /** Returns the delta of 2 current touches locations in screen coordinates.
  17. *
  18. * @return The delta of 2 current touches locations in screen coordinates.
  19. */
  20. Vec2 getDelta() const;
  21. /** Returns the current touch location in screen coordinates.
  22. *
  23. * @return The current touch location in screen coordinates.
  24. */
  25. Vec2 getLocationInView() const;
  26. /** Returns the previous touch location in screen coordinates.
  27. *
  28. * @return The previous touch location in screen coordinates.
  29. */
  30. Vec2 getPreviousLocationInView() const;
  31. /** Returns the start touch location in screen coordinates.
  32. *
  33. * @return The start touch location in screen coordinates.
  34. */
  35. Vec2 getStartLocationInView() const;

下面介绍转换函数:

  1. /**
  2. * Converts a Vec2 to node (local) space coordinates. The result is in Points.
  3. *
  4. * @param worldPoint A given coordinate.
  5. * @return A point in node (local) space coordinates.
  6. */
  7. Vec2 convertToNodeSpace(const Vec2& worldPoint) const;
  8. /**
  9. * Converts a Vec2 to world space coordinates. The result is in Points.
  10. *
  11. * @param nodePoint A given coordinate.
  12. * @return A point in world space coordinates.
  13. */
  14. Vec2 convertToWorldSpace(const Vec2& nodePoint) const;
  15. /**
  16. * Converts a Vec2 to node (local) space coordinates. The result is in Points.
  17. * treating the returned/received node point as anchor relative.
  18. *
  19. * @param worldPoint A given coordinate.
  20. * @return A point in node (local) space coordinates, anchor relative.
  21. */
  22. Vec2 convertToNodeSpaceAR(const Vec2& worldPoint) const;
  23. /**
  24. * Converts a local Vec2 to world space coordinates.The result is in Points.
  25. * treating the returned/received node point as anchor relative.
  26. *
  27. * @param nodePoint A given coordinate.
  28. * @return A point in world space coordinates, anchor relative.
  29. */
  30. Vec2 convertToWorldSpaceAR(const Vec2& nodePoint) const;

convertToWorldSpace:将当前结点的本地坐标下的坐标转化成世界坐标系中(以对象作为父结点下的结点计算坐标);

convertToNodeSpace:将世界坐标转换成当前结点的本地坐标系中;

而后面有AR表明了其的基准坐标是基于坐标锚点;

参考文章:Cocos2d-x 详解坐标系统

Cocos2dx坐标转换的更多相关文章

  1. cocos2dx进阶学习之坐标转换

    在cocos2dx中,有四种坐标系 GL坐标系:左下为原点,x轴向右,y轴向上 UI坐标系:左上为原点,x轴向右,y轴向下 世界坐标系:与GL坐标系相同 本地坐标系:是节点(CCNode)的坐标系,原 ...

  2. 【Cocos2d-x游戏开发】浅谈游戏中的坐标系

    无论是开发2D还是开发3D游戏,首先必须弄清楚坐标系的概念.在Cocos2d-x中,需要了解的有OpenGL坐标系.世界坐标系和节点坐标系.  1.UI坐标系 IOS/Android/Windows ...

  3. [原创]cocos2d-x研习录-第二阶 概念类之节点类(CCNode)

    节点类CCNode在基本概念中并不存在,它是为了建立基本概念之间的关联关系而抽象出来的中间辅助类.这个类在Cocos2D-x中极为重要,它为概念类之间搭建了一座宏伟的桥梁.它的继承关系图如下:     ...

  4. Cocos2d-x v3.6制作射箭游戏(二)

    原文 Cocos2d-x v3.6制作射箭游戏(二) 六 24, 2015by RENSHANin COCOS2D-X 上章我们创建并加载了游戏地图,接下来的两章我们将实现如下的效果. 在开始之前,先 ...

  5. Cocos2dx 把 glview 渲染到 Qt 控件上(Mac 环境)

    本文原链接:http://www.cnblogs.com/zouzf/p/4423256.html 环境:Mac 10.9.2   Xcode5.1.1  Qt5.3  cocos2dx-2.2.4 ...

  6. Cocos2dx中零散知识点

    cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce.功能分别是 每隔几秒调用自定义函数.调用系统默认的update()函数.只调用一次自定义函数 ...

  7. cocos2dx使用TiledMap创建斜45度地图场景

    做游戏,场景是一个很重要的部分,如果缺少这一步,很难做出好的游戏,对于cocos2dx来说,有很多2D的地图编辑器可以用,效果都还可以,其中Tiled是支持的比较好的,它支持Tiled编辑出来的几种模 ...

  8. 实例介绍Cocos2d-x物理引擎:使用关节

    在游戏中我们可以通过关节约束两个物体的运动.我们通过一个距离关节实例,介绍一下如何在使用关节. 这个实例的运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点和附近生成 ...

  9. Cocos2d-x 3.0坐标系详解(转载)

    Cocos2d-x 3.0坐标系详解 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系. 笛卡尔坐标系 笛卡尔坐标系中定义右手系原点在左下角,x向右,y向上,z向外,OpenG ...

随机推荐

  1. Centos7 安装mongodb3.2.9 过程

    1:wget --no-check-certificate  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.2.9.tg ...

  2. linux 远程工具

    SecureCRT SecureCRT官网地址:http://www.vandyke.com/products/securecrt/ Xmanager官方网址:http://www.netsarang ...

  3. 使用WCF和WEBService出现配置的问题

    错误代码:system.serviceModel/bindings/customBinding 处的绑定没有名称为"SMSServiceServiceSoapBinding"的已配 ...

  4. webform处理过程

    一.post/get传值注意几点 post提交的时候,只有写了name属性且没有写disable=true表单元素(input,select,textarea)才会被提交. 如果不确定是get还是po ...

  5. ASP中双引号单引号和&连接符使用技巧

    ASP中双引号单引号和&连接符使用技巧 一.ASP中处在双引号中的可以是任意的字符.字符串,HTML代码 1.<%response.write ("I am here" ...

  6. ASP的高效率的分页算法.net,php同样可以参考

    一般习惯使用的有两种分页算法,一是传统的ADO分页,二是SELECT TOP分页算法.对于小型数据表,比如一两万的数据量的表,我倾向使用ADO算法,对于大型的数据表,则必须采用后者的算法了. 先来说说 ...

  7. ASP.NET Web Forms 4.5的新特性

    作者:Parry出处:http://www.cnblogs.com/parry/ 一.强类型数据控件 在出现强类型数据控件前,我们绑定数据控件时,前台一般使用Eval或者DataBinder.Eval ...

  8. php上传文件大小限制

    和你的配置文件有关.改一下PHP配置文件php.ini给你总结下相关配置,自己去改吧 1.php.ini:upload_max_filesize 所上传的文件的最大大小.默认值2M. 2.php.in ...

  9. sql 更新重复数据只取一条记录

    select s.*  from (     select *, row_number() over (partition by PersonnelAccount order BY Personnel ...

  10. 打包C#程序

    开源中国. 今天来使用VS2010对C#程序进行打包发布. 我们有一个C#程序.程序很简单,我们需要对它进行发布. Contents 步骤: 建立一个安装项目.我们得到了一个Setup1项目. 在应用 ...