天天看點

cocos2dx進階學習之坐标轉換

在cocos2dx中,有四種坐标系

GL坐标系:左下為原點,x軸向右,y軸向上

UI坐标系:左上為原點,x軸向右,y軸向下

世界坐标系:與GL坐标系相同

本地坐标系:是節點(CCNode)的坐标系,原點在節點左下角,x軸向右,y軸向上

GL坐标和UI坐标轉換在導演類(CCDirector)中

CCPoint CCDirector::convertToGL(const CCPoint& obPoint);

CCPoint CCDirector::convertToUI(const CCPoint& obPoint);

世界坐标系和本地坐标系轉換在節點類(CCNode)中

CCPoint CCNode::convertToNodeSpace(const CCPoint& worldPoint);

CCPoint CCNode::convertToWorldSpace(const CCPoint& nodePoint);

CCPoint CCNode::convertToNodeSpaceAR(const CCPoint& worldPoint);

CCPoint CCNode::convertToWorldSpaceAR(const CCPoint& nodePoint);

UI坐标系用在觸摸事件的參數中,在重載

virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event)

函數時,touch中攜帶的坐标就是UI坐标系,當用來判斷某個精靈是否被點選時,需要将touch轉為GL坐标系

世界坐标系和GL坐标系是一樣的,在cocos2dx中用這種坐标系最多