简体   繁体   English

LibGDX:平铺地图的视差效果

[英]LibGDX: Parallax effect with tiled maps

Need to make parallax effect in the game with LibGDX and tiled map. 需要在LibGDX和平铺地图的游戏中产生视差效果。 Background should move with different speed then foreground. 背景应该以不同的速度移动然后前景。 In the docs https://github.com/libgdx/libgdx/wiki/Tile-maps said: 在文档中https://github.com/libgdx/libgdx/wiki/Tile-maps说:

By rendering each layer separately and modifying the view for every layer, you can also achieve a parallax effect. 通过单独渲染每个图层并修改每个图层的视图,您还可以实现视差效果。

Current implementation : with PlayScreenVariantOne all works but background moving with same speed like foreground 当前的实现 :使用PlayScreenVariantOne都可以工作,但背景以与前景相同的速度移动

public class PlayScreenVariantOne implements Screen {

    private TextureAtlas atlas;
    private OrthographicCamera gameCam;
    private Viewport viewport;
    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;
    ...

    // ...
    // ... bla-bla-bla...
    // ...
    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        renderer.setView(gameCam);
        renderer.render();

        game.batch.setProjectionMatrix(gameCam.combined);
        game.batch.begin();

        player.draw(game.batch);
        game.batch.end();
        hud.stage.draw();

    }
 }

Parallax implementation : with PlayScreenVariantTwo foreground and background rendered with same speed. 视差实现 :使用PlayScreenVariantTwo前景和背景以相同的速度渲染。 Parallax effect does not work! 视差效果不起作用! :*( :*(

public class PlayScreenVariantTwo implements Screen {

    private TextureAtlas atlas;
    private OrthographicCamera gameCam;
    private Viewport viewport;
    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;
    ...

    // ...
    // ... bla-bla-bla...
    // ...
    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        int[] backgroundLayers = { 7 };
        int[] ground = { 9 };

        renderer.setView(gameCam.combined, (gameCam.position.x - gameCam.viewportWidth / 2 - 2), gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth, gameCam.viewportHeight);
        renderer.render(backgroundLayers);

        renderer.setView(gameCam.combined, (gameCam.position.x - gameCam.viewportWidth / 2) / 10, gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth  * 10, gameCam.viewportHeight);
        renderer.render(ground);

        game.batch.setProjectionMatrix(gameCam.combined);
        game.batch.begin();

        player.draw(game.batch);
        game.batch.end();

        hud.stage.draw();

    }
 }

Did you try to simply multiply X (or Y, what ever you need) coordinate for background with factor: 您是否尝试简单地将X(或Y,您需要的)坐标与背景因子相乘:

  renderer.setView(gameCam.combined, (gameCam.position.x*0.5 - gameCam.viewportWidth / 2 - 2), gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth, gameCam.viewportHeight);

or 要么

  renderer.setView(gameCam.combined, (gameCam.position.x - gameCam.viewportWidth / 2 - 2)*0.5, gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth, gameCam.viewportHeight);

I added that "*0.5", so background would move twice slower. 我添加了“* 0.5”,因此背景会慢两倍。 Not sure which one of those 2 rows will give you better results - you have to try for your self, but that's the ide - multiply background coordinates, X or Y, which one you need to set different speed. 不确定这两行中的哪一行会给你更好的结果 - 你必须尝试自己,但那就是ide - 乘以背景坐标,X或Y,你需要设置不同的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM