滚动视差背景,在libgdx中无限重复

fun*_*ead 12 scroll textures 2d libgdx parallax

我正在制作一个2D侧面空间射击游戏,我需要一个可以无限滚动的背景(它是平铺或重复包裹).我也想实现视差滚动,所以可能有一个最低的背景星云纹理几乎没有移动,一个较高的背景星云几乎没有移动,最高的背景包含移动很多的近星.

我从谷歌看到我的每一层移动比它上面的层少50%,但我如何在libgdx中实现它?我有一个可以放大和缩小的相机,在物理800x480屏幕上可以显示从128x128像素(一艘船)到大面积空间的任何东西,其特征是纹理在边缘上多次包裹.

我如何连续地包裹较小的纹理(比如说512x512),好像它是无限平铺的(当相机被缩小时),然后我如何将这些多个纹理分层,将它们组合在一个合适的结构中(在那里) libgdx api中的一个?)并随着玩家的坐标改变而移动它们?我查看了javadocs和示例,但找不到类似这个问题的任何内容,如果很明显就道歉!

pfg*_*pfg 11

嘿,我也在制作parrallax背景,试图让它滚动.

存储库中有一个ParallaxTest.java,可以在这里找到.

此文件是一个独立的类,因此您需要将其合并到游戏中.并且您需要更改控制输入,因为它连接到使用触摸屏/鼠标.

这对我有用.至于重复的bg,我还没有到目前为止,但我认为你只需要基本的逻辑,好一个屏幕远离结束,改变前几个屏幕pos最后排队.


小智 3

关于视差滚动,我没有比 PFG 更多的话要说的了。在测试文件夹下的存储库中确实有一个示例,并且网络上有一些解释。我喜欢这个。有背景的事情其实很容易解决。这个问题和其他相关问题可以通过使用模代数来解决。我不会详细介绍,因为一旦显示就很容易理解。

想象一下您想在屏幕上显示指南针。您有一个代表基点的 1024x16 纹理。基本上你所拥有的只是一条带子。抛开有关真实方向等的考虑,您必须渲染它。

例如,您的视口为 300x400,并且您希望屏幕上的纹理为 200px(以使其更有趣)。您可以使用单个区域完美地渲染它,直到到达位置 (1024-200) = 824。一旦到达这个位置,显然就不再有纹理了。但既然是指南针,显然一旦到达终点,就必须重新开始。这就是答案。另一个纹理区域就可以解决问题。范围 825-1023 必须由另一个区域表示。对于每个值 pos>824 && pos<1024,第二个区域的大小为 (1024-pos)

该代码旨在作为指南针的真实示例。它非常脏,因为由于范围 (0-3.6) 到 (0-1024) 之间的转换,它始终与相对位置一起工作。

spriteBatch.begin();
    if (compassorientation<0)
        compassorientation = (float) (3.6 - compassorientation%3.6);
    else
        compassorientation = (float) (compassorientation %  3.6);
    if ( compassorientation < ((float)(1024-200)/1024*3.6)){
        compass1.setRegion((int)(compassorientation/3.6*1024), 0, 200, 16);
        spriteBatch.draw(compass1, 0, (Gdx.graphics.getHeight()/2) -(-250 + compass1.getTexture().getHeight()* (float)1.2), Gdx.graphics.getWidth(), 32 * (float)1.2);

    }
    else if (compassorientation > ((float)(1024-200)/1024*3.6)) {
        compass1.setRegion((int)(compassorientation/3.6*1024), 0, 1024 - (int)(compassorientation/3.6*1024), 16);
        spriteBatch.draw(compass1, 0, (Gdx.graphics.getHeight()/2) -(-250 + compass1.getTexture().getHeight()* (float)1.2), compass1.getRegionWidth()/200f * Gdx.graphics.getWidth() , 32 * (float)1.2);
        compass2.setRegion(0, 0, 200 - compass1.getRegionWidth(), 16);
        spriteBatch.draw(compass2, compass1.getRegionWidth()/200f * Gdx.graphics.getWidth() , (Gdx.graphics.getHeight()/2) -(-250 + compass1.getTexture().getHeight()* (float)1.2), Gdx.graphics.getWidth() - (compass1.getRegionWidth()/200f * Gdx.graphics.getWidth())  , 32 * (float)1.2);
    } 


    spriteBatch.end();
Run Code Online (Sandbox Code Playgroud)