如何在Android中的特定渲染器区域中检测触摸事件? [英] How to detect touch events within a certain Renderer-area in android?

查看:90
本文介绍了如何在Android中的特定渲染器区域中检测触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android中,我以在此处给出的为例.它创建了一个显示旋转球体(地球)的简单应用.

In android I have taken a rotating sphere example given here. It creates a simple app showing a rotating sphere (the earth).

现在,如果要在电话显示屏上按下旋转球体,我想做些事情.我不想对旋转球的外部触摸事件做出反应.到目前为止,我已经尝试过使用GLSurfaceView的派生版本:

Now, I want to do something if the rotating sphere is pressed on the phone display. I do not want to react to touch events outside of the rotating sphere. Here is what I have tried so far, by using a derived version of the GLSurfaceView:

public class MyGLSurfaceView extends GLSurfaceView {



    MyGLSurfaceView(Context context) {
        super(context);
    }

    public boolean onTouchEvent(MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        Log.d("onTouchEvent",String.format("keyCode: %d  coords: %d  %d", event.getActionMasked(), x, y));

        Renderer renderer = this.Renderer.getRenderer(); // ERROR
        return super.onTouchEvent(event);
    }

}

没有ERROR行,此代码有效,当我按显示器时,我得到了x/y坐标.

Without the ERROR line this code works, and when I press the display I get the x/y coordinate.

但是,为了评估事件是否在球体内部,我的方法是获取具有Sphere对象的渲染器,以确定x/y坐标是否在球体内部.

However, in order to evaluate if the event was within the sphere, my approach was to get the renderer which has the Sphere object to determine if the x/y coordinates are within the sphere.

但是ERROR行不起作用,我收到一条错误消息

But the ERROR line does not work, I get an error saying

Error:(26, 56) error: cannot find symbol method getRenderer()

如何解决此错误;还是有一种更明智的方式自动发现事件是否在Sphere -graphics-object上?

How can I fix this error; or is there a more sane way to find out automatically if the event was on the Sphere-graphics-object?

也许更改构造函数并将渲染器实例也传递给MyGLSurfaceView是个好主意吗?附录:这个想法似乎行不通.我收到一个我不明白的构造函数错误...

Maybe it is a good idea to change the constructor and pass the renderer instance to MyGLSurfaceView as well? ADDENDUM: This idea does not seem to work. I get a constructor error I do not understand...

推荐答案

您的问题比仅检测触摸事件要复杂得多.渲染器不仅具有sphere对象,还包含整个OpenGL ES内容.因此,即使在检测到触摸事件之后,也需要将屏幕的2D坐标转换为3D射线,并检查其是否与球体相交.这称为射线投射. 此处是有关射线投射算法的很好的解释.还有一个好的视频教程,用于实现射线投射Java.

Your question is much more complex than just detecting touch events. The renderer doesn't only have the sphere object, it holds the entire OpenGL ES content. So even after you detect your touch events, you will need to convert your screen 2D coordinates to 3D ray and check if it intersects with the sphere. This is called ray casting. Here is a nice explanation of the ray casting algorithms. Also there is a good video tutorial for implementing ray casting Java.

此外,您将希望按照您和@ satm12的建议在GLSurfaceView的构造函数中传递渲染器.

Also you will want to pass the renderer in the constructor of the GLSurfaceView as you and @satm12 suggested.

这篇关于如何在Android中的特定渲染器区域中检测触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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