在Layer中添加几个控件,如图中的格子,创建一个camera,camera和Layer输入同一父控件层,对camera进行setRotation3D(35,0,0)后,怎么获取格子在屏幕中的坐标?
好熟悉的背影,你用的是cocos2d-x哪个版本
游戏王???
我是想得到格子对应的屏幕坐标,我想在格子上面(相对与屏幕)加ui的控件。ui控件我用另一个正交投影的camera。
3.9的版本
不是啊。。。
自己网上扒的背景
引擎好像没有现成的接口,大概的步骤是
- 得到格子的世界坐标p
- p * view矩阵 * projection矩阵
- (p.x * 屏幕宽, p.y * 屏幕高)
void GameLayer::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
for (auto &item : touches)
{
//获取触碰坐标
auto touch = item;
//将触碰坐标转换为屏幕坐标
auto location = touch->getLocationInView();
//定义了两个远近点坐标,其实就是相机屏幕触摸点前后一个像素的两个点。
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
//把指定坐标点从屏幕坐标转换为世界坐标。
camera->unproject(WINSIZE, &nearP, &nearP);
camera->unproject(WINSIZE, &farP, &farP);
//定义一个远近两个点的方向向量。
Vec3 dir(farP - nearP);
float dist = 0.0f;
//取y方向上的差值,个人感觉取x方向上的差值效果一样。都是为了之后算比例。
float ndd = Vec3::dot(Vec3(0, 0, 1), dir);
if (ndd == 0)
dist = 0.0f;
float ndo = Vec3::dot(Vec3(0, 0, 1), nearP);
/*
因为
dir.y = farP.y - nearP.y
ndo.y = nearP.y
ndd = dir.y
所以,dist = (0 - ndo) / ndd = -nearP.y/(farP.y - nearP.y)
*/
dist = (0 - ndo) / ndd;
//计算比例,通过这种方式可以降低误差。
Vec3 p = nearP + dist * dir;
nowLocation = Vec2(p.x, p.y);
当时我用低版本的cocos2d-x v3.3,貌似没有Plane planeZ10(Vec3(0, 0, 1), Vec3(0, 0, 10));不过还是谢谢,因为你的代码,我才在引擎测试源码当中找到需要的代码,谢谢
有办法实现 节点坐标到屏幕坐标 的转化么
最终的解决方案是什么?