【问题标题】:android - set starting point to top left on libgdxandroid - 在 libgdx 上将起点设置为左上角
【发布时间】:2023-03-25 06:43:01
【问题描述】:

我想使用 libgdx 作为基于屏幕纵横比缩放应用程序的解决方案。 我找到了这个链接,我发现它真的很有用: http://blog.acamara.es/2012/02/05/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/

我对 opengl 很生疏(已经好几年没写了),我希望使用这个链接上的例子,这样可以很容易地放置图像和形状。 可悲的是,起点在中间。我想像在许多其他平台上一样使用它 - 左上角应该是 (0,0) ,右下角应该是 (targetWidth-1,targetHeight-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();
  }

【问题讨论】:

    标签: android camera pivot point libgdx


    【解决方案1】:

    这些行:

    camera.translate(VIRTUAL_WIDTH/2,VIRTUAL_HEIGHT/2,0);
    camera.rotate(90,0,0,1);
    

    应该移动相机,让它做你想做的事。但是,我的代码还有一个:

    camera.update()
    

    在它调用 translate 之后调用,看起来你错过了那个。 doc for Camera.update 说:

    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.
    

    【讨论】:

    • 它似乎不起作用 - 绿色矩形没有占用整个窗口空间,并且文本和图像没有显示在正确的位置。
    • 它似乎在 android 上也无法正常工作。我不明白我错过了什么?我也希望把起点放在左上角,而不是当前的(左下角)。
    • 你的相机旋转了 90 度......这是把 X 和 Y 弄乱了吗?尝试删除该行或将其更改为 180?没有图片或一些细节,很难知道“似乎效果不佳”的具体含义。不过,可能是时候提出一个新问题了。
    • 相机旋转是因为我尝试自己设置起点。原来的起点是在屏幕中间,所以我先把相机移到了。我旋转了它,因为轴值不是从左上角开始的——这意味着你向右和向下移动的越多,x & y 增加的越多。关于“似乎效果不佳”,我已经给出了示例链接,这与我目前的链接相同(因为我还没有找到解决方案)。一旦我找到一个可行的解决方案,我会发布它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2021-11-07
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多