简体   繁体   English

在“触摸平面”和“游戏平面”之间转换

[英]Translate between 'Touch Plane' and 'Game Plane'

I am trying to create a 2D game. 我正在尝试创建2D游戏。 Because I am using OpenGL ES I have to plot everything in 3D, but I just fix the z coordinate, which is fine. 因为我使用的是OpenGL ES,所以我必须以3D形式绘制所有内容,但是我只修复了z坐标,这很好。 Now what I want to do is calculate the angle between two vectors (C = player center, P = point just above player, T = touch point) CP and CT so that I can make the player face that direction. 现在我要做的是计算两个向量之间的角度(C =玩家中心,P =玩家上方的点,T =接触点)CP和CT,这样我就可以使玩家面对该方向。 I know how to get the angle between 2 vectors, but my problem is getting all the points to exist on the same plane (by translating the T). 我知道如何获取2个向量之间的角度,但是我的问题是要使所有点都存在于同一平面上(通过平移T)。

I know that T exists on a plane where (0,0) is upper left and UP is actually DOWN (visually). 我知道T位于(0,0)在左上角而UP实际上在DOWN(从视觉上)的平面上。 I also know that C and P's UP is actually UP and that any their X and Y is on a completely 3 dimensional different plane to T. I need to get either C and P onto T's plane (which I have tried below) or get T onto C and P's plane. 我也知道C和P的UP实际上是UP,并且它们的X和Y都在与T完全3维的平面上。我需要将C和P放在T的平面上(我在下面尝试过)或得到T到C和P的平面上。 Can anyone help me? 谁能帮我? I am using the standard OpenGL projection model and I am 0,0,-4 zoomed out of the frustrum (I am looking directly at (0,0,0)). 我正在使用标准的OpenGL投影模型,并且我从视锥中缩小了0,0,-4(我直接看(0,0,0))。 My 2D objects all sit on the plane (0,0,1); 我的2D对象都位于平面(0,0,1)上;

private float getRotation(float touch_x, float touch_y)
{
    //center_x = this.getWidth() / 2;
    //center_y = this.getHeight() / 2;

    float cx, cy, tx, ty, ux, uy;

    cx = (player.x * _renderer.centerx);
    cy = (player.y * -_renderer.centery);

    ux = cx;
    uy = cy+1.0f;

    tx = (touch_x - _renderer.centerx);
    ty = (touch_y - _renderer.centery);

    Log.d(TAG, "center  x: "+cx+"y:"+cy);
    Log.d(TAG, "up      x: "+ux+"y:"+uy);
    Log.d(TAG, "touched x: "+tx+"y:"+ty);

    float P12 = length(cx,cy,tx,ty);
    float P13 = length(cx,cy,ux,uy);
    float P23 = length(tx,ty,ux,uy);

    return (float)Math.toDegrees(Math.acos((P12*P12 + P13*P13 - P23*P23)/2.0 * P12 * P13));
}

Basically I want to know if there is a way I can translate (tx, ty, -4) to (x, y, 1) using the standard view frustum. 基本上我想知道是否有一种方法可以使用标准视图视锥将(tx,ty,-4)转换为(x,y,1)。

I have tried some other things now. 我现在尝试了其他一些东西。 In my touch event I am trying to do this: 在触摸事件中,我正在尝试执行以下操作:

float[] coords = new float[4];
GLU.gluUnProject(touch_x, touch_y, -4.0f, renderer.model, 0, renderer.project, 0, renderer.view, 0, coords, 0);

Which is throwing an exception I am setting up the model, projection and view in the OnSurfaceChanged of the Renderer object: 我在Renderer对象的OnSurfaceChanged中设置模型,投影和视图,这引发了异常:

    GL11 gl11 = (GL11)gl;

    model = new float[16];
    project = new float[16]; 
    int[] view = new int[4];

    gl11.glGetFloatv(GL10.GL_MODELVIEW, model, 0);
    gl11.glGetFloatv(GL10.GL_PROJECTION, project, 0);
    gl11.glGetIntegerv(GL11.GL_VIEWPORT, view, 0);

I have several textbooks on openGL and after dusting one off I found that the term for what I want to do is called picking . 我有几本关于openGL的教科书,除掉其中的一本后,我发现我想做的一件事叫做“ 挑选” Once I knew what I was asking, I found a lot of good web sites and references: 知道自己要问的内容后,我发现了很多不错的网站和参考资料:

The list is almost innumerable. 这份清单几乎是无数的。 There are 700 ways to do this, and none of them worked for me. 有700种方法可以完成此操作,但没有一种方法对我有用。 Ultimately I have decided to go back to basics and do a thorough OpenGL|ES learning stint, to which effect I have bought the book here: http://www.amazon.com/Graphics-Programming-Android-Programmer-ebook/dp/B0070D83W2/ref=sr_1_2?s=digital-text&ie=UTF8&qid=1362250733&sr=1-2&keywords=opengl+es+2.0+android 最终,我决定回到基础知识,并进行全面的OpenGL | ES学习,为此,我在这里购买了该书: http : //www.amazon.com/Graphics-Programming-Android-Programmer-ebook/dp /B0070D83W2/ref=sr_1_2?s=digital-text&ie=UTF8&qid=1362250733&sr=1-2&keywords=opengl+es+2.0+android

One thing I have already learnt is that I was most definitely using the wrong type of projection. 我已经了解到的一件事是,我肯定使用了错误的投影类型。 I should not use full 3D for a 2D game. 我不应该在2D游戏中使用完整的3D。 In order to do picking in a full 3D environment I would have to cast a ray from the screen point onto the surface of the 3D plane where the game was taking place. 为了在完整的3D环境中进行拾取,我必须将光线从屏幕点投射到进行游戏的3D平面的表面上。 In addition to being a horrendous waste of resources (raycasting per click), there were other tell-tales. 除了浪费大量资源(每次点击进行光线广播)外,还有其他故事。 I would render my player with a circle encompassing her, and as I moved her, the circle would go off center of the player. 我将用一个包围着她的圆圈渲染我的玩家,并且当我移动她时,圆圈将偏离玩家的中心。 This is due to the full 3D environment rendered on a 2D plane. 这是由于在2D平面上渲染了完整的3D环境。 It just will not produce a professional result. 只是不会产生专业的结果。 I need to use an orthographic projection. 我需要使用正交投影。

I think you're trying to do too much all at once. 我认为您正在尝试一次做太多事情。 I can understand each sentence of your question separately; 我可以分别理解您问题的每个句子; but strung all together, it's very confusing. 但是串在一起,这很令人困惑。

For the exceptions, you probably need to pass identity matrices instead of zero matrices to get a basic 1-to-1 projection. 对于例外情况,您可能需要传递身份矩阵而不是零矩阵来获得基本的一对一投影。

Then I'd suggest that you scale the y dimension by -1 so all the UPs and DOWNs match at least. 然后,我建议您将y维度缩放-1,以便所有UP和DOWN至少匹配。

I hope this helps, because I'm not 100% sure what you're trying to do. 我希望这会有所帮助,因为我不确定100%是否要尝试做。 Particularly, " translate (tx, ty, -4) to (x, y, 1) using the standard view frustum" doesn't make sense to me. 特别是,“使用标准视图视锥将(tx,ty,-4)转换为(x,y,1)”对我来说没有意义。 You can translate with a translation matrix. 您可以使用翻译矩阵进行翻译。 You can clip to a view frustum, or project an object from the frustum to a plane (usually the view plane). 您可以剪切到视锥台,或将对象从视锥投影到平面(通常是视平面)。 But if all your Zs are constant, you can just discard them right? 但是,如果您所有的Z都是恒定的,您就可以丢弃它们吗? So, assuming x=tx and y=ty, then tz += 5 ? 因此,假设x = tx且y = ty,则tz += 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM