0

I trying to make a game where player only move forward in an infinity map, and the path (just thing of them like points, the path is only the visual) is procedurally generated. I want those path to have different length (something like the tree of life, but only branches of the selected path are generated).


This is how I generate branches without overlap:

List<Vector3> everyPos; //predetermined position

public void Spawn(int amount)
{
    List<Vector3> possiblePos = new List<Vector3>(everyPos);

    for (int i = 0; i < amount; i++)
    {
        int index = Random(0, possiblePos.Count);          //Find a random position
        SpawnObjectAt(currentPosition+possiblePos[index]));//Create a point there
        possiblePos.RemoveAt(index);                       //Remove that position from the list
    }
}

The problem is , look at this image(I can't embed image yet):

Red is where player start, green is possible spawn position in the first move.

If there are 2 point spawned at 1 and 2, player choose point1, then the possible position in the second time will be a point in the black zone, which include point2, so if I keep continue there will eventually overlap.


How can I avoid this? I'm making a mobile game so I don't want to cache every single point. Any help would be really appreciated! Thanks!

This is a small web game that have somewhat similar mechanic to what I trying to achieve: newgrounds.com/portal/view/592325/

leloctai
  • 191
  • 3
  • 14
  • What language are you using? Could you include either some code or some images to explain what you mean? – bigcodeszzer Nov 20 '15 at 16:57
  • I'm using C# but any language is fine, actually I don't need any code, just a direction. About image I included a pic above, and a simple game that doesn't take more than a minute to try – leloctai Nov 21 '15 at 02:50
  • I played the game, but I don't see what you mean by the path overlapping, but if you want to generate a path 'like' that it shouldn't be too difficult. – bigcodeszzer Nov 21 '15 at 05:11

1 Answers1

0

This is an attempt here to answer, but honestly, you need to provide more information.

Depending on the language you are writing in, you can handle this differently. You may need dynamic allocation, but for now lets assume, since your idea is quite small, that you can just do one large array predefined before compile time.

I assume you know how to make an array, so create one with say, 500 length to start. If you want to 'generate' a link like they did in that game, you simply need a random function, (there is a built in library in pretty much every language I think) and you need to do a little math.

Whatever language you use will surely have a built in graphics library, or you can use a popular easy to use one. I'll just draw a picture to make this clear.

enter image description here

There are a number of ways you can do this mathematically as shown in the image, using angles for example, the simplest way, however, is just to follow the boxes.

If you have worked with graphics before, you know what a vector is, if not, you will need to learn. The 9 vectors presented in this image (0,1) (1,0) (1,1) etc. can be created as vector objects, or even stored as individual ints.

To make your nodes 'move' into another path, you can simply do a rand 1-9 and then correlated the result to one of 9 possible vectors, and then add them to your position vector. It is easiest to do this in array and just use the rand int as the index. In most c derived languages you do that like this:

positionVector += changeVectorArray[rand(1,9)];

You then increment your position vector by one of the 9 vectors as shown above.

The simplest way of making the 'path' is to copy the position before you add the change vector, and then store all of the changes sequentially in another 'path' array.

To show the path on screen, simply draw a line between the first and second, second and third, third and forth elements of your path array. This formula (of joining lines) is discrete mathematics if I'm not mistaken, and you can do much more complicated path shapes if you want, but you get the gist.

That should at least start you off. Without more info I can't really help you.

I could go off on a tangent describe a bunch of different ways you can make this happen differently but its probably easier if you just ask for specifics.

enter image description here

EDIT>>>

Continuing with this answer, yes, looking at it now, the nodes can definitely overlap. To solve this problem you could use collision detection, every time you generate a new 'position', before adding it and drawing the line you have to loop through your array like this:

boolean copy = true;
for(int i = 0; i < getLength(pathArray); i++){

     if( newVector == pathArray[i]){
        copy=false;
     }
}

Then of course, if copy still is true, copy the new position int the pathArray. NOTE: this whole solution is sloppy as hell, and as your array gets larger, your program is going to take longer and longer to search through that loop. This may not also guarantee that the path goes in one direction, but it is likely. And note that the lines will still be able to overlap each other, even though the position vectors can't be on top of one another.

All this considered, I think it will work, the optimization is up to you. I would suggest that there is probably a much more efficient solution using a discrete formula. You can also use such a formula to make the path go in particular directions and do other more complicated things.

You could also quite easily apply constraints on your random rolls if you want to make the path go in a particular direction. But there are so many ways of doing this I can't begin to explain. You could google path-finding algorithms for that.

Good luck.

bigcodeszzer
  • 916
  • 1
  • 8
  • 27
  • Thanks you for your answers, I updated my question. About the collision detection, I don't think it a good idea, it like cracking password by brute force. I believe there must be a better way of doing this. – leloctai Nov 21 '15 at 07:56
  • No problem. You can accept the answer by clicking the green check. – bigcodeszzer Nov 22 '15 at 00:20
  • One simple way of solving the overlap would be limit the random roll to either (+1,0) (0,+1) or (+1, +1) - This will mean it can only right, up, or upright. Using those constraints, there shouldn't be any overlap. – bigcodeszzer Nov 22 '15 at 00:23
  • The only other way would be with collision detection or pathfinding, that I know of. – bigcodeszzer Nov 22 '15 at 00:23