Hello!
I am working on a voxel project and I am looking at the task of pathfinding. I implemented a Dykstra algorithm in three-dimensional space and it appears to be working alright. The main snag was that when the path was calculated for just one entity, the game would experience a sever frame drop for about half a second.
My next step was to move the pathfinding to a Job to perform the pathfinding on a separate thread (as I had heard was a popular approach), and this did not seem too daunting since I already have the block/chunk generation done via threading anyways. Here is my strategy:
- Initialize a NativeParallelHashMap of a fairly large size.
- During chunk generation, pass each block type (enum) to the hash map with its world coordinates (Vector3) as the key.
- Pass this hash map to the pathfinding job for navigation.
However, I get stuck on step two when passing the blocks to the hash map. After the first two chunks, the execution begins to exponentially slow down and the project is unplayable in less than three seconds. Are NativeParallelHashMaps not good for storing large amounts of data?
Code:
Block generation (chunk sizes are currently 16 x 16 x 100)
for (int xIndex = 0; xIndex < jobChunkWidth; xIndex++) {
for (int zIndex = 0; zIndex < jobChunkWidth; zIndex++) {
int groundHeight = (int)GetHeightForCoord(new Vector2(jobAnchorPoint.x + xIndex, jobAnchorPoint.y + zIndex));
for (int yIndex = 0; yIndex < jobChunkHeight; yIndex++) {
int blockIndex = ConvertVectorToInt(new Vector3(xIndex, yIndex, zIndex));
if (yIndex < groundHeight - jobStoneLine) {
jobBlocks[blockIndex] = BlockType.Dirt;
} else if (yIndex < groundHeight) {
jobBlocks[blockIndex] = BlockType.Dirt;
} else if (yIndex == groundHeight && yIndex <= jobWaterLevel) {
jobBlocks[blockIndex] = BlockType.Sand;
} else if (yIndex == groundHeight && yIndex > jobWaterLevel) {
jobBlocks[blockIndex] = BlockType.Grass;
} else {
jobBlocks[blockIndex] = BlockType.Air;
}
jobBlockLibrary[ConvertIntToWorldVector(blockIndex)] = jobBlocks[blockIndex]; // Adding the block to the hash map.
}
}
}