坐标系
Cocos2d 中 分有大致4个坐标系
player1 = [CCSprite spriteWithFile:@"Icon-72.png" rect:CGRectMake(0, 0, 72,72)];
player1.position = ccp(winWidth/2, winHeight/2);
[self addChild:player1 z:0];
player2 = [CCSprite spriteWithFile:@"Icon-Small-50.png" rect:CGRectMake(0, 0,50, 50)];
[player1 addChild:player2 z:0];
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [player1 convertToNodeSpace:touchLocation];
CCSprite *player = (CCSprite*)[self getChildByTag:77];
touchLocation = [player1 convertToWorldSpace:touchLocation];
NSLog(@"%f, %f", touchLocation.x, touchLocation.y);
然后点击图片后得到的坐标是 223,122
这就说明现在所在的坐标系是world space 或者说 opengl es 坐标系中 也就是 图片的 父级坐标系
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
如果直接想得到 用户是否点击到 精灵 或者图片上面的 本地坐标
再添加这条
touchLocation = [player1 convertToNodeSpace:touchLocation];
当然 用 一条代码 可以代替上面的 2条 省事的话
touchLocation = [player1 convertTouchToNodeSpace: touch];
——————————————————————————————————————————————————
// converting local node coordinates to world space
-(CGPoint) convertToWorldSpace:(CGPoint)nodePoint;
-(CGPoint) convertToWorldSpaceAR:(CGPoint)nodePoint;
// converting world coordinates to local node space
-(CGPoint) convertToNodeSpace:(CGPoint)worldPoint;
-(CGPoint) convertToNodeSpaceAR:(CGPoint)worldPoint;
// converting touch (world) coordinates to local node
space
-(CGPoint) convertTouchToNodeSpace:(UITouch*)touch;
-(CGPoint) convertTouchToNodeSpaceAR:(UITouch*)touch;
// convert a world point to node coordinate space
CGPoint localPoint = [someNode
convertToNodeSpace:worldPoint];
现在 我用player1 和 player2 , 把player2 的坐标系 换成 player1 的
再判断点击是不是在坐标系内
if(CGRectContainsPoint([player2
textureRect], touchLocation))
{
NSLog(@"Inside");
}else {
NSLog(@"Outside");
}
可以看到 player2 的 图片位于靠近原点的位置 但是它的坐标系已经转换为player1 的 local Node space coordinate。
可以看到 不管你怎么点都是 215, 135 的坐标 应为 player1的position 坐标 位于 player2 的本地节点坐标系(Local Node space coordinate)当中, 原点是 player2 的 原点 而不是屏幕左下角的原点
再点击player1 和 player2
控制台输出
下面给出了几个常用的坐标系转换的情况
// convert a local node point to world coordinate
space
CGPoint worldPoint = [someNode
convertToWorldSpace:localPoint];
// same as above, but localPoint is offset to be relative to
anchorPoint of someNode
CGPoint worldPointAR = [someNode
convertToWorldSpaceAR:localPoint];
本地坐标系转换到另外一个节点的坐标系当中(这里是父坐标系)
// convert a local node point to another node's coordinate
space
CGPoint targetNodeLocalPoint = [targetNode
convertToWorldSpace:otherNodeLocalPoint];
// same as above, but otherNodeLocalPoint is offset to be relative
to anchorPoint of targetNode
CGPoint targetNodeLocalPointAR = [targetNode
convertToWorldSpaceAR:otherNodeLocalPoint];
Normally (and unfortunately) cocos2d places child nodes relative to the parent's lower left contentSize corner. In the case of a sprite using a 100x100 image, and the sprite positioned at 400x300, a child node with a position of 0x0 will be centered on the coordinate 400-(100/2)x300-(100/2) = 350x250. This is odd but that's the way cocos2d works. The non-AR conversion coordinates convert coordinates relative to this origin point 350x250 whereas the AR variants convert coordinates relative to the node's anchorPoint, in this case relative to 400x300.
一般或者不幸的是 cocos2d 放置 子节点的位置 是相对 父节点的 左下角的位置这样的方式。 在这个例子里 使用100*100的图片, 精灵位置是 400,300, 一个子节点的位置是 0,0 应该是位于400-100/2 = 350,和300-100/2 = 250中心的地方. 这比较奇怪, 但这就是cocos2d 的方式。(按照一般思维 应该在原点 就是 400, 300 的地方 但是 父节点的锚点不是 0.5 , 0.5 而是 0,0)
The difference between non-AR and AR coordinate conversion methods is merely the distance between the node's lower left origin point and the anchorPointInPoints property. Should the anchorPoint of the node and all of its parents be set to 0x0 (not recommended btw) then both coordinate conversion methods would give you the same results.
这样锚地和 无锚点的坐标系之间转换 将出现很微小的误差, 应该把节点的锚点和所有它的父节点都设置为0,0 ,你将得到正确的结果.
CGPoint cocosPoint = [[CCDirector sharedDirector]
convertToGL:uikitPoint];
CGPoint uikitPoint = [[CCDirector sharedDirector]
convertToUI:cocosPoint];
CGPoint cocosPoint = [someNode
convertToWorldSpace:localPoint];
CGPoint uikitPoint = [[CCDirector sharedDirector]
convertToUI:cocosPoint];
反之
CGPoint cocosPoint = [[CCDirector sharedDirector]
convertToGL:uikitPoint];
CGPoint localPoint = [someNode
convertToNodeSpace:cocosPoint];
相关文章
- ROS1云课→15主题与坐标系
- 圆形目标中心点在tof相机坐标系下的三维坐标的计算(1)
- 使用坐标系分析Paxos算法
- 坐标系旋转变换公式图解[通俗易懂]
- 重新认识ArcGIS中的坐标系
- 笛卡尔坐标系,它结合了_笛卡尔坐标系的故事
- 3D打印机marlin固件自动调平设置和使用及方向及坐标系设置
- GPM 降雨量数据处理 -R(坐标系转换)
- OpenCV-Python实战(4) —— OpenCV 五角星各点在坐标系上面的坐标计算(以重心为原点)
- 使用metpy将台风数据插值转换为极坐标系
- Python+Matplotlib绘制半极坐标系折线图
- ENVI中不含地理参考信息的栅格图像添加地理或投影坐标系
- FreeJTS部标视频平台:车载坐标系与地图坐标系转换
- Flutter 绘制探索 | 操作坐标系范围
- 基于 ArcGIS 的坐标系转换
- 【Android 应用开发】Android 组件 位置坐标 属性 ( 组件位置属性 | 父容器坐标系坐标 | 窗口坐标系坐标 | 屏幕坐标系坐标 | 触摸坐标 )
- 【Android UI】Canvas 画布 ④ ( Canvas 坐标系 | Canvas 自身坐标系 | Canvas 绘图坐标系 )
- 【Android UI】Canvas 画布 ⑤ ( Canvas 坐标系 | Canvas 绘图坐标系变换示例 )
- 【Android UI】Canvas 画布 ⑧ ( Canvas 绘图坐标系 2x2 矩阵 | Canvas 绘图坐标系 3x3 操作矩阵 )
- 【Android UI】Canvas 画布 ⑨ ( Canvas 绘图坐标系平移实例 )
- 【Unity3D】世界坐标系 ( 左手坐标系 | 游戏物体坐标 | 世界坐标系方向 )
- 【Unity3D】3D 物体概念 ② ( 空物体概念 | 创建空物体 | 利用空物体管理多个子节点 | 世界坐标系和本地坐标系操作模式 | 切换坐标系操作模式 | 轴心和几何中心模式 )
- 【Echarts】极坐标系下的柱状图实现圆角环状进度条
- MySQL:帮助我们定义新的坐标系(坐标mysql)
- 统利用Oracle中的坐标系统改善定位精度(oracle中的坐标系)
- Shape对象与VML坐标系
- Cocos2d-x学习笔记之世界坐标系、本地坐标系、opengl坐标系、屏幕坐标系