Libgdx's World Units

前端 未结 2 816
独厮守ぢ
独厮守ぢ 2020-12-12 00:19

I\'ve been trying to learn libgdx a little but now i\'m kinda confused about world units there that i don\'t even know what exact question i should be asking.

Anyway

相关标签:
2条回答
  • 2020-12-12 01:10

    Imagine you have a device 1200 x 800 pixel big.

    Now you say you will have a box2d rectangle on position: 100, 100 and size: 100,100

    When you now use your SpriteBatch (Box2dDebugRenderer) to render this you will see a world 1200 x 800 units big. You must try to forget to think in pixels. Important is that the SpriteBatch always draw 1200 x 800 pixels but not always 1200 x 800 units, later more.

    So this is what you see:

    When we now use a Camera we say we will only our world of 600 x 400 units.
    We create a Camera with size: 600 x 400 units!

    camera = new OrthographicCamera(600, 400);
    

    The batch still draw 1200 x 800 units and 1200 x 800 pixels

    Now with batch.setProjectionMatrix(camera.combined); you give the batch a Matrix to calculate the size of a unit. With this Matrix he can calculate how big it musst draw 1 unit.
    And after that the SpriteBatch draw 600 x 400 units but still 1200 x 800 pixels.
    And then you see:

    For this reason you can forget to think in pixels the Matrix of camera makes the calculation for you from pixel to units and you can focus on thinking in units.

    The next thing why you must think in units is that box2D calculates in meters. So the rectangle is 100 x 100 meters big and 100 meter above the bottom. So by gravity of g = 9,81m/s the rectangle falls not so fast as you might expect.

    The SpriteBatch always draw 1200 x 800 pixels otherwise you only see the game on the half of your screen. But with the Matrix of camera the batch know how many pixel he must draw 1 unit and so you see a World of 600 x 400 units on a Screen of 1200 x 800 pixels.

    Attention: 1 unit is not always equal 1 meter. For example in a spaceship game there are for example 5 kilometre between two ships. So please don't create a World of 10000 x 10000 units because Box2d won't calculate that.
    Then you can say 1 unit = 100 meter, so you have a World of 100 x 100 units. Now you must remember when a ship should be 200 m/s fast you must set body.setLinearVelocity(2,0) because 1 unit = 100 meter.

    So always think in units!

    Edit:
    Sometimes we do not come around pixel for example Gdx.input
    Gdx.input return the position in pixel.
    Now our camera have two methods to convert from pos in pixel to pos of our units and vice versa.

    float x = Gdx.input.getX();
    float y = Gdx.input.getY();
    camera.unproject(new Vector3(x, y, 0));//this converts a Vector3 position of pixels to a Vector3 position of units
    camera.project(new Vector3(10,10,0));//this converts a Vector3 position of units to a Vector3 position of pixels
    

    I hope I could help you

    0 讨论(0)
  • 2020-12-12 01:10

    When rendering things using a camera, you should be setting the projection matrix of the sprite batch to the combined view of the camera.

    The world is completely separate from the camera. The world has coordinates both positive and negative. The camera is your view of the world, and like a literal camera, it can move around in space. When you move the game camera, you are merely changing what part of the world you can see.

    All physics is done on pixel level (in your case) relative to the world origin. It is not changed in any way by the camera.

    0 讨论(0)
提交回复
热议问题