iPhone:OpenGL ES:检测您是否在屏幕上点击了一个对象(立方体) [英] iPhone : OpenGL ES : Detecting if you have tapped a object (cube) on screen

查看:123
本文介绍了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,但没有成功(请参见我的其他帖子)

解决方案

您正在讨论的概念(通过2D坐标在3D屏幕上选择对象)称为拾取. >

有关一些解决方案,请参见我的较早帖子:

OpenGL GL_SELECT还是手动碰撞检测?

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)

解决方案

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?

这篇关于iPhone:OpenGL ES:检测您是否在屏幕上点击了一个对象(立方体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆