Loading outward from a point

Started by
8 comments, last by coope 7 years, 11 months ago

So, I've set up a script that finds a players position relative to the chunks that make up my world. I was wondering If there was a way to get from a center point, and load outwards from the player - something like layers.

Code for those who are wondering:


using UnityEngine;
using System.Collections;

[System.Serializable()]

public class LoadChunks : MonoBehaviour {

    public Planet planet;
    public bool finished = true;

    public float planetx;
    public float planety;
    public float planetz;

    public float x;
    public float y;
    public float z;

    float lastx;
    float lasty;
    float lastz;

    public float length;

    public float Range = 32;
	
	// Update is called once per frame
	public void Update () {

        if (finished == true)
        {
            if (planet == null)
            {
                GetPlanet();
                Vector3 pos = planet.transform.position;
                planetx = pos.x;
                planety = pos.y;
                planetz = pos.z;
                length = planet.planetSize / 16;
            }

            //Getting player position
            x = this.transform.position.x;
            y = this.transform.position.y;
            z = this.transform.position.z;

            //Check if the player has moved, skip a few calculations if they didn't
            if (x != lastx || y != lasty || z != lastz)
            {
                //Normalizing position with planet position
                x = x - planetx;
                y = y - planety;
                z = z - planetz;

                //Converts into chunk position
                x = Mathf.FloorToInt(x / (float)localVars.chunklength);
                y = Mathf.FloorToInt(y / (float)localVars.chunklength);
                z = Mathf.FloorToInt(z / (float)localVars.chunklength);

            }

            //Make sure the player is within the chunks ranges
            if (x > -Range && y > -Range && z > -Range && x <= length + Range && y <= length + Range && z <= length + Range)
            {

                LoadChunksInRadius(x, y, z);
                lastx = x;
                lasty = y;
                lastz = z;

            }
        }
	}

    void LoadChunksInRadius(float x, float y, float z)
    {
        



    }

    void GetPlanet()
    {
        //TODO: Make this get closest planet once multi-planet functionality is added
        planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<Planet>();
    }

}

I'm making an open source voxel space game called V0xel_Sp4ce! Help me out with the repository here: https://github.com/SpikeViper/V0xel_Sp4ce

Advertisement

Hmmm loading chunks is your logic, you get the closest x,y,z position, just load chunks that are close to this poistion.

I seriously can't understand what you want.

Okay, Ill explain in more detail. I calculate where the player is in chunk position, so I know which chunk they are in. So, what I want to do is:

1. Load that chunk

2. Load the chunks surrounding that chunk

3. Repeat 2 with the new chunks (imagine an expanding circle)

Even more example:

4 4 3 4 4

4 3 2 3 4

3 2 1 2 3

4 3 2 3 4

4 4 3 4 4

(Numbers in order of loading)

I'm making an open source voxel space game called V0xel_Sp4ce! Help me out with the repository here: https://github.com/SpikeViper/V0xel_Sp4ce

Is this related to your other thread? Without knowing how you store your chunks I can't effectively suggest a way to load chunks. Are we talking file io, coordinate seeded procedural generation, an STL container?

edit> what do you mean by 'load'?

Is this related to your other thread? Without knowing how you store your chunks I can't effectively suggest a way to load chunks. Are we talking file io, coordinate seeded procedural generation, an STL container?

edit> what do you mean by 'load'?

I have loading set up, I just call planet.load(Position(Its a Vector3)); That loads the chunk. I just need to find the positions to load, preferably going from the player's position outward as explained above.

I'm making an open source voxel space game called V0xel_Sp4ce! Help me out with the repository here: https://github.com/SpikeViper/V0xel_Sp4ce

Okay, Ill explain in more detail. I calculate where the player is in chunk position, so I know which chunk they are in. So, what I want to do is:

1. Load that chunk

2. Load the chunks surrounding that chunk

3. Repeat 2 with the new chunks (imagine an expanding circle)

Even more example:

4 4 3 4 4

4 3 2 3 4

3 2 1 2 3

4 3 2 3 4

4 4 3 4 4

(Numbers in order of loading)

The generic way to expand outwards through a graph is Dijkstra's algorithm for a shortest path tree.

You would load the player's chunk, then stick all of its neighbors into a queue that's sorted by distance to the player. Then keep taking new chunks out of the queue, loading them, and putting the neighbors into the queue (while keeping track of which ones were already loaded, so you don't load them again). You can stop when you've loaded enough chunks or when the queue is empty.

The generic way to expand outwards through a graph is Dijkstra's algorithm for a shortest path tree.

You would load the player's chunk, then stick all of its neighbors into a queue that's sorted by distance to the player. Then keep taking new chunks out of the queue, loading them, and putting the neighbors into the queue (while keeping track of which ones were already loaded, so you don't load them again). You can stop when you've loaded enough chunks or when the queue is empty.

I would tend to agree that would be a smart way to do it. Seeing as it is such a useful algorithm in general it makes perfect sense to apply it to any situation it would be warranted and even desired.

I would have probably taken to pen and paper to see if I could derive an algorithm from the basic mathematics of your requirements, but Dijkstra is a far better course of action. Far better to use a wheel you've got than to craft a new one.

The generic way to expand outwards through a graph is Dijkstra's algorithm for a shortest path tree.

You would load the player's chunk, then stick all of its neighbors into a queue that's sorted by distance to the player. Then keep taking new chunks out of the queue, loading them, and putting the neighbors into the queue (while keeping track of which ones were already loaded, so you don't load them again). You can stop when you've loaded enough chunks or when the queue is empty.

I would tend to agree that would be a smart way to do it. Seeing as it is such a useful algorithm in general it makes perfect sense to apply it to any situation it would be warranted and even desired.

I would have probably taken to pen and paper to see if I could derive an algorithm from the basic mathematics of your requirements, but Dijkstra is a far better course of action. Far better to use a wheel you've got than to craft a new one

How would I use this algorithm to do what m asking? I see it's used for pathfinding, and I've found C# code samples, but I don't see how to make this work for my problem. Sorry, I'm not very experienced with complex algorithms yet.

I'm making an open source voxel space game called V0xel_Sp4ce! Help me out with the repository here: https://github.com/SpikeViper/V0xel_Sp4ce

Thanks guys, made my own formula to do this and it works great!

I'm making an open source voxel space game called V0xel_Sp4ce! Help me out with the repository here: https://github.com/SpikeViper/V0xel_Sp4ce

How would I use this algorithm to do what m asking? I see it's used for pathfinding, and I've found C# code samples, but I don't see how to make this work for my problem. Sorry, I'm not very experienced with complex algorithms yet.

Since you've already got something that works this is just for the sake of answering your question.

Dijkstras_progress_animation.gif

This image is from the Wikipedia article on the algorithm. Notice how the expansion resembles a growing circle.

I'd recommend you take some time in the future to learn how to program the A* algorithm from scratch. It can be used to solve a myriad of problems not just pathfinding in the sense we think about it.

I know a guy who used it to solve a word problem.

The problem he faced was something like this:

What are the fewest changes you can make to get from Word <A> to word <B>.

Now I can't recall the restrictions he was presented with, but the point is you can solve word problems with a pathfinding algorithm. To use an algorithm you know, to creatively solve a problem it wasn't meant to solve, requires you to be creative and heavily modify said algorithm. The same sort of thing applies here.

First step: learn the algorithm.

Second step: show your code who's boss.

This topic is closed to new replies.

Advertisement