- Home /
My precedural Algorithm for Tidy TileMapper is not working!
I've recently bought Tidy Tilemapper, and I am now trying to create a procedural tile world with it. All I want is to remove any tiles too far away to be useful, and only show those close to my character. For testing purposes I have been using arbitrary numbers...
What I do (or am trying to do) is create a radius where I check all tiles within. I then have a smaller radius within that, and I remove everything there's outside the small radius, and inside the big radius. This way I avoid accessing the rest of the map, outside the big radius. Everything inside the small radius should be drawn to the screen. At the moment my map is pretty simple, if the value in the 2 dimensional array is 1, it should draw something.
When I launch the Scene I make the player a child of the Blockmap that is created. I then compare localPosition values to the position of the blocks. I think this is here my problem starts - Everything tile(block) that is created, are created towards the negative direction of Worldspace (and local space). I've messed around a lot with the following code on that account (I've burned out my brain :P). I'm spawning my player at X=0, and Y = half the height of the map. While I fall down to the map, if I move out in the positive direction on the X-axis (off the map) it will actually remove some of the tiles that it is supposed to. But it does not redraw any of them. If I move around on the world that is created for testing purposes, it does not remove anything in the "large radius", and doesn't create anything either.
I've taken the Perlin and level generation examples that are included in Tidy Tile Mapper, and modified them.
First off the Start and update methods, just so you can see the order of things. ConstructInitialWorld method prepares the Block Map, and creates the int array containing the map via the GetLevelMap method.
// Use this for initialization
void Start ()
{
ConstructInitialWorld();
}
void Update()
{
DrawMap();
}
//We'll construct the map here
private void ConstructInitialWorld()
{
map = GetLevelMap(levelWidth, levelHeight);
//We'll go ahead and create our map now
createdMap = BlockUtilities.CreateBlockMap(mapName,tileSize,chunkWidth,chunkHeight,BlockMap.GrowthAxis.Up);
SpawnPlayer(new Vector2(0, levelHeight / 2));
//And now we refresh our map
BlockUtilities.RefreshMap(createdMap,randomiseInitialMap);
}
public int[,] GetLevelMap (int levelWidth, int levelHeight)
{
map = new int[levelWidth,levelHeight];
//We will randomise the y value that we pass to the perlin function
//This will result in a random level everytime
float yOffset = UnityEngine.Random.value;
for(int x =0; x < levelWidth; x++){
//We'll get our perlin noise value -
//normalizing our x to pass it as a parameter
float p = Mathf.PerlinNoise((float)x/(float)levelHeight,yOffset);
//Given this value, we'll get our height cap
//And set it to allow for an empty column at the top, and
//assure it doesn't leave gaps in the bottom
int ny = (int)(p * (float)levelHeight-2)+1;
//Given this, we'll set all values below this number to be 'true'
for(int y = ny; y < levelHeight; y++)
{
map[x,y] = 1;
}
}
return map;
}
My guess is the problem is in the following code:
private void DrawMap()
{
//Sets an arbitrary radius to "scan" around the player.
for(int x = (int)pc.transform.localPosition.x - 30; x < pc.transform.localPosition.x + 30; x++)
for(int y = (int)pc.transform.localPosition.y - 30; y < pc.transform.localPosition.y + 30; y++)
{
if(x < pc.transform.localPosition.x - 10 || x > pc.transform.localPosition.x + 10 || y < pc.transform.localPosition.y - 10 || y > pc.transform.localPosition.y + 10)
BlockUtilities.RemoveBlockFromMap(createdMap, x, y, 0, false,true);
if(+x > map.GetLength(0) || +x < 0 || +y > map.GetLength(1) || +y < 0)
break;
if(map[+x,+y] == 1)
{
Debug.Log("If succeeded");
Block b = GameObject.Instantiate(blockPrefab) as Block;
BlockUtilities.AddBlockToMap(createdMap,b,true,0,false,+x,+y,0, true, false);
}
else if(useEmptyBlocks)
BlockUtilities.AddBlockToMap(createdMap,null,true,0,false,+x,+y,0,true,true);
}
}
I know this is a heavy read, and thanks for reading it- But I really hope someone has an idea or can actually see what I am doing wrong.
Here's a link to a little documentation for Tidy TileMapper http://tools.dopplerinteractive.com/
Answer by Bunny83 · May 02, 2012 at 01:26 PM
I don't have this toolkit, but it seems that it's already managed in chunks, so basically you should create / delete whole chunks instead of manipulating single block in those chunks...
It's actually very similar to the concept of minecraft ;) Just in case you haven't seen this yet, there is a free Minecraft starter package for Unity3d.
Btw, it looks like you create your map array ahead of time, wouldn't it make more sense to generate the terrain when it's needed?
Answer by Mekuri · May 02, 2012 at 04:36 PM
Nice, I will look into that package. I've already thought of operating in chunks, but there doesn't seem to be out of the box methods to add or remove chunks. But whether I figure out to handle it chunkwise or not, I'm still ending up with the same issues with my algorithm :)
Answer by Mekuri · May 02, 2012 at 04:36 PM
Nice, thanks a lot for the link, I'll check it out.
I have already considered working in chunks, but I it seems there are no "out of the box" add or remove methods for chunks. I will probably do a method for this later, but this will not fix my currently bugged algorithm, that I've stared myself blind on :)
Thanks for the hints, I'll definately check out the Minecraft kit.