Cocos2dx坐标转换

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中已经封装了一些获取位置坐标的函数:

/** 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表明了其的基准坐标是基于坐标锚点;

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

转载于:https://www.cnblogs.com/George1994/p/6155890.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值