Ubuntu cocos2d-x 3.13版本游戏开发学习系列3 Cocos2d-x的坐标系
1.UI坐標(biāo)和OpenGL坐標(biāo)
Cocos2d-x默認(rèn)坐標(biāo)為OpenGL坐標(biāo).
2.世界坐標(biāo)和模型坐標(biāo)
在游戲里面,世界坐標(biāo)就是相當(dāng)于整塊屏幕原點(diǎn)的坐標(biāo).也就是以屏幕的左下角為原點(diǎn)的OpenGL坐標(biāo)系.
模型坐標(biāo)就是相對于屏幕里面的某個(gè)精靈的坐標(biāo)系,以精靈所在的位置為原點(diǎn),向右為X軸正方向,向上為Y軸正方向.
3.4個(gè)API的解釋
(1) Vec2 convertToNodeSpace(const Vec2& worldPoint). 將世界坐標(biāo)轉(zhuǎn)換為模型坐標(biāo)
(2) Vec2 convertToNodeSpaceAR(const Vec2& worldPoint) 將世界坐標(biāo)轉(zhuǎn)換為模型坐標(biāo),AR代表錨點(diǎn)
(3) Vec2 converToWorldSpace(const Vec2& nodePonit) 將模型坐標(biāo)轉(zhuǎn)換為世界坐標(biāo)
(4) Vec2 convertTpWorldSpaceAR(const Vec2& nodePoint) 將模型坐標(biāo)轉(zhuǎn)換為世界坐標(biāo).AR表示錨點(diǎn)
代碼演示如下:
#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();///創(chuàng)建Nodel1 寬度為300,高度為100.auto nodel1 = Sprite::create("node1.png");nodel1->setPosition(Point(400, 500));nodel1->setAnchorPoint(Point(0.5, 0.5));this->addChild(nodel1);//創(chuàng)建Nodel2 寬度為300,高度為100auto nodel2 = Sprite::create("node2.png");nodel2->setPosition(Point(300, 300));nodel2->setAnchorPoint(Point(1, 0));this->addChild(nodel2);//這個(gè)是以node1的原點(diǎn)生成的坐標(biāo)系,即node1左下角為原點(diǎn).auto nsPos = nodel1->convertToNodeSpace(nodel2->getPosition());//AR代表錨點(diǎn)的意思,這個(gè)是以node1的錨點(diǎn)為原點(diǎn).這個(gè)已經(jīng)在代碼里設(shè)置成了其中點(diǎn).精靈默認(rèn)錨點(diǎn)也是其中點(diǎn)auto nsArPos = nodel1->convertToNodeSpaceAR(nodel2->getPosition());log("模型2相當(dāng)于模型1原點(diǎn)坐標(biāo)的距離是:%f,%f", nsPos.x, nsPos.y);log("模型2相當(dāng)于模型1錨點(diǎn)坐標(biāo)的距離,看來是:%f,%f", nsArPos.x, nsArPos.y);//創(chuàng)建Nodel3 寬度為58,高度為59auto nodel3 = Sprite::create("orange.png");nodel3->setPosition(Point(100, 100));nodel3->setAnchorPoint(Point(1, 0));//吧node3添加到node1上去了,添加上去默認(rèn)就是以node1左下角為原點(diǎn),顯然此時(shí)node3將不可見,因?yàn)橐呀?jīng)抄錯(cuò)了nodel1->addChild(nodel3);log("node3相對于node1的坐標(biāo)是:{%f,%f}", nodel3->getPosition().x, nodel3->getPosition().y);auto ns13 = nodel1->convertToNodeSpace(nodel3->getPosition());log("node3相對于node1的坐標(biāo)是錯(cuò)誤做法:{%f,%f}", ns13.x, ns13.y); //這個(gè)也能看出其實(shí)轉(zhuǎn)化坐標(biāo)系其實(shí)是取node3的坐標(biāo)與node1的距離//總結(jié): 1.[node]->convertToNodeSpace 傳入的 [node]世界里面其他的node坐標(biāo),來求相對于[node]的距離// 2.[node]->convertToNodeSpaceAR是不在以[node]模型的原點(diǎn)為坐標(biāo),而是以其錨地為坐標(biāo).auto ws13 = nodel1->convertToWorldSpace(nodel3->getPosition());auto wsAr13 = nodel1->convertToWorldSpaceAR(nodel3->getPosition());log("node3相對于node1的坐標(biāo)在世界坐標(biāo)系的表示:{%f,%f}", ws13.x, ws13.y); //這個(gè)也能看出其實(shí)轉(zhuǎn)化坐標(biāo)系其實(shí)是取node3的坐標(biāo)與node1的距離log("node3相對于node1(錨點(diǎn))的坐標(biāo)在世界坐標(biāo)系的表示:{%f,%f}", wsAr13.x, wsAr13.y); //這個(gè)也能看出其實(shí)轉(zhuǎn)化坐標(biāo)系其實(shí)是取node3的坐標(biāo)與node1的距離return true; }結(jié)果為:
模型2相當(dāng)于模型1原點(diǎn)坐標(biāo)的距離是:50.000000,-150.000000 模型2相當(dāng)于模型1錨點(diǎn)坐標(biāo)的距離,看來是:-100.000000,-200.000000 node3相對于node1的坐標(biāo)是:{100.000000,100.000000} node3相對于node1的坐標(biāo)是錯(cuò)誤做法:{-150.000000,-350.000000} node3相對于node1的坐標(biāo)在世界坐標(biāo)系的表示:{350.000000,550.000000} node3相對于node1(錨點(diǎn))的坐標(biāo)在世界坐標(biāo)系的表示:{500.000000,600.000000}轉(zhuǎn)載于:https://www.cnblogs.com/soongkun/p/6027248.html
總結(jié)
以上是生活随笔為你收集整理的Ubuntu cocos2d-x 3.13版本游戏开发学习系列3 Cocos2d-x的坐标系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DOM_06之定时器、事件、cookie
- 下一篇: 如何判断ios设备中是否安装了某款应用