• <legend id='Bq00f'><style id='Bq00f'><dir id='Bq00f'><q id='Bq00f'></q></dir></style></legend>

    • <bdo id='Bq00f'></bdo><ul id='Bq00f'></ul>

      <i id='Bq00f'><tr id='Bq00f'><dt id='Bq00f'><q id='Bq00f'><span id='Bq00f'><b id='Bq00f'><form id='Bq00f'><ins id='Bq00f'></ins><ul id='Bq00f'></ul><sub id='Bq00f'></sub></form><legend id='Bq00f'></legend><bdo id='Bq00f'><pre id='Bq00f'><center id='Bq00f'></center></pre></bdo></b><th id='Bq00f'></th></span></q></dt></tr></i><div id='Bq00f'><tfoot id='Bq00f'></tfoot><dl id='Bq00f'><fieldset id='Bq00f'></fieldset></dl></div>
    1. <tfoot id='Bq00f'></tfoot>

      <small id='Bq00f'></small><noframes id='Bq00f'>

        在 libgdx 中渲染 box2d

        时间:2023-05-18

            • <i id='TnwqL'><tr id='TnwqL'><dt id='TnwqL'><q id='TnwqL'><span id='TnwqL'><b id='TnwqL'><form id='TnwqL'><ins id='TnwqL'></ins><ul id='TnwqL'></ul><sub id='TnwqL'></sub></form><legend id='TnwqL'></legend><bdo id='TnwqL'><pre id='TnwqL'><center id='TnwqL'></center></pre></bdo></b><th id='TnwqL'></th></span></q></dt></tr></i><div id='TnwqL'><tfoot id='TnwqL'></tfoot><dl id='TnwqL'><fieldset id='TnwqL'></fieldset></dl></div>

              <tfoot id='TnwqL'></tfoot><legend id='TnwqL'><style id='TnwqL'><dir id='TnwqL'><q id='TnwqL'></q></dir></style></legend>

              <small id='TnwqL'></small><noframes id='TnwqL'>

                <bdo id='TnwqL'></bdo><ul id='TnwqL'></ul>
                    <tbody id='TnwqL'></tbody>
                  本文介绍了在 libgdx 中渲染 box2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个使用 FitViewport 的大小为 800x480 的游戏世界,并且最初使用像素渲染 box2d 主体 + 固定装置,因此所有物理都显得浮动且缓慢.查看文档,我意识到 box2d 使用度量单位,因此我将 box2d 的位置和大小转换为 32 倍,因此我最终得到了一个 25x15 米的 box2d 世界.

                  I have a game world of size 800x480 using a FitViewport and was initally rendering box2d bodies + fixtures using pixels so all the physics appeared floating and slow. Looking at the documentation I realised box2d uses metrics units so I went through and converted the box2d positions and sizes by a factor of 32 so I end up with a box2d world of 25x15 metres.

                  我遇到的问题是现在 box2d 对象被渲染得非常小.如何缩小它们以使它们在屏幕上显示为常规尺寸?

                  The issue I'm having is that now box2d objects are rendered incredibly small. How can I scale them back so they appear regular size on screen?

                  推荐答案

                  你需要使用world和box2之间的转换因子.

                  You need to use conversions factor between world and box2.

                  在您的 create() 中,您将拥有:

                  In your create() you'll have :

                  private OrthographicCamera camera;
                  private BodyDef bodyDef;
                  private Body body;
                  private PolygonShape box;
                  static float WORLD_TO_BOX, BOX_TO_WORLD, bodyWidth, bodyHieght;
                  
                  public Create(){
                      //Conversion factors
                      WORLD_TO_BOX = 1/32f;  
                      BOX_TO_WORLD = 1/WORLD_TO_BOX;
                  
                      //Creation of the camera with your factor 32
                      camera = new OrthographicCamera();
                      camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX;  
                      camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX;  
                      camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);  
                      camera.update();  
                  
                      //Let's say you want to create a body in the center of your screen
                      BodyDef = new BodyDef();  
                      BodyDef.position.set(new Vector2(camera.viewportWidth/2, -camera.viewportHeight/2));  
                      body = world.createBody(BodyDef);  
                      box = new PolygonShape();  
                      //Let's say you want your body to have the shape of a square which sides size is one fifth of your camera width
                      bodyWidth = camera.viewportWidth/10;
                      bodyHeight = camera.viewportWidth/10;
                      box.setAsBox(bodyWidth, bodyHeight);
                      body.createFixture(box, 0.0f);
                  } 
                  

                  现在您只使用转换系数 WORLD_TO_BOX 来创建您的相机.但是如果你想在你的身体上绘制一些精灵,你将在 render() 中使用 BOX_TO_WORLD 因子.例如,您想使用 spritebatch 在您的正方形上绘制一个具有确切大小和位置的精灵,在您将拥有的 render() 中:

                  For now you only used the conversion factor WORLD_TO_BOX, to create your camera. But if you want to draw some sprites on your body, you'll use the BOX_TO_WORLD factor, in the render(). For example you want to use a spritebatch to draw a sprite on your square, with the exact size and position, in the render() you'll have :

                  private Texture texture;
                  
                  public void render(){
                      game.batch.begin();
                      game.batch.setColor(1,1,1,1);
                  //In Box2D, the orgin of a body is the center of the body, 
                  //while in your world the orgin of a sprite is the bottom left corner
                  //I Box2D the width and the height of a body will be twice the values you entered,
                  //while in your world the width and the height of a sprite are the exact values you entered.
                  //Taking all this into account, to draw a sprite above a body you need to :
                  //1 - center the sprite above the body. Ex : (body.getPosition().x - bodyWidth) for the X position
                  //2 - draw the sprite at the right size, taking in account the factor 2. Ex : bodyWidth * 2 for the widht of the sprite
                  //3 - Apply the BOX_TO_WORLD factor to all every sizes and distances
                      game.batch.draw(texture,
                                      BOX_TO_WORLD * (body.getPosition().x - bodyWidth),
                                      BOX_TO_WORLD * (body.getPosition().y - bodyHeight),
                                      BOX_TO_WORLD * bodyWidth * 2,
                                      BOX_TO_WORLD * bodyHeight * 2);
                       game.batch.end();
                  }
                  

                  希望它能回答你的问题.

                  Hope it answers your question.

                  这篇关于在 libgdx 中渲染 box2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:触摸时移除精灵 下一篇:加载 TextureAtlas 的状态

                  相关文章

                  最新文章

                1. <legend id='OHDwx'><style id='OHDwx'><dir id='OHDwx'><q id='OHDwx'></q></dir></style></legend>
                  <tfoot id='OHDwx'></tfoot>

                  <small id='OHDwx'></small><noframes id='OHDwx'>

                2. <i id='OHDwx'><tr id='OHDwx'><dt id='OHDwx'><q id='OHDwx'><span id='OHDwx'><b id='OHDwx'><form id='OHDwx'><ins id='OHDwx'></ins><ul id='OHDwx'></ul><sub id='OHDwx'></sub></form><legend id='OHDwx'></legend><bdo id='OHDwx'><pre id='OHDwx'><center id='OHDwx'></center></pre></bdo></b><th id='OHDwx'></th></span></q></dt></tr></i><div id='OHDwx'><tfoot id='OHDwx'></tfoot><dl id='OHDwx'><fieldset id='OHDwx'></fieldset></dl></div>

                      <bdo id='OHDwx'></bdo><ul id='OHDwx'></ul>