pixelherodev
pixelherodev

Reputation: 1

display sprite on tile map libgdx

I am creating a game in LibGDX. I have taken the tiles used in Pixel Dungeon and created a tile map using Tiled.

The main character's class is a subclass of Actor, and because the character is animated, I am using this code to draw the sprite:

if (direction.isEastwards() || direction.isNorthwards()) {
        Vector3 projectedPosition = getLocation().getCamera().project(
                new Vector3(16 * getX() + 1, Gdx.graphics.getHeight()
                        - (16 * getY()) - 15, 0));
        batch.draw(current.getKeyFrame(
                animTime += Gdx.graphics.getDeltaTime(), true),
                projectedPosition.x, projectedPosition.y);
    } else {
        Vector3 projectedPosition = getLocation().getCamera().project(
                new Vector3(16 * getX() + 13, Gdx.graphics.getHeight()
                        - (16 * getY()) - 15, 0));
        batch.draw(current.getKeyFrame(
                animTime += Gdx.graphics.getDeltaTime(), true),
                projectedPosition.x, projectedPosition.y);

    }

When I launch the game in Eclipse, the sprite initially appears in the correct location. However, if I resize the screen, the sprite ceases to be in the correct location and eventually will disappear off the map.

The same thing happened before I started using projection, and I figured the problem was related to projection. As this is an area I have not explored before I decided after an hour of being able to solve this to ask for help.

The animation flips depending on which direction the character faces, which is why the if/else clause is there.

Addendum: The stage is created with new Stage(new ExtendViewport(800,600),batch);, the resize method updates the camera, and the batch is set to the projection matrix.

Here is more relevant code:

Camera and map initialisation:

camera=new OrthographicCamera();
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
mapRenderer=new OrthogonalTiledMapRenderer(map,batch);

Render method:

camera.update();
batch.setProjectionMatrix(camera.combined);
mapRenderer.setView(camera);
mapRenderer.render();
stage.act();
stage.draw();

Resize method:

camera.viewportWidth=width;
camera.viewportHeight=height;
camera.update();

Actor draw method:

@Override
public void draw(Batch batch, float parentAlpha) {
    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    if (direction.isEastwards() || direction.isNorthwards()) {
        Vector3 projectedPosition = getLocation().getCamera().project(
                new Vector3(16 * getX() + 1, Gdx.graphics.getHeight()
                        - (16 * getY()) - 15, 0));
        batch.draw(current.getKeyFrame(
                animTime += Gdx.graphics.getDeltaTime(), true),
                projectedPosition.x, projectedPosition.y);
    } else {
        Vector3 projectedPosition = getLocation().getCamera().project(
                new Vector3(16 * getX() + 13, Gdx.graphics.getHeight()
                        - (16 * getY()) - 15, 0));
        batch.draw(current.getKeyFrame(
                animTime += Gdx.graphics.getDeltaTime(), true),
                projectedPosition.x, projectedPosition.y);

    }
    // Walk animation displays for 0.2 seconds
    if (current == walk && animTime >= 0.2) {
        current = idle;
        animTime = 0;
    }
}

Upvotes: 0

Views: 712

Answers (1)

Wouf
Wouf

Reputation: 21

I think the problem it caused because you dont combine the sprite with camera projection. set your spriteBatch like this :

 spriteBatch.setProjectionMatrix(camera.combined);

And dont miss update viewport in resize method :

public void resize(int width, int height) {
    camera.viewportWidth = width;
    camera.viewportHeight = height;
    camera.update();
}

With that, you can resize or zoomin zoomout, the spriteBatch it scalled to the camera projection.

Upvotes: 1

Related Questions