2
\$\begingroup\$

I am making a procedurally generated space exploration game in unity. I'm currently generating 5000 stars in a cube of a fixed size. Each star just gets a random 3d coordinate. What I would like to do is to dynamically load and unload stars at a certain radius from the player. This would be the maximum view distance. The algorithm that does this must be working with a seed for the universe, as the stars need to be loaded in the same spot when you revisit them.

My idea is something like in the amazing Space Engine.

I have pretty much no knowledge of algorithms that can do this, so I ask for your help.

\$\endgroup\$
2
\$\begingroup\$

Have two different kinds of star game-object:

LoadedStar
UnloadedStar

While the UnloadedStar is nothing but the transform coordinates, a very simple collider and its basic properties, the LoadedStar is the full-fledged object with all its visual representation and game-mechanics functionality.

Have a Collider with IsTrigger=true attached to your player which represents the current active zone. Whenever a UnloadedStar triggers the OnTriggerEnter event, replace it with a corresponding LoadedStar. Whenever a LoadedStar triggers the OnTriggerExit event, replace it with an UnloadedStar.

The advantage of also having active game objects for stars out of range is that you have the option to later add a simplified representation of them. You can, for example, add a simple billboard sprite to represent far-away stars or add a navigation widget for selected stars to the GUI.

the disadvantage is that your universe can not be truely infinite and not generated on-the-fly while the player is exploring. But it can still get very large without losing too much performance because the unloaded stars won't consume many resources when they are simple enough.

\$\endgroup\$
0
\$\begingroup\$

You could use playerPrefs() to save the location of the stars so you could restore it later.

\$\endgroup\$
  • 1
    \$\begingroup\$ That would work in a way, but it would mean that I will be saving thousands, maybe even millions of stars. \$\endgroup\$ – Avanak Aug 4 '15 at 11:39
  • \$\begingroup\$ You could make all the stars one object and save only the object that holds the stars itself. \$\endgroup\$ – Vadim Tatarnikov Aug 4 '15 at 11:43
  • \$\begingroup\$ I'm currently thinking about a 3d chuncked loading system, this is however not the ideal way. This means that very dim stars are also loaded when beight stars are \$\endgroup\$ – Avanak Aug 4 '15 at 12:07
0
\$\begingroup\$

What you have is two separate problems. One generating/saving/loading stars. The other is finding nearest stars. It seems that you already have star generator, which means that you can save and load seed or entire list of stars via PlayerPrefs or Application.persistentDataPath. Read about serialization if you are not sure.

To find nearest stars you can use simple brute force algorithm:

private List<Vector3> GetNearestStars(Vector3 playerPosition, List<Vector3> stars)
{
    return stars.FindAll(star => Vector3.Distance(playerPosition, star) < 100);
}

Just iterate through all coordinates and filter the ones you need. For 5000 stars that might be enough. If you need more speed, you can use something like Octree or Spatial hash.

\$\endgroup\$
0
\$\begingroup\$

Consider a grid in your space, take the grid cross points. P1 .. Pn Consider a pseudo random generation function f(x,y,z,k). Foreach P you can call f(x,y,z,k1) to get dx coord of a star from P, f(x,y,z,k2) to get dy, f(x,y,z,k3) to get dz. Repeat this incrementing k to get all stars near P. Consider your radius. For each point P on your radius generate its stars. You can add other parameters other than dx dy dz for a star (color,size,type) so you can go full procedural generation avoiding storing data. Finaly I suggest a coroutine that checks if stars are out of scope and can be deleted.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.