Mathematical question: procedural generation of a galaxy

前端 未结 19 1549
太阳男子
太阳男子 2020-12-22 17:25

I\'m going to make a space/trading/combat game that is completely procedurally generated. But, I know that storing all of the details of the whole galaxy in memory is unfeas

19条回答
  •  鱼传尺愫
    2020-12-22 18:18

    Can't you just SHA1 the galaxy ID, EG:

    Galaxy 1

    Sha1(1) = 356a192b7913b04c54574d18c28d46e6395428ab
    

    Galaxy 2

    Sha1(2) = da4b9237bacccdf19c0760cab7aec4a8359010b0
    

    Galaxy 3

    Sha1(3) = 77de68daecd823babbb58edb1c8e14d7106e83bb
    

    You can then segment the code, IE:

    First 4 Chars = Number of planets

    356a
    da4b
    77de
    

    You would need some sort of string to number algorithm, one simple one would be to take the ASCII code of every non numeric character, and then multiply them all together or something.

    So now we know how many planets are in our galaxy, how about galaxy x,y,z dimensions?

    Next 9 chars = Galaxy Dimensions (x,y,z)

    Same principle as above, turn the code into a large number. Have some sensibility checks as well, you don't want a galaxy thats 10milesx10milesx10miles with 20 million planets in it. Have some sort of weighted algorithm, like minimum size is # of planets * 10000. You will need to play with the numbers to make sure the ranges are all compatible and the selected chars from the hash actually give you a reasonable range.

    Or, instead of this, you can have a random number pick a number between the min and max size of the galaxy, but use a constant RNG seed, such as the galaxy ID! This way the galaxy sizes are essentially 'random' for the observer, but they will be the same every time.

    Etc etc!

    That's one way of getting properties of your Universe, but what about planet properties? Like population and other stuff?

    If you have Galaxy 1 with 20,000 planets, you can do:

    Sha1('1:340') = bc02ab36f163baee2a04cebaed8b141505cc26b5
    

    That is, galaxy one, planet 340. You can then just splice up that code however you want. The benefit of using a hash is that every planet should have a totally unique code.

提交回复
热议问题