0
\$\begingroup\$

I'm just starting to learn LibGDX and have been having some troubles getting my world size to match with my texture size and box2d bodies. I have a texture 64x64 that I am trying to display but it takes up half of the window, both on desktop and android. I've tried scaling the texture down also. I am wondering if this has anything to do with my two variables FRUSTUM_WIDTH and FRUSTUM_HEIGHT being used in the creation of my camera.

  int FRUSTUM_WIDTH = 8;
int FRUSTUM_HEIGHT = 15;
World world;
Box2DDebugRenderer debugRenderer;

@Override
public void create () {
    batch = new SpriteBatch();
    img = new Texture("player.png");

    player = new Sprite(img);
    player.setPosition(5, 5);
    //player.scale(.8f);

    camera = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
    camera.position.set(FRUSTUM_WIDTH / 2, FRUSTUM_HEIGHT / 2, 0);

    world = new World(new Vector2(0, -50), true);
    debugRenderer = new Box2DDebugRenderer();

    setUpBox2D();

}

private void setUpBox2D()
{
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;

    bodyDef.position.set(4, 10);


    Body body = world.createBody(bodyDef);

    CircleShape circle = new CircleShape();
    circle.setRadius(1);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 0.1f;
    fixtureDef.friction = 0.1f;
    fixtureDef.restitution = 0.5f;

    Fixture fixture = body.createFixture(fixtureDef);

    BodyDef groundBodyDef = new BodyDef();

    groundBodyDef.position.set(new Vector2(0, 1));

    Body groundBody = world.createBody(groundBodyDef);

    PolygonShape groundBox = new PolygonShape();

    groundBox.setAsBox(camera.viewportWidth, 1.0f);
    groundBody.createFixture(groundBox, 0);
}
@Override
public void render () {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    drawScene();
    updateScene();
}

private void drawScene()
{
    debugRenderer.render(world, camera.combined);
    camera.update();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    player.draw(batch);
    batch.end();
    world.step(1/60f, 6, 2);
}
private  void updateScene()
{

}

I do have a resize method but it is currently empty.

The window size is 480 by 800. I looked at a game called Super Jump on Github which helped fix my camera issue, but I am still having issues with the giant textures

\$\endgroup\$
0
\$\begingroup\$

What you are currently referring to as FRUSTUM_WIDTH, FRUSTUM_HEIGHT is actually the world width and height for the camera. This means that, across your application window, 8 world units in width and 12 world units in height will be displayed.

Since you are not changing the size of your Sprite, it will have the texture's width and height, so it will be drawn at a size of 64x64 world units. This means you need to set the Sprite's size to fit into the world. If you want it to be drawn at 64x64px then you need to do some math (spriteWidth / (windowWidth / worldWidth)) to convert the Sprite's pixel size to world unit size.

Also, after you manipulate any of the camera's attributes (like its position) you need to call camera.update().

Read the OrthographicCamera wiki page for more information.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.