Ubuntu cocos2d-x 3.13版本游戏开发学习系列3 Cocos2d-x的坐标系
生活随笔
收集整理的這篇文章主要介紹了
Ubuntu cocos2d-x 3.13版本游戏开发学习系列3 Cocos2d-x的坐标系
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.UI坐標和OpenGL坐標
Cocos2d-x默認坐標為OpenGL坐標.
2.世界坐標和模型坐標
在游戲里面,世界坐標就是相當于整塊屏幕原點的坐標.也就是以屏幕的左下角為原點的OpenGL坐標系.
模型坐標就是相對于屏幕里面的某個精靈的坐標系,以精靈所在的位置為原點,向右為X軸正方向,向上為Y軸正方向.
3.4個API的解釋
(1) Vec2 convertToNodeSpace(const Vec2& worldPoint). 將世界坐標轉換為模型坐標
(2) Vec2 convertToNodeSpaceAR(const Vec2& worldPoint) 將世界坐標轉換為模型坐標,AR代表錨點
(3) Vec2 converToWorldSpace(const Vec2& nodePonit) 將模型坐標轉換為世界坐標
(4) Vec2 convertTpWorldSpaceAR(const Vec2& nodePoint) 將模型坐標轉換為世界坐標.AR表示錨點
代碼演示如下:
#include "HelloWorldScene.h"USING_NS_CC;Scene *HelloWorld::createScene() {// 'scene' is an autorelease objectauto scene = Scene::create();// 'layer' is an autorelease objectauto layer = HelloWorld::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene; }// on "init" you need to initialize your instance bool HelloWorld::init() {//// 1. super init firstif (!Layer::init()) {return false;}auto visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();///創建Nodel1 寬度為300,高度為100.auto nodel1 = Sprite::create("node1.png");nodel1->setPosition(Point(400, 500));nodel1->setAnchorPoint(Point(0.5, 0.5));this->addChild(nodel1);//創建Nodel2 寬度為300,高度為100auto nodel2 = Sprite::create("node2.png");nodel2->setPosition(Point(300, 300));nodel2->setAnchorPoint(Point(1, 0));this->addChild(nodel2);//這個是以node1的原點生成的坐標系,即node1左下角為原點.auto nsPos = nodel1->convertToNodeSpace(nodel2->getPosition());//AR代表錨點的意思,這個是以node1的錨點為原點.這個已經在代碼里設置成了其中點.精靈默認錨點也是其中點auto nsArPos = nodel1->convertToNodeSpaceAR(nodel2->getPosition());log("模型2相當于模型1原點坐標的距離是:%f,%f", nsPos.x, nsPos.y);log("模型2相當于模型1錨點坐標的距離,看來是:%f,%f", nsArPos.x, nsArPos.y);//創建Nodel3 寬度為58,高度為59auto nodel3 = Sprite::create("orange.png");nodel3->setPosition(Point(100, 100));nodel3->setAnchorPoint(Point(1, 0));//吧node3添加到node1上去了,添加上去默認就是以node1左下角為原點,顯然此時node3將不可見,因為已經抄錯了nodel1->addChild(nodel3);log("node3相對于node1的坐標是:{%f,%f}", nodel3->getPosition().x, nodel3->getPosition().y);auto ns13 = nodel1->convertToNodeSpace(nodel3->getPosition());log("node3相對于node1的坐標是錯誤做法:{%f,%f}", ns13.x, ns13.y); //這個也能看出其實轉化坐標系其實是取node3的坐標與node1的距離//總結: 1.[node]->convertToNodeSpace 傳入的 [node]世界里面其他的node坐標,來求相對于[node]的距離// 2.[node]->convertToNodeSpaceAR是不在以[node]模型的原點為坐標,而是以其錨地為坐標.auto ws13 = nodel1->convertToWorldSpace(nodel3->getPosition());auto wsAr13 = nodel1->convertToWorldSpaceAR(nodel3->getPosition());log("node3相對于node1的坐標在世界坐標系的表示:{%f,%f}", ws13.x, ws13.y); //這個也能看出其實轉化坐標系其實是取node3的坐標與node1的距離log("node3相對于node1(錨點)的坐標在世界坐標系的表示:{%f,%f}", wsAr13.x, wsAr13.y); //這個也能看出其實轉化坐標系其實是取node3的坐標與node1的距離return true; }結果為:
模型2相當于模型1原點坐標的距離是:50.000000,-150.000000 模型2相當于模型1錨點坐標的距離,看來是:-100.000000,-200.000000 node3相對于node1的坐標是:{100.000000,100.000000} node3相對于node1的坐標是錯誤做法:{-150.000000,-350.000000} node3相對于node1的坐標在世界坐標系的表示:{350.000000,550.000000} node3相對于node1(錨點)的坐標在世界坐標系的表示:{500.000000,600.000000}轉載于:https://www.cnblogs.com/soongkun/p/6027248.html
總結
以上是生活随笔為你收集整理的Ubuntu cocos2d-x 3.13版本游戏开发学习系列3 Cocos2d-x的坐标系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DOM_06之定时器、事件、cookie
- 下一篇: 如何判断ios设备中是否安装了某款应用