The Universe Project

Rust version has caught up to the C one

It took a while, but the Rust version of the code generating the positions of galaxies has finally reached the level of functionality of the C version. Meanwhile, I have gathered quite a bit of interesting experience, which I'm now going to share.

Rust vs other languages

Programming in Rust is nothing like programming in any other language I've had contact with (which means mainly the C family and Python). One remotely similar experience was experimenting with Haskell, but even that generally due to incompatibility between my intuition and the language (although Rust has many functional features, but as will be mentioned later, one shouldn't overuse them...).


Progress of the Universe project

I made some progress in rewriting the Universe project in Rust during the last few days.

Before I started with the project itself, I had to make some preparations. The order of magnitude of the numbers appearing in the program caused the need for the GMP and MPFR libraries, allowing for calculations on arbitrarily big numbers. I also used the xxhash algorithm. The problem? All 3 pieces of code were written in C.

It's not actually that big of a problem, since one of the advantages of Rust is the ease of using it with C libraries. The only thing to do was to find or create modules (or, in Rust terms - crates) which would make it more straightforward, so I started searching.

I focused on GMP and MPFR first. There were 2 crates for GMP and one for MPFR, created by the author of one of the GMP crates. After having a look at them I decided that the GMP module without corresponding MPFR one is more straightforward to use, which left me with the task of writing my own crate for MPFR. Moreover, the GMP module was old and didn't even compile with the latest version of the language.

I started working. Fixing the GMP crate turned out to be tedious, but not very hard. Writing the MPFR module was similar, but needed a bit more work (like writing many similar functions that only served to call their C counterparts...). Both modules are available on GitHub (click, click). I created a pull request for the author of the GMP crate, but he turned out not to be interested in maintaining the project any longer.

Then it was time for xxhash. Here, like with GMP, a crate existed, but it didn't compile. A few hours of work later everything was fixed and the library worked.

So, now I have working dependencies for the project. I also started to rewrite the code of the project itself, but for now only a small part of it is done. More news about the project progress - (hopefully) soon.

Generating the structure of the universe

The structure of space

When creating a universe, you have to begin somewhere. A good starting point is to define its shape.

The most convenient and probably the most obvious shape is a cube. Each of the three coordinates is then a number from the same range and it is very easy to make it so that there are no boundaries - it is enough to add a condition that leaving the cube on one side is equivalent to entering it on the other side. It is similar to the well-known Snake game, where the snake leaving the screen on the right returned from the left - just in three dimensions.

So we have a cubic space made of points described by three numbers: x,\; y,\; z\; \in (a,b). A question arises: what data type will be best for representing the coordinates of a point? The answer requires us to check the size of the numbers involved first.

The Universe Project

The project, which I temporarily call "Universe" (a better name would be nice, but it isn't urgent), aims to create an interactive universe in a realistic scale - that is, containing billions of galaxies, consisting of billions of stars themselves, some of which have planets, some being parts of multiple-star systems etc.

The inspiration comes from a few sources:

  • Space Engine - a program which lets you travel through a realistic universe
  • Minecraft - a game creating almost unlimited possibilities for building and interacting with the world
  • Wurm Online - a game similar to Minecraft in some aspects, also creating great possibilities of shaping the world

In short - the purpose would be to achieve interactiveness similar to that of Minecraft or Wurm, but in a universe the size of Space Engine.

Of course, this is a huge endeavour, so the project will probably take years, if I manage to finish it at all. Thus, I'm going to make small steps forward, until it takes some interesting shape sometime in the future, and describe the progress here, in the category "The Universe Project".