Cocos2d-x Coordinate System(坐标系统)

本文详细解析了Cocos2d-x中的各种坐标系,包括世界坐标系与本地坐标系的区别,描点(AnchorPoint)的概念及其作用,以及触摸点如何在不同坐标系间进行转换。
Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系。就是数学里面的直角坐标系。右手法则坐标系。
  • 伸出右手
  • 大拇指、食指、中指做出直角坐标系的形状,对应关系为
  • 大拇指 -- X 轴
  • 食指 -- Y 轴
  • 中指 -- Z 轴
  • 当中指垂直屏幕向外,原点在屏幕坐下角就是Cocos2d-x 的坐标系统



世界坐标系(World Coordinate) VS 本地坐标系(Node Local)
  • 世界坐标系,听得好牛逼哦,但没有什么卵用,我不明白呀,猜想理解法:世界是指游戏世界,世界坐标系,就是游戏世界的坐标系,就是每一个Node 在游戏世界里面的坐标,合适的就是绝对坐标系
    • 使用世界二字,就是想告诉开发者,每一个游戏都是一个世界,这个世界的规则由开发者定。
  • 本地坐标,不用说了,就是相对坐标。每一个Node 都有独立的坐标系,有位置和方向,当Node 移动或改变方向,和该Node 关联的坐标系将随之移动或改变方向。

描点(Anchor Point)
  • 描:描绘,绘制,画。
  • 描点:绘制的起点,就是Node 的左下角。
    • [0,0] 那么绘制的起点和position 重合
    • [0.5,0.5] 那绘制的起点比position 早Node.size*0.5
    • [0.2,0.3] 那么绘制的起点比position 早[Node.size.width*0.2,Node.size.height*0.3]
    • [1,1] 那绘制的起点比position 早Node.size,相当于Node 的右上角和position 重合
    • Anchor Point的两个参数都在0~1之间。它们表示的并不是像素点,而是乘数因子。(0.5, 0.5)表示Anchor Point位于节点长度乘0.5和宽度乘0.5的地方,即节点的中心
    • 在Cocos2d-x中Layer的Anchor Point为默认值(0, 0),其他Node的默认值为(0.5, 0.5)。
  • Node
    • size = [100,100]
      • width = 100
      • height = 100
    • position = [100,100]
    • anchorPoint = [0.5,0.5]
  • ParentNode = Scene
  • 我们以Android 的形式分析一下这个Node,坐标系统Cocos2d-x
    • measure 侧量Node 的大小,这里对应的是size
    • layout 布局Node 的位置
      • position 得出相对Scene 的位置是[100,100]
      • anchorPoint 得出绘制的起点比position 早size*0.5 = [50,50]
      • 绘制的起点(Node 的左下角)[50,50]
        • 这个值是相对Scene
    • draw 绘制Node

  • 记住一点
    • Anchor Point and Size belong ChildNode
    • Position belong ParentNode
    • Anchor Point 是ChildNode 为了不让ParentChild 囚禁而出现
    • Size 是属于ChildNode自己
    • Position 是ParentNode 想囚禁ChildNode 而出现
    • Size、Anchor Point、Position ParentChild 都委托为ChildNode,这样ParentNode 只管把ChildNode加入进来就可以了,要怎么样显示ChildNode自己定吧
      • 很好的设计,委托了 
设置描点
  • ignoreAnchorPointForPosition(bool) -- 忽略Anchor Point
  • setAnchorPoint(Point(1, 1)); -- 设置Anchor Point

Z Coordinate -- VertexZ,PositionZ和zOrder

VerextZ是OpenGL坐标系中的Z值
  • PositionZ == VerextZ,两个坐标是一样的
  • 全局渲染顺序
PositionZ是Cocos2d-x坐标系中Z值
  • setPosition(Point(1,1))
  • PositionZ == VerextZ,两个坐标是一样的
zOrder是Cocos2d-x本地坐标系中Z值
  • this->addChild(red, 0); -- zOrder = 0
  • this->addChild(green, 1); -- zOrder = 1
  • 局部渲染顺序

同样节点的PositionZ也是决定了该节点的渲染顺序,值大的渲染在上层,但是与zOrder不同的区别在于,PositionZ是全局渲染顺序即在根节点上的渲染顺序,而zOrder则是局部渲染顺序,即该节点在其父节点上的渲染顺序,与Node的层级有关。用以下事例来说明:

触摸点(Touch Position)

在处理触摸事件时,我们需要重写以下四个函数
virtual bool onTouchBegan(Touch *touch, Event * event);
virtual void onTouchEnded(Touch *touch, Event * event);
virtual void onTouchCancelled(Touch *touch, Event * event);
virtual void onTouchMoved(Touch *touch, Event * event);
这个时候我们需要讲Touch 的坐标转换为Cocos2d坐标系中的坐标
  • Touch 坐标使用的是屏幕坐标系
// returns the current touch location in OpenGL coordinates
Vec2 Touch::getLocation() const
{
return Director::getInstance()->convertToGL(_point);
}
// returns the current touch location in screen coordinates
Vec2 Touch::getLocationInView() const
{
return _point;
}

常用接口
  • 函数声明在Node 类里面,所以大家都可以用Scene、Layer、Sprite
  • 想象Cocos 世界只有一个Node,就像自己一个在宇宙游荡,为自己去一个名字就OnlyNode,这样Cocos 就只有两个东西,Cocos世界和OnlyNode
    • 有一个天OnlyNode 看到自己的原点Point(0,0)突然发出一道闪光
    • 一瞬间闪光跑到了OnlyNode 坐标系的Point(10,10)位置
    • 过了一秒钟后,闪光跑到了Cocos坐标系的Point(10,10)位置
    • OnlyNode不知道自己在哪里,更加无法找到Cocos坐标系的Point(10,10)在哪里,OnlyNode 自己很孤独想找到这个闪光在哪里。
    • 问题1:求闪光第一次出现在Cocos坐标系的坐标。
    • 问题2:求闪光第一次出现在Cocos坐标系的坐标。
    • 问题3:求闪光过了一分钟后,闪光在OnlyNode 坐标系的哪个坐标。
    • 闪光游走了一个晚上,第二天,OnlyNode还没有找到闪光,OnlyNode使用了AnchorPoint,把AnchorPoint设置为了Point(1,1) 希望能找到闪光,果真,设置完之后闪光再次出现在OnlyNode 坐标系的Point(10,10)位置
    • 一瞬间闪光跑到了OnlyNode 坐标系的Point(10,10)位置
    • 过了一秒钟后,闪光不见了。OnlyNode很希望,希望闪光能再次回来
    • 问题4:求设置AnchorPoint后,闪光出现在Cocos坐标系的坐标。
    • 问题5:你闪光消失了,闪光最大可能跑去哪里了?
  • 解:
    • 问题1:OnlyNode->convertToWorldSpace(Point(0,0));
    • 问题2:OnlyNode->convertToWorldSpace(Point(10,10));
    • 问题3:OnlyNode->convertToNodeSpace(Point(10,10));
    • 问题4:OnlyNode->convertToWorldSpaceAR(Point(0,0));
    • 问题5:这是一个笛卡尔坐标系游戏,只要OnlyNode在这里等就可以了,明天天黑就可以再次看到闪光。
      • OnlyNode->convertToWorldSpaceAR(Point(0,0));
// 把世界坐标转换到当前节点的本地坐标系中
Point convertToNodeSpace(const Point& worldPoint) const;
 
// 把基于当前节点的本地坐标系下的坐标转换到世界坐标系中
Point convertToWorldSpace(const Point& nodePoint) const;
 
// 基于Anchor Point把基于当前节点的本地坐标系下的坐标转换到世界坐标系中
Point convertToNodeSpaceAR(const Point& worldPoint) const;
 
// 基于Anchor Point把世界坐标转换到当前节点的本地坐标系中
Point convertToWorldSpaceAR(const Point& nodePoint) const;

开发者给出的代码from ptm-pose import project my_splice_data_annotated, spliced_ptms = project.project_ptms_onto_splice_events(my_splice_data, ptm_coordinates, chromosome_col = 'chromosome', strand_col = 'strand', region_start_col = 'region_start', region_end_col = 'region_end', event_id_col = 'event_id', gene_col = 'Gene name', dPSI_col='dPSI', coordinate_type = 'hg19') 运行后显示 my_splice_data_annotated, spliced_ptms = project.project_ptms_onto_splice_events(my_splice_data, ptm_coordinates, ^^^^^^^^^^^^^^^ NameError: name 'ptm_coordinates' is not defined 打开project.py文件找到 ptm_coordinates: pandas.DataFrame dataframe containing PTM information, including chromosome, strand, and genomic location of PTMs SE_events: pandas.DataFrame dataframe containing skipped exon event information from MATS A5SS_events: pandas.DataFrame dataframe containing 5' alternative splice site event information from MATS A3SS_events: pandas.DataFrame dataframe containing 3' alternative splice site event information from MATS RI_events: pandas.DataFrame dataframe containing retained intron event information from MATS MXE_events: pandas.DataFrame dataframe containing mutually exclusive exon event information from MATS coordinate_type: str indicates the coordinate system used for the start and end positions. Either hg38 or hg19. Default is 'hg38'. dPSI_col: str Column name indicating delta PSI value. Default is 'meanDeltaPSI'. sig_col: str Column name indicating significance of the event. Default is 'FDR'. extra_cols: list List of column names for additional information to add to the results. Default is None. separate_modification_types: bool Indicate whether residues with multiple modifications (i.e. phosphorylation and acetylation) should be treated as separate PTMs and be placed in unique rows of the output dataframe. Default is False. PROCESSES: int Number of processes to use for multiprocessing. Default is 1. 请问是我的输入有问题还是需要重新定义ptm_coordinates
最新发布
07-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值