android-在libgdx上将起点设置为左上角

android-在libgdx上将起点设置为左上角,android,camera,pivot,point,libgdx,Android,Camera,Pivot,Point,Libgdx,我想使用libgdx作为基于屏幕纵横比的应用程序缩放解决方案。 我找到了这个链接,我觉得它非常有用: 我对opengl已经有好几年没有写过了,我希望在这个链接上使用这个例子,这样就可以很容易地放置图像和形状。 可悲的是,起点在中间。我想像在其他平台上一样使用它-左上角应该是0,0,右下角应该是targetWidth-1,targetRight-1, 根据我的记忆,我需要移动、平移和旋转相机才能实现它,但我不确定 下面是我对onCreate方法的链接示例的修改代码: @Override

我想使用libgdx作为基于屏幕纵横比的应用程序缩放解决方案。 我找到了这个链接,我觉得它非常有用:

我对opengl已经有好几年没有写过了,我希望在这个链接上使用这个例子,这样就可以很容易地放置图像和形状。 可悲的是,起点在中间。我想像在其他平台上一样使用它-左上角应该是0,0,右下角应该是targetWidth-1,targetRight-1, 根据我的记忆,我需要移动、平移和旋转相机才能实现它,但我不确定

下面是我对onCreate方法的链接示例的修改代码:

    @Override
public void create()
  {
  camera=new OrthographicCamera(VIRTUAL_WIDTH,VIRTUAL_HEIGHT);
  // camera.translate(VIRTUAL_WIDTH/2,VIRTUAL_HEIGHT/2,0);
  // camera.rotate(90,0,0,1);
  camera.update();
  //
  font=new BitmapFont(Gdx.files.internal("data/fonts.fnt"),false);
  font.setColor(Color.RED);
  //
  screenQuad=new Mesh(true,4,4,new VertexAttribute(Usage.Position,3,"attr_position"),new VertexAttribute(Usage.ColorPacked,4,"attr_color"));
  Point bottomLeft=new Point(0,0);
  Point topRight=new Point(VIRTUAL_WIDTH,VIRTUAL_HEIGHT);
  screenQuad.setVertices(new float[] {//
      bottomLeft.x,bottomLeft.y,0f,Color.toFloatBits(255,0,0,255),//
          topRight.x,bottomLeft.y,0f,Color.toFloatBits(255,255,0,255),//
          bottomLeft.x,topRight.y,0f,Color.toFloatBits(0,255,0,255),//
          topRight.x,topRight.y,0f,Color.toFloatBits(0,0,255,255)});
  screenQuad.setIndices(new short[] {0,1,2,3});
  //
  bottomLeft=new Point(VIRTUAL_WIDTH/2-50,VIRTUAL_HEIGHT/2-50);
  topRight=new Point(VIRTUAL_WIDTH/2+50,VIRTUAL_HEIGHT/2+50);
  quad=new Mesh(true,4,4,new VertexAttribute(Usage.Position,3,"attr_position"),new VertexAttribute(Usage.ColorPacked,4,"attr_color"));
  quad.setVertices(new float[] {//
  bottomLeft.x,bottomLeft.y,0f,Color.toFloatBits(255,0,0,255),//
      topRight.x,bottomLeft.y,0f,Color.toFloatBits(255,255,0,255),//
      bottomLeft.x,topRight.y,0f,Color.toFloatBits(0,255,0,255),//
      topRight.x,topRight.y,0f,Color.toFloatBits(0,0,255,255)});
  quad.setIndices(new short[] {0,1,2,3});
  //
  texture=new Texture(Gdx.files.internal(IMAGE_FILE));
  spriteBatch=new SpriteBatch();
  spriteBatch.getProjectionMatrix().setToOrtho2D(0,0,VIRTUAL_WIDTH,VIRTUAL_HEIGHT);
  }
到目前为止,为了使用缩放坐标,我已经成功地使用了这段代码,并且仍然保持了很好的纵横比,但是我没有成功地将起点0,0移动到左上角

请帮帮我

编辑:好的,经过一些测试,我发现它不工作的原因是我使用了spriteBatch。我想它忽略了摄像机。此代码出现在渲染部分。无论我对相机做了什么,它仍然会显示相同的结果

@Override
public void render()
  {
  if(Gdx.input.isKeyPressed(Keys.ESCAPE)||Gdx.input.justTouched())
    Gdx.app.exit();
  // update camera
  // camera.update();
  // camera.apply(Gdx.gl10);
  // set viewport
  Gdx.gl.glViewport((int)viewport.x,(int)viewport.y,(int)viewport.width,(int)viewport.height);
  // clear previous frame
  Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  //
  final String msg="test";
  final TextBounds textBounds=font.getBounds(msg);
  spriteBatch.begin();
  screenQuad.render(GL10.GL_TRIANGLE_STRIP,0,4);
  quad.render(GL10.GL_TRIANGLE_STRIP,0,4);
  Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
  spriteBatch.draw(texture,0,0,texture.getWidth(),texture.getHeight(),0,0,texture.getWidth(),texture.getHeight(),false,false);
  spriteBatch.draw(texture,0,VIRTUAL_HEIGHT-texture.getHeight(),texture.getWidth(),texture.getHeight(),0,0,texture.getWidth(),texture.getHeight(),false,false);
  font.draw(spriteBatch,msg,VIRTUAL_WIDTH-textBounds.width,VIRTUAL_HEIGHT);
  spriteBatch.end();
  }
这些线路:

camera.translate(VIRTUAL_WIDTH/2,VIRTUAL_HEIGHT/2,0);
camera.rotate(90,0,0,1);
应该移动相机,使其符合您的要求。但是,我的代码还有一个附加功能:

camera.update()
一个接一个地打电话给translate,看起来你错过了。报告说:


它似乎不起作用——绿色矩形没有占据整个窗口空间,文本和图像没有显示在正确的位置。它似乎在android上也不起作用。我不明白我错过了什么?我还想把起点放在左上角,而不是现在的左下角。你的相机旋转了90度。。。这是不是把X和Y搞乱了?尝试删除该行或将其更改为180?如果没有图片或一些细节,就很难知道什么看起来不太好。也许是时候问一个新问题了。相机是旋转的,因为我试着自己设定起点。最初的起点在屏幕的中间,所以我首先将相机移到了屏幕上。我之所以旋转它,是因为轴的值不是从左上角开始的——这意味着你越是向右和向下移动,x&y的增加就越多。关于似乎不起作用,我给出了示例链接,它与我目前的链接相同,因为我还没有找到解决方案。一旦我得到一个有效的解决方案,我将发布它。
update
public abstract void update()

Recalculates the projection and view matrix of this camera and the Frustum planes. Use this after you've manipulated any of the attributes of the camera.