滚动视差背景,在 libgdx 中无限重复
我正在制作一款 2D 横向卷轴太空射击游戏,我需要一个可以无限滚动的背景(它可以重复平铺或包裹)。我还想实现视差滚动,因此也许有一个几乎不移动的最低背景星云纹理,一个包含几乎不移动的遥远恒星的较高背景星云纹理,以及包含移动很多的近距离恒星的最高背景。
我从 google 看到我让每一层的移动量比它上面的层少 50%,但是我如何在 libgdx 中实现这个?我有一个可以放大和缩小的相机,在 800x480 的物理屏幕中可以显示从 128x128 像素(一艘船)到一个巨大的空间区域(其边缘多次包裹纹理)的任何内容。
如何连续包裹较小的纹理(例如 512x512),就好像它无限平铺一样(当相机向右缩小时),然后如何将多个像这样的纹理分层,将它们保持在合适的结构中(有吗? libgdx api 中的一个?)并随着玩家坐标的变化而移动它们?我查看了 javadocs 和示例,但找不到类似此问题的任何内容,如果很明显,请道歉!
I'm making a 2D sidescrolling space shooter-type game, where I need a background that can be scrolled infintely (it is tiled or wrapped repeatedly). I'd also like to implement parallax scrolling, so perhaps have one lowest background nebula texture that barely moves, a higher one containing far-away stars that barely moves and the highest background containing close stars that moves a lot.
I see from google that I'd have each layer move 50% less than the layer above it, but how do I implement this in libgdx? I have a Camera that can be zoomed in and out, and in the physical 800x480 screen could show anything from 128x128 pixels (a ship) to a huge area of space featuring the textures wrapped multiple times on their edges.
How do I continuosly wrap a smaller texture (say 512x512) as if it were infinitely tiled (for when the camera is zoomed right out), and then how do I layer multiple textures like these, keep them together in a suitable structure (is there one in the libgdx api?) and move them as the player's coords change? I've looked at the javadocs and the examples but can't find anything like this problem, apologies if it's obvious!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嘿,我也在制作视差背景并尝试让它滚动。
存储库中有一个ParallaxTest.java,可以找到此处。
该文件是一个独立的类,因此您需要将其合并到您的游戏中。并且您将需要更改控制输入,因为它连接到使用触摸屏/鼠标。
这对我有用。至于重复的背景,我还没有做到这一点,但我认为你只需要基本的逻辑,例如,距离最后一个屏幕,更改前几个屏幕的位置以在最后对齐。
Hey I am also making a parrallax background and trying to get it to scroll.
There is a ParallaxTest.java in the repository, it can be found here.
this file is a standalone class, so you will need to incorporate it into your game how you want. and you will need to change the control input since its hooked up to use touch screen/mouse.
this worked for me. as for repeated bg, i havent gotten that far yet, but i think you just need to basic logic as in, ok one screen away from the end, change the first few screens pos to line up at the end.
关于视差滚动,我没有比 PFG 更多的话要说的了。在测试文件夹下的存储库中确实有一个示例,并且网络上有一些解释。我喜欢这个 。
有背景的事情其实很容易解决。这个问题和其他相关问题可以通过使用模代数来解决。我不会详细介绍,因为一旦显示就很容易理解。
想象一下您想在屏幕上显示指南针。您有一个代表基点的 1024x16 纹理。基本上你所拥有的只是一条带子。抛开有关真实方向等的考虑,您必须渲染它。
例如,您的视口为 300x400,并且您希望屏幕上的纹理为 200px(以使其更有趣)。您可以使用单个区域完美地渲染它,直到到达位置 (1024-200) = 824。一旦到达这个位置,显然就不再有纹理了。但既然它是一个指南针,很明显,一旦到达终点,它就必须重新开始。这就是答案。另一个纹理区域就可以解决问题。范围 825-1023 必须由另一个区域表示。对于每个值 pos>824 &&,第二个区域的大小将为 (1024-pos)。 pos<1024
此代码旨在作为指南针的真实示例。它非常脏,因为由于范围 (0-3.6) 到 (0-1024) 之间的转换,它始终与相对位置一起工作。
I have not much more to say regarding to the Parallax Scrolling than PFG already did. There is indeed an example in the repository under the test folder and several explanations around the web. I liked this one.
The matter with the background is really easy to solve. This and other related problems can be approached by using modular algebra. I won't go into the details because once shown is very easy to understand.
Imagine that you want to show a compass in your screen. You have a texture 1024x16 representing the cardinal points. Basically all you have is a strip. Letting aside the considerations about the real orientation and such, you have to render it.
Your viewport is 300x400 for example, and you want 200px of the texture on screen (to make it more interesting). You can render it perfectly with a single region until you reach the position (1024-200) = 824. Once you're in this position clearly there is no more texture. But since it is a compass, it's obvious that once you reach the end of it, it has to start again. So this is the answer. Another texture region will do the trick. The range 825-1023 has to be represented by another region. The second region will have a size of (1024-pos) for every value pos>824 && pos<1024
This code is intended to work as real example of a compass. It's very dirty since it works with relative positions all the time due to the conversion between the range (0-3.6) to (0-1024).
您可以使用 setWrap 函数,如下所示:
它会重复绘制背景!希望这有帮助!
You can use setWrap function like below:
It will draw background repeatedly! Hope this help!
在初始化对象纹理的位置下方。然后在“Where YourTexture”中的该类型下方
是您想要视差滚动的纹理。
在您的渲染文件中输入此代码。
它会给你一个错误,所以创建一个名为 srcy 的变量。这没什么太花哨的。
Beneath where you initialize your Texture for the object. Then beneath that type in this
Where YourTexture is your texture that you want to parallax scroll.
In Your render file type in this code.
It is going to give you an error so make a variable called srcy. It is nothing too fancy.