0

The problem

When I move the Camera every texture that is getting rendered flickers about the screen on 2 different positions.

What I want

example: When I move the camera to the left, I want the all the textures to move 32 pixels to the right. camera moves 32 pixels per button press.

Current code

I added some extra explanation in comments.

MainProgramEntryPoint

/**
 * DefaultCamera: Setting up OrthographicCamera
 * CameraMovement: Using camera.translate on keypresses to move the screen.
 * TestMoveable: Creating a texture for testing rendering.
 */
public class WorldSimGame extends ApplicationAdapter {
    private DefaultCamera defaultCamera;
    private CameraMovement cameraMovement;
    private TestMoveable testMoveable;

    // Setting up the camera and texture.
    public void create ()  {
        defaultCamera = new DefaultCamera();
        cameraMovement =  new CameraMovement(defaultCamera.getCamera());
        testMoveable = new TestMoveable();
        testMoveable.create(defaultCamera);
    }

    // The testMoveable.render(defaultCamera) should keep track of the  testMoveable position
    public void render ()  {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        defaultCamera.render();
        testMoveable.render(defaultCamera);
    }
}

TestMoveable

public class TestMoveable {
    private Texture tex;
    private SpriteBatch batch;
    private Vector3 position;

    public void create(DefaultCamera defaultCamera) {
        tex = new Texture(("wall.png"));    
        batch = new SpriteBatch();
        position = new Vector3(100, 100, 0);
        defaultCamera.getCamera().unproject(position);
    }

I cant imagine setting the x and y coordinates on the world coordinates wouldn't work.

    public void render(DefaultCamera defaultCamera) {       
        batch.begin();
        batch.draw(tex, position.x, position.y);
        batch.end();
    }
}

What am I doing wrong here? And is there a better way to implement position checking for the renderers?

bloxxyyy
  • 3
  • 3
  • I think you have some miss understanding how Orthographic camera works. Here are some links where it is explained how to work with the Orthographic camera: https://github.com/libgdx/libgdx/wiki/Orthographic-camera. Here is a good tutorial: http://www.gamefromscratch.com/post/2013/11/06/LibGDX-Tutorial-7-Camera-basics.aspx. And maybe this can help you to understand the use of `.unproject`: https://stackoverflow.com/questions/51993577/libgdxs-world-units/52072708#52072708 – Morchul Sep 10 '18 at 10:08

1 Answers1

0

You don't need to check the position of the renderer. All what you must do is to set the size and position of your camera. Then with batch.setProjectionMatrix(camera.combined) you say the batch to draw what the camera sees.

So when you create a camera with size = 50x50 and position = 100,100
and you now create a Texture with size = 50x50 and position = 75,75
The Texture will perfectly fit the hole screen.

The position of camera is in the center. So the position of Texture is 75,75 and not 100,100

When you now will move your camera, you can use the method: translate() of your camera.
Call: camera.translate(25,0) to move your camera 25 units to the right and now you only see the half of your Texture on the left side of the screen.

This is an easy example of a Moveable camera. You can use the arrow keys to move around:

public class WorldSimGame extends ApplicationAdapter {

    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture texture;

    public WorldSimGame() { }

    @Override
    public void create(){
        //Create texture, batch and camera
        texture = new Texture(Gdx.files.internal("badlogic.jpg"));
        batch = new SpriteBatch();
        camera = new OrthographicCamera(60,60);
    }

    @Override
    public void render(){
        //clear screen
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        moveCamera();
        //update camera that he recalculate his position and Matrix
        camera.update();

        batch.setProjectionMatrix(camera.combined); //set batch to draw what the camera sees
        batch.begin();
        batch.draw(texture,0,0); //draw texture
        batch.end();
    }

    private void moveCamera(){
        //move camera
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            camera.translate(4,0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            camera.translate(-4,0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            camera.translate(0,4);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
            camera.translate(0,-4);
        }
    }
}
Morchul
  • 1,987
  • 1
  • 7
  • 21