Procedurally generated galaxy (image-heavy)

Community Forums/Showcase/Procedurally generated galaxy (image-heavy)

Vilu(Posted 2008) [#1]
500 million unique stars in a couple of hundred lines of code. David Braben did it in the early 90's, and I thought to try my hand at it, too.

Starmap zoomed up on a star sector:


The names of the stars are pseudo-randomly generated out of three syllables glued together. I think I accidentally invented a whole new language.

Zooming out just a little bit...:


A bit more...:


When zoomed out far enough, a galaxy image overlay kicks in with the individual star generator disabled:


One quarter of the entire galaxy:



The starfield density variations are based on a 1024x1024 grayscale image of a real galaxy, NGC 5457, taken by the Hubble space telescope. The algorithm loads and parses the actual image pixel-by-pixel, and the amount of stars generated in any area depends on the brightness of a pixel. Each pixel represents an area of 8x8 star sectors (one of which being highlighted green in the screenshots), so the entire galaxy has some ~67 million star sectors, each having 0 to 63 stars.

Every individual star seen in the screenshots are created on-the-fly while scrolling and zooming the starmap. Scrolling is quite fast even with tens of thousands of generated star blips on the screen.

Here's a small stripped-down Win32 demo of Ananta, featuring only the scrollable starmap and galaxy generator. Everything else in the game has been disabled for the purposes of this demo. Please give it a spin! :)

http://ananta.googlecode.com/files/ananta_procedural_universe_demo_20080518_win32_bin.zip


puki(Posted 2008) [#2]
Oooh, I like that last picture.


Vilu(Posted 2008) [#3]
Maybe that's because it's the only thing not generated, it's taken by the Hubble ;)


puki(Posted 2008) [#4]
Wait a minute - the last image is a real picture.

Still, I very much like this - I could have a use for this in my Star Trek game. I made my own section of the galaxy. I have nowhere near the same amount of stars - mainly because I didn't see the point.

If nobody else wants this then I will gladly take it off your handsl.


Vilu(Posted 2008) [#5]
Well, go ahead and checkout the Subversion repository :). The project is open-source and will remain so. Just remember to follow the GPLv3 rules ;)


OldNESJunkie(Posted 2008) [#6]
Looks cool, works well. As you said, pretty fast even with thousands on the screen. It be pretty cool if you could generate galaxies & planets as well.


Vilu(Posted 2008) [#7]
Solar system generator is in the works. The game will feature procedurally generated planets soon enough.


puki(Posted 2008) [#8]
Sweet Jesus!!!

He's written this in BMax.

Why?

Can you knock up a Blitz3D exe?


_33(Posted 2008) [#9]
Well, what doesn't impress me in this is:
- It uses a predefined picture, and generates a number of stars based on the intensity of the RGB pixel.
- The maps generated are obviously 2D
- No moving stars

Otherwise, I like the naming system :P


puki(Posted 2008) [#10]
He's put a jump-drive system in there too.


Vilu(Posted 2008) [#11]
I knew this whole 2d issue would come up ;)

Yeah, the starmap is 2d and will remain so, and so will the game itself. As for Puki's question "Why?", see my latest worklog entry.

The star generator code can be easily adapted for 3d for anyone interested in doing so.


puki(Posted 2008) [#12]
What happens when you find a fuel tank?

EDIT:
Do ships have different types of fuel?


Vilu(Posted 2008) [#13]
Do ships have different types of fuel?


Well, yes and no. Different kind of engines burn different types of fuel, so fuel type depends on what type of engines are installed on your ship. That whole fuel thing is still on the drawing board largely unfinished, as you might notice as you're obviously digging through the source... ;-)


puki(Posted 2008) [#14]
Re-do the exe in Blitz3D - that will slow me down.


Vilu(Posted 2008) [#15]
It uses a predefined picture, and generates a number of stars based on the intensity of the RGB pixel.


It's a lot more trickier than you think. The pixel values are interpolated so that there are no sudden variations in star densities between a totally white and a totally black pixel. To smooth out the density transitions, the values of the neighboring pixels have to be taken into consideration. And all of this has to be calculated and generated in real-time while scrolling.

I wish I could take the credit for that brilliant algorithm, but the underlying function is actually blatantly ripped from the reverse-engineered Elite II Frontier executable... Ehem... Forget I said that. ;)

Anyone interested in the galaxy generator fundamentals, head over here:
http://www.jongware.com/galaxy1.html


plash(Posted 2008) [#16]
Hehe.. someone took alot of time disassembling elite.

Puki: BlitzMax is way better then b3d, sucker!


Cruis.In(Posted 2008) [#17]
so are these stars places you can enter and visit and fly around in? like actual solar systems? or you need to generatea solar system to go with each of these stars now?


_33(Posted 2008) [#18]
Vilu, mind you I think you have successfully done what you have done. I just think that a 3D version would immensely be more interesting for a future Elilte game as it would more represent the reality and we could possibly fly in there in 3D on warp speed :P

EDIT: Oh, and moving stars isn't probably what would be of interest, but more like moving planets that orbit the stars, and asteroid belts :O

EDIT2: Imagine a higly complex AI computer thingy that would scan for life on other solar systems :O


Kalisme(Posted 2008) [#19]
wow... I'm impressed. I mean, I guess it's not the hardest thing in the world to do, but who cares? it's pretty cool that you sat down and did something to construct a large gallaxy randomly.
Have you considered making this 3d? I don't mean blitz3d or even a direct3d plugin to blitzmax (maybe you could, but I think it might be slow), but a simple engine that grabs all the vectors and transform them using a camera matrix... use a projection formula to scale the distance so that you can navigate the stars. I think that would be pretty cool... I think I have some freeware game in mind actually... Oh well...
I'm blabbering on (I'm half asleep)... cool idea, I like.


Vilu(Posted 2008) [#20]
so are these stars places you can enter and visit and fly around in? like actual solar systems? or you need to generatea solar system to go with each of these stars now?


The plan is to implement a jumpdrive system that lets you travel across the galaxy, from one solar system to another. Solar system contents will also be generated at the moment the player enters them. That's next on my todo list.

The idea really is to make a living, rotating solar system with the bodies gravitationally bound together. I already have the game framework in place with stationary planets. I tried it to make them orbit the star, but I bumped into some orbit deterioration problems (mainly because my decision to use deltatimer instead of fixed rate logic). When I work around that problem, I'll make the them rotate again. E pur si muove! ;)

Thank you all for your comments. When it comes to 3d, while I'm not going to make this 3d, feel free to download the code yourself from the Subversion repository and dissect it. It's not all that complex and should be easily adaptable for 3d. Just remember, the code is GPL'ed so remember to show everyone else what you've done with it.


Rimmsy(Posted 2008) [#21]
I've got my eye on this, it looks very interesting - as does the source.


Yahfree(Posted 2008) [#22]
Impressive, but I did see some stars with the same name.. also, I think there's more than 500million stars in a galaxy :D


Vilu(Posted 2008) [#23]
Yeah, the star name generator is now fixed, it didn't rotate the syllables properly so stars in adjacent sectors often had the same name!

also, I think there's more than 500million stars in a galaxy :D

I dare you to visit all of them in your lifetime, and _then_ I'll code in the rest of the 100 billion ;)


*(Posted 2008) [#24]
bloody amazing stuff :)


Vilu(Posted 2008) [#25]
Ed, I'd love to see procedural galaxy generator in your multiplayer Elite :)


WMSteadman(Posted 2008) [#26]
This is good stuff. I agree with the others tho, 3d would perfect the stars and solar systems would make it complete. I'd love to know how procedural stuff works.


Lukasha(Posted 2008) [#27]
You're a genius^^


Yahfree(Posted 2008) [#28]
is it the same stars in the same spots each time? or is it all random? so if I played your game and wanted to go back to a star... would it still be there the next time i fired it up?

it seems like an external file housing 500million unique stars would be very large.


Vilu(Posted 2008) [#29]
is it the same stars in the same spots each time? or is it all random? so if I played your game and wanted to go back to a star... would it still be there the next time i fired it up?


The star generator is semi-random, meaning the star count and coordinates are generated real-time using the sector coordinates as a base seed. It doesn't use the stock brl.random, as it would use the hardware random number generator with what the results wouldn't be consistent on different machines. The methods I've been using ensure that the universe is the same for every game, every player and every machine anywhere. Well, actually I have some doubts about machines with PPC processor, as I'm using bit shifts/rotations in the code, so the Big Endianess might cause different kind of results.

But no, there is no external file for 500 million stars :)


*(Posted 2008) [#30]
The elite game is fixed galaxies, but Star Rogue would use a procedurally generated galaxy tho


_33(Posted 2008) [#31]
Well, the beauty about a space game is that you actually invent the universe and it's content based on what you think might be a realistic probability / interpretation of what it is, or a fantasized universe with absolute wackiness. I admit that it could get redundant to search for life / minerals / energy sources / etc etc, but what if the goal is to find a host planet for the human race, as hearth has now been defuct. Your spaceship would hold all the earth animals, plants and human races. You'd roam the universe and try to find the ideal planet with ideal conditions, or find the technology to build a genesis type bomb as seen on Star Trek (Wrath of Khan?). So you're big space cruiser would of course have a batallion of ships surrounding it, and of course you would often get in trouble, depending on your luck and avenues. There is immense potential for making the space conquest games :P I was a big fan of Battlestar Galactica and Space 1999 at the time, oh and the old Star Trek. I like the novel ideas, but the newer "Next Generation" and others weren't my cup of tea. I always tought invading other solar systems wasn't nessesarely an intellectual affair.


Vilu(Posted 2008) [#32]
Interesting ideas, Jacques. Now that I have big enough universe to actually explore until I die of old age, I've though a lot about making the exploring concept actually interesting and fun, unlike in previous games of this genre. I describe some of the ideas in my development wiki, and I'm always open for further suggestions.

I'm not going for the doomsday setting of Battlestar Galactica, but I'd still like to include some form of distant planet colonization. I'm thinking of working for a government or some other faction, and help them spread their sphere of influence by finding habitable planets around the galaxy and doing some massive passenger and cargo runs to colonize them. The changes in the universe will be persistent: colonized worlds will remain that way, populations grow and shrink and economies flourish and collapse. Dynamic wars and conquests can alter the allegiance of the colonized worlds, so government boundaries are certainly not set in stone.

I'm personally partial to galactic warfare and "invading other solar systems" as you put it, but the game will not be built only around wars. The wars in this game will have subtle political twists with varying tension levels, and when grown powerful enough, even the player has the ability to influence the relations between factions and take part in the actual warfare as a privateer. Ultimately I'm aiming for some kind of a mixture between the galactic "space mogulism" of Egosoft's X-series and strategic action of Starshatter, but with a lot more lively dynamic universe and factions lead by AI with their own agendas.

I know I'm aiming unrealistically high in this early phase of the project, but the beauty of a game of this genre is that more features can be constantly added in small steps between intermediate releases. So, not all of the budding ideas have to be implemented here and now. Ananta will start small.


mikeful(Posted 2008) [#33]
Colonizing and persistent world?

Doesn't that mean you have to save state of those 500 million star systems to disk after all?


Vilu(Posted 2008) [#34]
No, only the ones that have changed from the default.

edit: remember that 99.99% of the star systems are and will remain uninhabited, and the rate at which changes occur to those procedurally generated worlds will be quite slow. The actual amount of "deviations" that have to be recorded will remain in some tens of thousands of worlds. Not a biggie.


*(Posted 2008) [#35]
Star Rogue already has a system generator that can generate 4 billion different star systems in 3d, atm im going to make it so that the entire thing will generate life sized galaxies and planets etc. I can have upto 7 suns atm (due to lighting) BUT I do plan on making my own lighting system to unlock that limit.


Rimmsy(Posted 2008) [#36]
Vilu, the latest build runs at about 2-4 frames per second on my machine. 2.6 Ghz, 2 GB RAM, GeForce 6800, Windows XP. Is that due to the galaxy available in the background?


Vilu(Posted 2008) [#37]
Yeah, it must be related to the smooth transition from star generator to galaxy view as that's the only thing I've changed since the last commit. It has some annoying bug I haven't managed to track down. It even occasionally freezes up the program to a grinding halt.

Usually the worst regression happens when you take something simple and well-behaving and try to make it look a bit prettier :P

Feel free to poke around and see what's wrong with the code while I try the 3030 Deathwar demo I downloaded yesterday ;)


edit: How does the build 99 work for you? It's the first version that has the galaxy map integrated in the game and it works flawlessly here.


_33(Posted 2008) [#38]
Vilu, you might find this interesting: http://www.jongware.com/


Jeremy Alessi(Posted 2008) [#39]
He already referenced that site above ;)


Andres(Posted 2008) [#40]
Impressive. Why won't you create a procedurally generated galaxy depth too (from the 2d image taken by hubble telescope).


Vilu(Posted 2008) [#41]
As the game itself is 2d, importing a 3d module for the sake of the starmap only would be a real waste of space and effort. I briefly thought about implementing star distance marker poles similar to Frontier's starmap, hoping to achieve a 2.5d look to it, but opted against it. A third coordinate doesn't really add any value to the game if you cannot rotate the map view in 3d as well. The map will remain 2d.

The increasing star density over bright pixels is a sufficient representation of the thickening galaxy. :)


Andres(Posted 2008) [#42]
What about the center of the galaxy, is there any giant black holes like in Milkyway? :P


Vilu(Posted 2008) [#43]
Not yet, but guess you'll just have to wait and see ;)


Krischan(Posted 2008) [#44]
Let there be light. You can build your own galaxy with this code, here is my simple version:




Mortiis(Posted 2008) [#45]
Reminds me of Maxis' Spore.


slenkar(Posted 2008) [#46]
Puki - buy blitzmax you tight git :)


Xip(Posted 2008) [#47]
Mortiis:
I was just thinking the same thing, seems very Spore inspired ;)

however, i relly like this idea, so much cool stuff can be done with this :)
keep up the good work :)


Philip7(Posted 2008) [#48]
If only someone would make a new game inspired on Ascendancy.
I love that game.

Ha, while looking for a link to some screens of that old game i found out the studio (The Logic Factory) stil exists and their actually working on Ascendancy 2 :)


Wings(Posted 2008) [#49]
I already tryed a space sim.. o quited.

after 30.000 yards of flying in space craft.. the craft started to shake and player fell out of it :)

it was when i discovered that blitz dose not support double floats.


Matty(Posted 2008) [#50]
That's easy to fix Wings, simply move everything relative to the player instead. So rather than move the player far from the origin, move everything nearer to him/further from him when moving without actually moving him.


lo-tekk(Posted 2008) [#51]
i found out the studio (The Logic Factory) stil exists and their actually working on Ascendancy 2 :)
. That's kinda cool. Best news i've heard in days.


Vilu(Posted 2008) [#52]
Damn, this has been the longest summer break ever.

I promised myself to get something concrete done by autumn. Anyone up for a little side project? ;)


Ryudin(Posted 2008) [#53]
Interesting idea. I can't imagine this thing in 3D!


_PJ_(Posted 2008) [#54]
I like it. How's the generation for consistency, though?

For example, if I zoomed in on area X and there were Z number of stars with names of A,B,C... etc.

Will I get the same results if I zoomed in elsewhere for a wqhile, then returned to Area X ?

Anyway... And this is only for information and suggestion, not any form of critique because clearly it works to a standard. I'm just wondering if you were hoping for realism?

The following may be useful:

. Approximately 57% known stars are Binary systems.

. Galactic Arbitrary Matter Distribution Potential equations: RHO(x) = -G{integral rho(x1)/|x-x1|}_>d^3.x1}

This can be reduced because the spherical potential dispersion of galactic Halo Objects is neglible compared to the thin disk, which we can trate in a planar fashion with Gauss' theorem and then, assuming a constant density of the MAIN 'bulge' of the galactic disk, mass distrbution can be overall represented by:

M(r) = 4/3.PI.r^3.RHO

M(r)=Mass Distribution by Radius r
RHO=Density

Substituting in two values for the mean density, the equation can be performed twice to solve for the stellar density of the Galactic 'bulge' radius r1 and the remaindeer of the disk, radius r2.


Vilu(Posted 2008) [#55]
The galaxy generation is not random, it is procedurally generated, meaning it is consistent over different hardware platforms as it does not use the random number generator at all.

I'm aiming for as much realism as possible as long as it supports the gameplay. For instance, the game's galaxy "only" has around 500 million stars while our Milky Way has nearly a thousandfold. Then again, my galaxy is 2-dimensional while the real Milky Way is at around 1000 light years thick to the third dimension, so one might claim my galaxy is as realistic as a 2-dimensional representation of our Milky Way could ever be.

The galaxy generator algorithm is based on the real galaxy image taken by Hubble, and the star densities vary according to the apparent brightness of the various parts of the galaxy image. Therefore there are distinctive differences in matter distribution so that galactic arms are generated more dense than the dark ridges between the arms. IMO, that's enough matter distribution variances the game needs.


puki(Posted 2008) [#56]
Blitz3D version yet? It's been nearly half a year.


Vilu(Posted 2008) [#57]
Porting oo code into procedural is something I wouldn't recommend to any sane person, but you are welcome to try! ;)


ubergeek(Posted 2009) [#58]
Hi, I'm very interested in the techniques behind this. Where could I find the source for the procedural demo? All I could find either has the game alpha with a stripped down test universe, or a procedural demo with no zooming or controls. I would really like to see the source for the demo that you originally posted.
Thanks!


Vilu(Posted 2009) [#59]
@ubergeek:

The procedural demo has zoom and scroll controls. It scrolls with arrow keys and zooms with z and x. See the readme.txt in the package.

If you know your way around Subversion repositories, you can check out the source code of the procedural demo at
http://ananta.googlecode.com/svn/tags/procedural_demo1

It requires Brucey's bah.random, bah.libxml and bah.cairo libraries to compile.

The source of the procedural demo is really "stripped down", meaning that other parts of the game are just crudely disabled and it has remnants of non-working and commented-out code in it.

The most recent SVN version of the game has a working procedural in-system planet generator as well. While you're at it, why don't you checkout the whole game? ;)


ubergeek(Posted 2009) [#60]
Thanks for the quick reply!
The procedural demo has zoom and scroll controls. It scrolls with arrow keys and zooms with z and x. See the readme.txt in the package.

I was talking about the source packages I could find. I had looked through all the repositories and I thought that what you linked to was part of a game. I'll try that one again.

I did check out the game executable demos. Looks like it has a lot of potential to become really addictive!


ubergeek(Posted 2009) [#61]
Quick question - is there a faster way of downloading trees from the repository than copy-pasting the contents of each one into files?


xlsior(Posted 2009) [#62]
Ubergeek: If you have SVN installed on your PC, you can create a folder on your PC and have SVN associate it with the folder on googlecode -- you can then synchronize the folders and it will download everything. (Including future updates, potentially)


ubergeek(Posted 2009) [#63]
Okay thanks! I guess I need to install SVN. :-)


Nate the Great(Posted 2009) [#64]
Hey Vilu. I read one of your earlier posts about acurate gravity formulas. do you still need good formulas? I have some but I have been too lazy to put them in the codearcs but if you still need them ill do it and post the code here.


schilcote(Posted 2009) [#65]
Would this be incredibly hard to do in B+ (compared to BMax) or should I buy BMax?


Vilu(Posted 2009) [#66]
@Nate the Great:

I believe gravity formulas are not a problem. The bodies rotate about their gravitational focal point in a perfectly Keplerian way. The problem comes from the timing method (deltatimer!) I stupidly decided to use in a game that so heavily rely on physics. Currently the orbits deteriorate, albeit slowly, but most probably due to the inaccuracies in the variable time step iterations. Fixed time step would get rid of the deterioration, I believe.

'Fixing' this (no pun intended) is in my todo list, but not very high in priority. The orbit deterioration shows only after a considerable amount of time running the simulation.

I've been thinking that instead of an iterative gravity calculations between planets, coming up with some formulas to calculate the orbital positions as a function of time. Fixing the actual orbits would be a good alternative to fixing the timestep. If you had any ready-made formulas for that one, I'm all ears :)

@schilcote

The galaxy generator itself is not that much relying on OO-programming, so I guess it would not be very hard to port it in B+. The galaxy code is mostly in "includes/types/entities/i_TUni.bmx"


ubergeek(Posted 2009) [#67]
I installed TortoiseSVN and tried to import the procedural_demo into a folder using the http line shown on the googlecode site, but it prompts me for a username and password. What do I use for that? I thought I could download it anonymously. (Sorry, I'm somewhat new to SVN!)


ubergeek(Posted 2009) [#68]
Nevermind, got it. For some reason I was following the instructions in the help file, and trying to use import when I needed to use svn-checkout.

Great work Vilu, this is really cool!


Chroma(Posted 2009) [#69]
I already tryed a space sim.. o quited.

after 30.000 yards of flying in space craft.. the craft started to shake and player fell out of it :)

it was when i discovered that blitz dose not support double floats.


OMG, that's the exact reason I gave up on Air War 1946. You fly the plane too far from 0,0,0 and the shakes got so bad...it was very disappointing. As soon as Max3D is useable (read as all B3D commands are added) I'll port my flight sim to that and continue on. But must finish this 2D casual game first to fund the flight sim.


Vilu(Posted 2009) [#70]
There's actually a simple programmatic trick to counter that: When passing some predefined boundary for the player coordinates, teleport the player back to 0,0,0 and move all other objects in the game world in relation to the player's new centered location.

It shouldn't look too bad if non-player objects far from 0,0,0 lose some of their floating point precision, as long as the viewed object (the player) is relatively close to the origin.


*(Posted 2009) [#71]
why not just move the objects relative to the players movement, this way the player ALWAYS stays at 0, 0, 0 but the world moves around them this way the entire universe can be any size you want.


Vilu(Posted 2009) [#72]
Yeah, you have a point there.


Damien Sturdy(Posted 2009) [#73]
I use a mix of both, sometimes a large amount of objects could cause slowdown, so i do

If player distance from centre > 10000, move player to 0 and move objects by same amount.

works a treat, and in the project i was working on prevented the shakes *and* slowdowns