2

enter image description hereI am basing my code off of the tutorial found here

I am attempting to make my sprite (a 32x32 pixel pikachu) move across the map when I press the corresponding keys in an effort to understand the orthographic camera and map renderer of libgdx. The movement is working fine, however, my issue is that my sprite seems to be a different size than I am expecting. My tmx map is a 9x30 tiled map with each tile being 32x32 pixels. I am placing my sprite into it, and noticing the sprite seems to be about half the width of the map tiles. I notice that by changing the height and width inside the ortho camera, the size of my sprite seems to fluctuate as well. Since the sprite should be the same size as each of the tiles on the map (32x32 pixels each), I am unsure why the display is appearing this way. I suspect my understanding of the orthographic camera and how it interacts with sprites is incorrect, but I am hoping someone can correct my understanding and show me why my 32x32 sprite appears as a different size than the rest of the map tiles in this scenario.

public class TestMap extends ApplicationAdapter implements InputProcessor{
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
SpriteBatch sb;
Texture texture;
Sprite sprite;

@Override
public void create () {
    camera = new OrthographicCamera();
    camera.setToOrtho(false,320,320);
    camera.update();
    tiledMap = new TmxMapLoader().load("testmap.tmx");
    tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
    Gdx.input.setInputProcessor(this);
    sb = new SpriteBatch();
    texture = new Texture(Gdx.files.internal("pikachu.png"));
    sprite = new Sprite(texture);
}
@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    tiledMapRenderer.setView(camera);
    tiledMapRenderer.render();
    sb.begin();
    sprite.draw(sb);
    sb.end();
}
@Override
public boolean keyDown(int keycode) {
    return false;
}
@Override
public boolean keyUp(int keycode) {
    if(keycode == Input.Keys.LEFT)
        camera.translate(0,32);
    if(keycode == Input.Keys.RIGHT)
        camera.translate(0,-32);
    if(keycode == Input.Keys.UP)
        camera.translate(32,0);
    if(keycode == Input.Keys.DOWN)
        camera.translate(-32,0);
    if(keycode == Input.Keys.NUM_1)
        tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
    if(keycode == Input.Keys.NUM_2)
        tiledMap.getLayers().get(1).setVisible(!tiledMap.getLayers().get(1).isVisible());
    return false;
}
@Override
public boolean keyTyped(char character) {
    return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}
@Override
public boolean scrolled(int amount) {
    return false;
}
}

The grass tile and its inverted form are all 32x32, and the red space indicates the outside of the map boundaries. Please let me know if there is any more information required to answer this! Thank you!

Zoltorn
  • 171
  • 1
  • 2
  • 10
  • Maybe you can try `sb.getProjectionMatrix().setToOrtho2D(0, 0, width, height)` – Alex Sveshnikov Mar 18 '21 at 06:21
  • @Alex Thanks for the suggestion! I gave it a try, playing around with some values, when I set it to width and height of 640 I am able to get the size to match up as I was looking for. Do you know why I have to double the size of my viewport width and height (which I have as 320) to get the sprite to match with the tile? If you want to make an answer out of this response I can accept it too since it did get me what I was looking for – Zoltorn Mar 19 '21 at 01:55

0 Answers0