使用camera.unproject重新渲染纹理会在相机移动时设置错误的位置

问题描述 投票:0回答:1

问题

当我移动相机时,每个正在渲染的纹理在两个不同的位置上闪烁。

我想要的是

示例:当我将相机向左移动时,我希望所有纹理向右移动32个像素。按下每个按钮移动32个像素。

目前的代码

我在评论中添加了一些额外的解释。

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);
    }

我无法想象在世界坐标上设置x和y坐标是行不通的。

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

我在这做错了什么?是否有更好的方法来实现渲染器的位置检查?

java camera libgdx
1个回答
0
投票

您无需检查渲染器的位置。所有你必须做的是设置你的camera的大小和位置。然后用batch.setProjectionMatrix(camera.combined)你说批次画出相机看到的东西。

因此,当您创建尺寸= 50x50且位置= 100,100的相机时 现在你创建一个大小= 50x50且位置= 75,75的Texture 纹理将完美贴合孔屏幕。

相机的位置在中心。所以Texture的位置是75,75而不是100,100

当您现在移动相机时,可以使用相机的translate()方法。 打电话:camera.translate(25,0)将你的相机向右移动25个单位,现在你只能在屏幕左侧看到你的Texture的一半。

这是可移动相机的简单示例。您可以使用箭头键移动:

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.