iPhone:OpenGL ES:检测您是否点击了屏幕上的对象(立方体)
我已经问了一个类似的问题,这让我达到了现在的水平,但我真的需要一些帮助。这是我完成一些很酷的事情的最后一件事(在我看来哈哈)
我有一个 3D 世界,我可以在其中移动,在这个世界中是简单的立方体。
使用函数 -(CGPoint)getScreenCoorOfPoint:(IMPoint3D)_point3D 我可以计算出这些立方体在 3D 世界中的 X、Y 位置。但它不是基于我所在的位置,而是基于他们在 3d 区域中的位置。使用相同的功能,我还可以计算出我所在的位置。我还可以找出有人点击了屏幕的位置。
我到底如何将它们映射在一起,以便我可以确定我是否单击了其中一个?
当然,我需要一些东西来确定我面对的方向。人们建议将它们渲染出屏幕并做一些事情,但这完全超出了我的想象。我考虑废弃该功能并基于 3d 坐标构建自己的功能(以某种方式)
代码
for (NSDictionary *item in self.players)
{
int x;
int z;
x = [[item objectForKey:@"posX"] floatValue];
z = [[item objectForKey:@"posZ"] floatValue];
IMPoint3D playerPos;
playerPos.x = x;
playerPos.z = z;
playerPos.y = 1;
CGPoint screenPositionOfThisCube ;
screenPositionOfThisCube = [self getScreenCoorOfPoint:playerPos];
#define TUNE 28
CGRect fingerSquish = CGRectMake(
screenPositionOfThisCube.x - TUNE,
screenPositionOfThisCube.y - TUNE,
TUNE * 2,
TUNE * 2);
// now check that the POINT OF THE TOUCH
// is inside the rect fingerSquish
if ( CGRectContainsPoint( fingerSquish, pos ) )
{
NSLog(@"WOOP");
// YOU HAVE FOUND THE TOUCHED CUBEY.
// YOU ARE DONE. this item is the cube being touched.
// make the cube change color or something to test it.
}
}
我也尝试了 gluUnproject 但没有成功(请参阅我的其他帖子)
I asked a similar question already which got me to where I am now but I really need some help on this one. Its the last thing in my way to completing something cool (in my eyes lol)
I have a 3d world where I can move around and in this world are simple cubes.
Using the function -(CGPoint)getScreenCoorOfPoint:(IMPoint3D)_point3D I can work out where these cubes are in X,Y within the 3D world. But its not based on where I am but where they are with the 3d Area. Using the same function, I can also work out where I am. Also I can find out where someone has tapped on the screen.
How on earth do I map these together so that I can work out if I have clicked one of them?
Surely I need something to work out which way I am facing. People have suggested rendering them off screen and doing something but it went completely over my head. I think of scrapping the function about and building my own based on the 3d coords (somehow)
Code
for (NSDictionary *item in self.players)
{
int x;
int z;
x = [[item objectForKey:@"posX"] floatValue];
z = [[item objectForKey:@"posZ"] floatValue];
IMPoint3D playerPos;
playerPos.x = x;
playerPos.z = z;
playerPos.y = 1;
CGPoint screenPositionOfThisCube ;
screenPositionOfThisCube = [self getScreenCoorOfPoint:playerPos];
#define TUNE 28
CGRect fingerSquish = CGRectMake(
screenPositionOfThisCube.x - TUNE,
screenPositionOfThisCube.y - TUNE,
TUNE * 2,
TUNE * 2);
// now check that the POINT OF THE TOUCH
// is inside the rect fingerSquish
if ( CGRectContainsPoint( fingerSquish, pos ) )
{
NSLog(@"WOOP");
// YOU HAVE FOUND THE TOUCHED CUBEY.
// YOU ARE DONE. this item is the cube being touched.
// make the cube change color or something to test it.
}
}
I also trying out gluUnproject with no success (see my other post)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在讨论的概念(通过 2D 坐标在 3D 屏幕上选择对象)称为拾取。
请参阅我的旧帖子以获取一些解决方案:
你如何解决这个opengl必要性(在c中)涉及知道用户点击了棋盘游戏中的哪个方格?
OpenGL GL_SELECT 还是手动碰撞检测?
The notion which you're discussing (selecting object on a 3D screen by 2D coords) is referred to as picking.
See my older posts for some solutions:
How would you solve this opengl necessity (in c) involving knowing in which square in a boardgame did the user click?
OpenGL GL_SELECT or manual collision detection?
我以为我发布了主课
I thought I post the main class
我认为这看起来很有希望
http://blog.nova -box.com/2010/05/iphone-ray-picking-glunproject-sample.html#comment-form
I think this might look promising
http://blog.nova-box.com/2010/05/iphone-ray-picking-glunproject-sample.html#comment-form
在 Kos 链接的一篇文章中,他反对现代 GL 实现的 GL_SELECT,但也许对于 OpenGL ES,这种选择正是您想要的应用程序 - 在 OpenGL 中执行此操作似乎合理?我读过它,但自己还没有使用它: http://www.glprogramming.com/ red/chapter13.html
干杯,S
In one of the posts linked to by Kos he argues against GL_SELECT for modern GL implementations, but maybe for OpenGL ES this kind of selection is what you want for this application - seems reasonable to do this within OpenGL? I read about it but haven't used it myself yet: http://www.glprogramming.com/red/chapter13.html
Cheers, S