狡猾的方法来绘制星空狡猾、方法来、星空

2023-09-11 03:26:53 作者:专业治疗蛋疼丶

我工作的一个游戏,我想出了一个相当有趣的问题:聪明的方法来绘制starfields

I'm working on a game, and I've come up with a rather interesting problem: clever ways to draw starfields.

这是一个2D游戏,所以动作可以在X和Y方向滚动。此外,我们可以通过调整规模,表现出更多的播放区域或更低。我还想星空有假的视差产生深度的IM pression。

It's a 2D game, so the action can scroll in the X and Y directions. In addition, we can adjust the scale to show more or less of the play area. I'd also like the starfield to have fake parallax to give an impression of depth.

现在我这样做的传统方式,通过让分,其中每个标签由深度的因素一个大阵。要画,我翻译每颗根据摄像机的位置乘上深度,所以有些星移了不少,有的动了一下。这一切工作正常,但当然,因为我有我的阵列明星我有问题是有限的,当摄像机移动太远,否则,我们缩小太多。这是将所有的工作,但涉及大量的code和特殊情况。

Right now I'm doing this in the traditional way, by having a big array of stars, each of which is tagged by a 'depth' factor. To draw, I translate each star according to the camera position multiplied by the 'depth', so some stars move a lot, and some move a little. This all works fine, but of course since I have a finite number of stars in my array I have issues when the camera moves too far or we zoom out too much. This is will all work, but is involving lots of code and special cases.

这冒犯了我的优雅感。目前已经得到了成为实现这一目标的一个更好的办法。

This offends my sense of elegance. There has got be a better way of achieving this.

我认为在程序上产生我的明星,这让我有无限多个:如通过使用固定的种子和PRNG来确定坐标。我需要划分天空成砖,通过散列砖坐标产生种子,然后绘制,也就是说,每瓦100颗恒星。这让我无限期延长我的星空在所有方向上,同时还只需要考虑到可见的瓷砖---但这并不与深度的因素的工作,因为这可以让明星去流浪的瓷砖之外。我可以简单地使用多层非视差starfields使用这种算法,但这种在我看来是作弊。

I've considered procedurally generating my stars, which allows me to have an unlimited number: e.g. by using a fixed seed and PRNG to determine the coordinates. I would need to divide the sky up into tiles, generate the seed by hashing the tile coordinates, and then draw, say, 100 stars per tile. This allows me to extend my starfield indefinitely in all directions while still only needing to consider the tiles that are visible --- but this doesn't work with the 'depth' factor, as this allows stars to stray outside their tile. I could simply use multiple layered non-parallax starfields using this algorithm but this strikes me as cheating.

和,当然,我需要做的这一切的每一帧,所以它必须是快速的。

And, of course, I need to do all this every frame, so it's got to be fast.

你什么都算进去了吗?

推荐答案

有几个层次分。

对于每一个图层,使用一个种子随机数发生器(或只是一个数组)生成的空白上的明星,下一个的量(泊松发布包,如果你想成为挑剔的话)。你要天上的星星pretty的稀少,所以空白往往会超过整行。背面层会比前面的人越密,效果显着。

For each layer, use a seeded random number generator (or just an array) to generate the amount of blank space between a star and the next one (a poisson distibution, if you want to be picky about it). You want the stars pretty sparse, so the blank space will often be more than whole row. The back layers will be more dense than the front ones, obviously.

使用该给自己几个瓦片每个(说)两个屏幕宽。通过跟踪,其中说:第一个明星是每一层滚动星空。

Use this to give yourself several tiles each (say) two screens wide. Scroll the starfield by keeping track of where that "first" star is for each layer.

玩家不会注意到平铺,因为你滚动瓷砖以不同的速率为每一层,特别是如果你使用一些层是每个相当稀少。

The player won't notice the tiling, because you scroll the tiles at different rates for each layer, especially if you use a few layers that are each fairly sparse.