如何判断您是否正在触摸屏幕上的对象

如何判断您是否正在触摸屏幕上的对象

本文介绍了Opengl Iphone SDK:如何判断您是否正在触摸屏幕上的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是我的touchesBegan函数,然后是存储我的对象的值的结构.我有这些对象的数组,如果我触摸屏幕上的一个对象,我会尝试找出触摸屏幕的时间.

First is my touchesBegan function and then the struct that stores the values for my object. I have an array of these objects and I'm trying to figure out when I touch the screen if I'm touching an object on the screen.

我不知道是否需要遍历所有对象并确定我是否正在以这种方式触摸对象,或者是否有更简单,更有效的方法来做到这一点.

I don't know if I need to do this by iterating through all my objects and figure out if I'm touching an object that way or maybe there is an easier more efficient way.

通常如何处理?

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [super touchesEnded:touches withEvent:event];
    UITouch* touch = ([touches count] == 1 ? [touches anyObject] : nil);

    CGRect bounds = [self bounds];
    CGPoint location = [touch locationInView:self];
    location.y = bounds.size.height - location.y;


    float xTouched = location.x/20 - 8 + ((int)location.x % 20)/20;
    float yTouched = location.y/20 - 12 + ((int)location.y % 20)/20;
 }



typedef struct object_tag             // Create A Structure Called Object
{
    int   tex;      // Integer Used To Select Our Texture
    float x;        // X Position
    float y;        // Y Position
    float z;        // Z Position
    float yi;       // Y Increase Speed (Fall Speed)
    float spinz;    // Z Axis Spin
    float spinzi;   // Z Axis Spin Speed
    float flap;     // Flapping Triangles :)
    float fi;       // Flap Direction (Increase Value)
} object;

推荐答案

有几种方法可以做到这一点.

There are several ways to do this.

  1. 您可以对所有使用独特颜色纯色绘制的对象执行额外的场景绘制,然后使用glReadPixels读取所选像素的颜色并将其与对象关联.

  1. You can perform extra draw of the scene with all objects drawn solid using unique color, then read color of the selected pixel using glReadPixels and associate it with object.

使用类似 unproject 之类的东西进入-场景射线,然后找到射线与三角形的交点.可以在 glm 库中找到非投影函数以及ray-triangle相交函数,它非常适合open gl开发.

Use something like unproject to get in-scene ray, and then find ray-triangle intersection. Unproject function as well as ray-triangle intersection function can be found in glm library which is perfectly suits open gl development.

这篇关于Opengl Iphone SDK:如何判断您是否正在触摸屏幕上的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:35