Cocos2dx坐标转换
Cocos2dx坐标转换
这段时间加班有点猛,没有太多时间来写博客了,ok,继续完成任务;
前言
这里将会重点介绍四个函数:
- convertToNodeSpace
- convertToNodeSpaceAR
- convertToWorldSpace
- convertToWorldSpaceAR
- convertToWindowSpace
前面两个针对的是转化成相对于结点的坐标,后面的则是转化成世界坐标下的位置;
所以需要理解Cocos2dx里面的坐标系,这里面包括OpenGL坐标系,屏幕坐标系;
坐标系
OpenGL坐标系:坐标系原点在屏幕左下角,x轴向右,y轴向上,Cocos2dx以该坐标系为基础;
屏幕坐标系:和上面不同的是,其原点在于左上角,x轴向右,y轴向下,iOS的屏幕触摸事件传入的位置信息使用的是该坐标系,因此Cocos2dx需要将其转化成OpenGL坐标系,这就需要我们上面提到的几种函数;
绝对坐标系/世界坐标系:结点设置位置的时候,使用的是相对于其父结点的本地坐标系,而不是世界坐标系,因此Cocos2dx在绘制的时候,会把这些元素的本地坐标系映射成世界坐标系坐标,世界坐标和OpenGL坐标系方向一致;
结点坐标系统:Cocos2dx中的元素是有父子关系的层级结构,每个结点都有独立的坐标系,其设置位置使用的是相对于其父结点的本地坐标系,它和OpenGL坐标系中的方向是一致的,原点位置在父结点的左下角;(注意:结点坐标系的原点默认是其左下角位置)
如果父亲结点使用的是场景树中的顶层结点,也就是说其没有父结点,那么就和世界坐标系重合了;
在CCTouch中已经封装了一些获取位置坐标的函数:
/** Returns the current touch location in OpenGL coordinates.
*
* @return The current touch location in OpenGL coordinates.
*/
Vec2 getLocation() const;
/** Returns the previous touch location in OpenGL coordinates.
*
* @return The previous touch location in OpenGL coordinates.
*/
Vec2 getPreviousLocation() const;
/** Returns the start touch location in OpenGL coordinates.
*
* @return The start touch location in OpenGL coordinates.
*/
Vec2 getStartLocation() const;
/** Returns the delta of 2 current touches locations in screen coordinates.
*
* @return The delta of 2 current touches locations in screen coordinates.
*/
Vec2 getDelta() const;
/** Returns the current touch location in screen coordinates.
*
* @return The current touch location in screen coordinates.
*/
Vec2 getLocationInView() const;
/** Returns the previous touch location in screen coordinates.
*
* @return The previous touch location in screen coordinates.
*/
Vec2 getPreviousLocationInView() const;
/** Returns the start touch location in screen coordinates.
*
* @return The start touch location in screen coordinates.
*/
Vec2 getStartLocationInView() const;
下面介绍转换函数:
/**
* Converts a Vec2 to node (local) space coordinates. The result is in Points.
*
* @param worldPoint A given coordinate.
* @return A point in node (local) space coordinates.
*/
Vec2 convertToNodeSpace(const Vec2& worldPoint) const;
/**
* Converts a Vec2 to world space coordinates. The result is in Points.
*
* @param nodePoint A given coordinate.
* @return A point in world space coordinates.
*/
Vec2 convertToWorldSpace(const Vec2& nodePoint) const;
/**
* Converts a Vec2 to node (local) space coordinates. The result is in Points.
* treating the returned/received node point as anchor relative.
*
* @param worldPoint A given coordinate.
* @return A point in node (local) space coordinates, anchor relative.
*/
Vec2 convertToNodeSpaceAR(const Vec2& worldPoint) const;
/**
* Converts a local Vec2 to world space coordinates.The result is in Points.
* treating the returned/received node point as anchor relative.
*
* @param nodePoint A given coordinate.
* @return A point in world space coordinates, anchor relative.
*/
Vec2 convertToWorldSpaceAR(const Vec2& nodePoint) const;
convertToWorldSpace:将当前结点的本地坐标下的坐标转化成世界坐标系中(以对象作为父结点下的结点计算坐标);
convertToNodeSpace:将世界坐标转换成当前结点的本地坐标系中;
而后面有AR表明了其的基准坐标是基于坐标锚点;
Cocos2dx坐标转换的更多相关文章
- cocos2dx进阶学习之坐标转换
在cocos2dx中,有四种坐标系 GL坐标系:左下为原点,x轴向右,y轴向上 UI坐标系:左上为原点,x轴向右,y轴向下 世界坐标系:与GL坐标系相同 本地坐标系:是节点(CCNode)的坐标系,原 ...
- 【Cocos2d-x游戏开发】浅谈游戏中的坐标系
无论是开发2D还是开发3D游戏,首先必须弄清楚坐标系的概念.在Cocos2d-x中,需要了解的有OpenGL坐标系.世界坐标系和节点坐标系. 1.UI坐标系 IOS/Android/Windows ...
- [原创]cocos2d-x研习录-第二阶 概念类之节点类(CCNode)
节点类CCNode在基本概念中并不存在,它是为了建立基本概念之间的关联关系而抽象出来的中间辅助类.这个类在Cocos2D-x中极为重要,它为概念类之间搭建了一座宏伟的桥梁.它的继承关系图如下: ...
- Cocos2d-x v3.6制作射箭游戏(二)
原文 Cocos2d-x v3.6制作射箭游戏(二) 六 24, 2015by RENSHANin COCOS2D-X 上章我们创建并加载了游戏地图,接下来的两章我们将实现如下的效果. 在开始之前,先 ...
- 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 ...
- Cocos2dx中零散知识点
cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce.功能分别是 每隔几秒调用自定义函数.调用系统默认的update()函数.只调用一次自定义函数 ...
- cocos2dx使用TiledMap创建斜45度地图场景
做游戏,场景是一个很重要的部分,如果缺少这一步,很难做出好的游戏,对于cocos2dx来说,有很多2D的地图编辑器可以用,效果都还可以,其中Tiled是支持的比较好的,它支持Tiled编辑出来的几种模 ...
- 实例介绍Cocos2d-x物理引擎:使用关节
在游戏中我们可以通过关节约束两个物体的运动.我们通过一个距离关节实例,介绍一下如何在使用关节. 这个实例的运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点和附近生成 ...
- Cocos2d-x 3.0坐标系详解(转载)
Cocos2d-x 3.0坐标系详解 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系. 笛卡尔坐标系 笛卡尔坐标系中定义右手系原点在左下角,x向右,y向上,z向外,OpenG ...
随机推荐
- Android之网络编程利用PHP操作MySql插入数据(四)
因为最近在更新我的项目,就想着把自己在项目中用到的一些的简单的与网络交互的方法总结一下,所以最近Android网络编程方面的博文会比较多一些,我尽量以最简单的方法给大家分享,让大家明白易懂.如果有什么 ...
- Linux系统上安装软件(ftp服务器)
一:安装ftp服务器 在安装linux系统的时候,自定义软件包安装时,我已经勾选了ftp服务器,所以已经 安装过了,如果没有勾选,需要额外下载ftp的安装包,进行安装. ftp服务器搭建过程中遇到的问 ...
- java学习,从一个字符串中统计同一类型出现的次数
1.从字符串“AS345asdzf*())sddsWE”中统计大写字母.小写字母.其他类型的出现的次数 String s="AS345asdzf*())sddsWE"; int l ...
- MYSQL交换两列数据实例
UPDATE cf_2015_company a, cf_2015_company bSET a.cname = b.linkman, a.linkman = b.cnameWHERE a.id = ...
- ADO.NET通用数据库访问类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Unity3D 之UGUI制小地图
这里使用UGUI制作一个小地图. 方法一: 第一步:使用UGUI弄一个地图背景和人物指针 第二步:脚本获取人物的位置和角度给人物指针进行同步 将 PlayerIconController.cs 文件绑 ...
- SQL Server调优系列进阶篇 - 如何重建数据库索引
随着数据的数据量的急剧增加,数据库的性能也会明显的有些缓慢这个时候你可以考虑下重建索引或是重新组织索引了. DBCC SHOWCONTIG('表名') 可以查看当前表的索引碎情况. 重建索引 方法一: ...
- -exec和|xargs
注意xargs会被空格割裂,所以遇到带有空格的文件名就不好办了,解决方法是使用-print0 例如:删除.目录下30天之前的.png文件 -type f -name rm 或者使用-exec:删除.目 ...
- Java_链表实现
http://blog.csdn.net/a19881029/article/details/22695289
- ArryList vs LinkedList
references: http://www.javaperformancetuning.com/articles/randomaccess.shtml http://stackoverflow.co ...