我正在尝试创建2D游戏。因为我使用的是OpenGL ES,所以我必须以3D形式绘制所有内容,但我只是修正了z坐标,这很好。现在我想要做的是计算两个向量之间的角度(C =玩家中心,P =玩家正上方的点,T =触点)CP和CT,这样我可以让玩家面对那个方向。我知道如何获得2个向量之间的角度,但是我的问题是让所有点存在于同一个平面上(通过翻译T)。“触摸平面”和“游戏平面”之间的翻译
我知道T存在于(0,0)是左上角并且UP实际上是下(在视觉上)的平面上。我也知道C和P的UP实际上是UP的,并且它们的X和Y都在与T完全不同的三维平面上。我需要将C和P放到T的平面上(我在下面试过)或者得到T到C和P的飞机上。谁能帮我?我使用的是标准的OpenGL投影模型,我是0,0,-4从frustrum缩小(我直接看着(0,0,0))。我的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));
}
基本上我想知道是否有一种方法可以平移(TX,TY,-4)到(X,Y,1)使用标准的视锥。
我已经尝试了一些其他的东西。在我的触摸事件,我想这样做:
float[] coords = new float[4];
GLU.gluUnProject(touch_x, touch_y, -4.0f, renderer.model, 0, renderer.project, 0, renderer.view, 0, coords, 0);
这是抛出一个异常,我设立在渲染对象的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);
[处理三维场景中的触摸事件或屏幕到三维坐标](http://stackoverflow.com/questions/8014165/handling-touch-events-in-a-3d-scene-or -screen-to-3d-coordinates) – mbeckish 2013-03-01 15:28:25
我也一直在这里找,没有找到我的代码不应该工作的原因:http://stackoverflow.com/questions/8010971/how-to-move-a-opengl -square-the-the-finger – 2013-03-01 20:11:24