2011-11-04 89 views
2

我正试图在Android中使用3D(OpenGL ES)实现“whack-a-mole”类型的游戏。现在,在任何给定时间我都有一个3D形状(旋转立方体),代表我的“鼹鼠”。我在我的视图中有一个触摸事件处理程序,它在我的渲染器中随机设置一些x,y值,导致立方体移动(使用glTranslatef())。在3D“场景”中处理触摸事件或屏幕到3D坐标

我还没有遇到任何教程或文档,完全桥接屏幕触摸事件到3D场景。我已经做了很多工作,以到达我所在的位置,但我似乎无法完成剩余的任务。

developer.andrdoid.com我用我猜可能被视为辅助类的矩阵:MatrixGrabber.javaMatrixStack.javaMatrixTrackingGL.java

我在我的GLU.glUnProject method中使用这些类,它应该做从真实屏幕坐标到3D或物体坐标的转换。

段:

MatrixGrabber mg = new MatrixGrabber(); 
    int viewport[] = {0, 0, renderer._width, renderer._height}; 
    mg.getCurrentModelView(renderer.myg); 
    mg.getCurrentProjection(renderer.myg); 
    float nearCoords[] = { 0.0f, 0.0f, 0.0f, 0.0f }; 
    float farCoords[] = { 0.0f, 0.0f, 0.0f, 0.0f }; 
    float x = event.getX(); 
    float y = event.getY(); 
    GLU.gluUnProject(x, y, -1.0f, mg.mModelView, 0, mg.mProjection , 0, viewport, 0, nearCoords, 0) 
    GLU.gluUnProject(x, y, 1.0f, mg.mModelView, 0, mg.mProjection , 0, viewport, 0, farCoords, 0) 

这段代码执行没有错误,将输出看起来不正确的。我知道屏幕左下角有原点(0,0)。而3D场景,至少是我的,似乎有一个经典的笛卡尔系统在屏幕中间的起源。因此,在屏幕坐标为(0,718)的位置触摸左下角,运行我的代码。我从去年参数gluUnProject输出:

靠近:{-2.544,2.927,2.839,1.99}

远:{0.083,0.802,-0.760,0.009}

这些数字不对我有意义。我的触摸甚至在第三象限,所以我所有的近距离和远距离的x,y值应该是负的,但它们不是。 gluUnProject文件没有提到任何需要转换屏幕坐标。然后再一次,相同的文档会让你相信Near和Far应该是3号的数组,但它们必须是4号,我没有CLUE的原因。

所以,我有两个问题(我敢肯定会有更多的问题出现)。

  1. 我怎样才能确保我得到适当的近和远的坐标基于屏幕坐标 。
  2. 一旦我有近和远的坐标,我如何使用它来查找它们创建的线是否与屏幕上的对象相交。

回答

2

我记得在我大学的时候,在Android上回到glUnProject问题。 (这是在Android的早期阶段)我的一位同学发现我们的计算会被glUnProject的结果中的第四维所破坏。如果我记得正确的话,这是某处记录的东西,但由于某种原因,我还没有能够再次挖掘。我从来没有深入了解它的具体细节,但也许帮助我们的东西也可能对你有用。这是有可能与我们应用数学做...

/** 
* Convert the 4D input into 3D space (or something like that, otherwise the gluUnproject values are incorrect) 
* @param v 4D input 
* @return 3D output 
*/ 
private static float[] fixW(float[] v) { 
    float w = v[3]; 
    for(int i = 0; i < 4; i++) 
     v[i] = v[i]/w; 
    return v; 
} 

我们实际使用上面的方法来解决了glUnProject结果,并做拾/触摸/选择在3D空间中的球形物体的动作。以下代码可能会提供如何执行此操作的指南。这不仅仅是投射射线并进行射线球面相交测试。

一些额外的注意事项,可能使下面的代码更容易理解:

  • Vector3f是基于3个浮点值的三维矢量的自定义实现,并实现了通常的向量运算。
  • shootTarget是3D空间中的球形物体。
  • 诸如getXZBoundsInWorldspace(0)getPosition(0)这样的调用中的0只是一个索引。我们实现了三维模型动画,索引决定了要返回的模型的“框架/姿态”。由于我们最终在非动画对象上执行了此特定命中测试,因此我们始终使用第一帧。
  • Concepts.wConcepts.h只是屏幕的宽度和高度(以像素为单位) - 或者对于全屏应用程序可能会有所不同:屏幕的分辨率。

_

/** 
* Checks if the ray, casted from the pixel touched on-screen, hits 
* the shoot target (a sphere). 
* @param x 
* @param y 
* @return Whether the target is hit 
*/ 
public static boolean rayHitsTarget(float x, float y) { 
    float[] bounds = Level.shootTarget.getXZBoundsInWorldspace(0); 
    float radius = (bounds[1] - bounds[0])/2f; 
    Ray ray = shootRay(x, y); 
    float a = ray.direction.dot(ray.direction); // = 1 
    float b = ray.direction.mul(2).dot(ray.near.min(Level.shootTarget.getPosition(0))); 
    float c = (ray.near.min(Level.shootTarget.getPosition(0))).dot(ray.near.min(Level.shootTarget.getPosition(0))) - (radius*radius); 

    return (((b * b) - (4 * a * c)) >= 0); 

} 

/** 
* Casts a ray from screen coordinates x and y. 
* @param x 
* @param y 
* @return Ray fired from screen coordinate (x,y) 
*/ 
public static Ray shootRay(float x, float y){ 
    float[] resultNear = {0,0,0,1}; 
    float[] resultFar = {0,0,0,1}; 

    float[] modelViewMatrix = new float[16]; 
    Render.viewStack.getMatrix(modelViewMatrix, 0); 

    float[] projectionMatrix = new float[16]; 
    Render.projectionStack.getMatrix(projectionMatrix, 0); 

    int[] viewport = { 0, 0, Concepts.w, Concepts.h }; 

    float x1 = x; 
    float y1 = viewport[3] - y; 

    GLU.gluUnProject(x1, y1, 0.01f, modelViewMatrix, 0, projectionMatrix, 0, viewport, 0, resultNear, 0); 
    GLU.gluUnProject(x1, y1, 50f, modelViewMatrix, 0, projectionMatrix, 0, viewport, 0, resultFar, 0); 
    //transform the results from 4d to 3d coordinates. 
    resultNear = fixW(resultNear); 
    resultFar = fixW(resultFar); 
    //create the vector of the ray. 
    Vector3f rayDirection = new Vector3f(resultFar[0]-resultNear[0], resultFar[1]-resultNear[1], resultFar[2]-resultNear[2]); 
    //normalize the ray. 
    rayDirection = rayDirection.normalize(); 
    return new Ray(rayDirection, resultNear, resultFar); 
} 

/** 
* @author MH 
* Provides some accessors for a casted ray. 
*/ 
public static class Ray { 
    Vector3f direction; 
    Vector3f near; 
    Vector3f far; 

    /** 
    * Casts a new ray based on the given direction, near and far params. 
    * @param direction 
    * @param near 
    * @param far 
    */ 
    public Ray(Vector3f direction, float[] near, float[] far){ 
     this.direction = direction; 
     this.near = new Vector3f(near[0], near[1], near[2]); 
     this.far = new Vector3f(far[0], far[1], far[2]); 
    } 
} 
1

我该如何确保根据屏幕坐标得到正确的近和远坐标。

如果您还没有在其他地方阅读过有关GLU.glProject的信息,请首先阅读this answerGLU.glUnProject做了那个函数的反函数,我发现它更容易理解,并有助于理解将屏幕坐标映射到对象空间的概念。至少为我工作。

如果你想测试你得到正确值从GLU.glUnProject这是最容易做到,如果你从一些易于理解的投影和模型视图矩阵开始。这是我今天早些时候使用的代码片段;

// Orthographic projection for the sake of simplicity. 
    float projM[] = new float[16]; 
    Matrix.orthoM(projM, 0, -ratio, ratio, 1f, -1f, zNear, zFar); 

    // Model-View matrix is simply a 180 degree rotation around z -axis. 
    float mvM[] = new float[16]; 
    Matrix.setLookAtM(mvM, 0, 0f, 0f, eyeZ, 0f, 0f, 0f, 0f, 1f, 0f); 
    Matrix.rotateM(mvM, 0, 180f, 0f, 0f, 1f); 

    // (0, 0) is top left point of screen. 
    float x = (width - (width/ratio))/2; 
    float y = height; 
    float objNear[] = new float[4]; 
    float objFar[] = new float[4]; 
    int view[] = { 0, 0 ,width, height }; 
    // --> objNear = { 1, 1, eyeZ - zNear } 
    GLU.gluUnProject(x, y, 0, mvM, 0, projM, 0, view, 0, objNear, 0); 
    // --> objFar = { 1, 1, eyeZ - zFar } 
    GLU.gluUnProject(x, y, 1, mvM, 0, projM, 0, view, 0, objFar, 0); 

对于更复杂的模型 - 视图和投影矩阵就变得相当困难,验证你要在物空间你希望坐标。这可能是一个很好的起点,可以先玩一些容易理解的矩阵。一旦你对结果满意,你根本不用担心GLU.glUnProject

一旦我有近及远的坐标,我怎么用它来找到,如果他们所创造的线与屏幕上的对象。

对于3D对象的空间命中测试,它应该是最简单的预先计算bounding spherebounding box为对象。一旦你想检查用户是否点击了一个对象,并且在对象空间中有两个屏幕点unProjected,则对象空间中的一条线上有两个点。

如果您使用的是边界球,则可以使用此线计算intersection points,或者也可以只计算球体中心点distance。在前一种情况下,至少应该有一个交点,后一个距离应该小于球半径。

对于边框this question是一个很好的阅读。

+0

谢谢您的回答和编辑。假设我的gluUnProject工作正常,我想了解更多有关如何使用坐标来检测碰撞的信息。我遇到的大多数资源都提示“雷霆万钧”,但实际上并没有提供任何关于如何“走线”的信息,以查看它是否与屏幕上的对象相交。也许这并不像我想要的那样困难,但是我已经为此做了一些工作,而且这里的资源也很差。 – Pete

+0

这个[Wolfram页面](http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html)很好地解释了与边界球体的相交。意思是,对于命中测试来说,计算球体中心点与该线的距离并将其与半径进行比较就足够了。 – harism