r/howdidtheycodeit • u/dotpusheria • Oct 19 '23
Question How to Simulate Space in Game Engines like No Man's Sky
I've been making some researches for a while for a idea of mine which probably I'll never get to do but I want to learn and try in order to improve my coding skills.
The Question I have is how to simulate space in Unreal Engine like in No Man's Sky. I know about procedural world generation using seeds but I couldnt find any clear info about how to handle the space between the planets. I was thinking about hidden cutscenes to handle loading new solar system while jumping in between solar systems like in Elite Dangerous but I have no clue about this. At first I thought of using LODs and really fast moving space ships but that doesnt really sound like an idea considering in editor there will be huge empty gaps that will most likely create issues.
I also want to know what should be the best way to handle loading new solar systems. Should I remove everything on a map and spawn the new planets in according to solar system or should I load a new map?
Thanks in advance!
7
u/Oarc Oct 20 '23
In addition to the other comments regarding a floating origin, definitely check this out: https://youtu.be/mXTxQko-JH0?si=ICuUlKa9jQSWyQ-D
KSP solves a lot of these problems and that talk gives an idea of how to solve them.
But yeah, the main concepts you'll want to learn are "floating origin" and something like a "cube sphere quad tree" for the planet terrain (there are lots of different approaches).
20
u/[deleted] Oct 19 '23 edited Oct 19 '23
To avoid issues with floating point precision failing you, move the "world" around the player camera (or per /u/ctothel 's suggestion, keep it confined to a limited range before resetting the engine units). Another user suggested a chunk based approach, which is excellent and works with this.
Strongly suggest pen&paper for outlining your data needs at this point. Figure out everything you need to express the world. Keep it simple, start with figuring out what you need to describe space in this chunk system, and then how to describe how much you can see.
Once your world data format is ready, implement basic "movement". You'll figure out very quickly the camera is what remains at the origin, and you will be moving the character by translating your "world" coordinates back to the engine's world coordinates.
Things like gigantic planets and how much "space" they fill is ultimately an illusion. Do not try to map engine world units 1:1 to "real world" sizes. You can do a lot of easy things to fake the "approach" and the increasing size of the planets.
6
u/ISvengali Oct 19 '23
Since youre using Unreal, I would definitely ask this on the both the Unreal Engine subreddits, as well as the very good Unreal discord
My brain isnt currently in Unreal mode, but youll be wanting to load and unload chunks of the world in order to make it seamless. To start out I could be worth doing a scene load and unload from chunk to chunk to start prototyping your idea
2
u/leorid9 Oct 20 '23
Limitations of any game engine:
Space: about 3000 units away from the origin of your coordinate system, things start to get buggy. Floats only have 7 digits (iirc), the more digits you need at the front, the more you lose at the end, resulting in less and less precision the further you move away from your origin. At some point your maximum precision is decimeters, collision tests will all fail at this point.
Nr of Objects: engines partially iterate over existing objects, if the list of objects contains 100.000+ enties, it's basically game over for your performance.
Memory: A game doesn't run on your hard drive, it runs on your RAM (and VRAM, the GPU RAM). Users only have about 8-16GB RAM usually. If you fill that up with data (sounds, textures, meshes, object positions,..) the game will crash or run extremely slow.
Render Performance: At some amount of objects on your screen, it will lag - nanite can help with that, but it also has some limitations (mostly related to memory).
CPU Performance: Basically calculations per frame and a game runs usually at 60 frames per second. If you have too many physics objects, to many loops, too many calculations or basically just too many lines of code running, it will lag.
With all of that in mind, it should be clear that you can't just build a planet using the terrain tool and then go a few million kilometers away from that point and start there with another planet. That's just not possible.
1
u/pillow_paper Oct 21 '23
For the Unreal Engine, look up "World Composition". It is Unreal's tool for managing massive worlds with level streaming.
For the other commenters' discussions on the floating point precision, World Composition has "World Origin Shifting" which solves just that. It moves the floating origin (depending on distance away) closer to the player location to prevent floating precision errors.