@WookiePaw said:
Hello,
this is one of my first posts on this forum. I have been working on a silly little voxel game, but need to have a worthy-of-use chunk manager (loading/unloading). Can anyone point me in the right direction as to how to do this? I have physics all setup and custom blocks and world generator. But the generator takes forever due to no chunks
Any help would be great!!
@lawsy
Thanks!!
It would be a pleasure to help out in any way I can, please understand that I am no means an expert and there is way smarter guys with tones of experience in this forum.
This topic has been discuss many times over in this post, and there are different ways to go about a chunk manager for the framework, some of the guys on this forum have used clever techniques creating a chunk manager using the framework in its current form.
As for myself, I rewritten @destroflyer cube framework and using @sleaker greedy meshing to create a voxel world that suited my project, the guys done tones of awesome work, however going down this path is a lot of work, but like a true engineer, I needed to know how it all works and have no regrets
Think it would be safe to say, if you are planning a infinite world using a chunk manager, you will at some point need to have a good threading model for loading/unloading chunks for smooth game play, and only displaying surface blocks, and watch your memory usage, when you start getting in the millions to tens of millions of blocks, memory will be very important, have a good concept or research Java garbage collector.
My method:
Chunk Manager
[java]private final ConcurrentHashMap<Vector3Int, Chunk> chunks = new ConcurrentHashMap<>();[/java]
Player position
[java]private Chunk getCurrentPosition() {
CollisionResults results = new CollisionResults();
terrainNode.collideWith(getRayCasting(Vector3f.UNIT_Y.negate()), results);
if (results.size() == 0) {
return null;
}
return getChunkControl(results.getClosestCollision().getGeometry());
}[/java]
Generate chunks around the player position
[java]private synchronized Set<Vector3Int> generator(Vector3Int location, Vector3Int size) {
Set<Vector3Int> points = new HashSet<>();
int width = size.getX(), height = size.getY(), length = size.getZ();
for (int y = height; y >= 0; y--) {
for (int x = width - width * 2; x <= width; x++) {
for (int z = length - length * 2; z <= length; z++) {
points.add(location.add(x, y, z));
}
}
}
return points;
}[/java]
Remove chunks that are no longer in the player vicinity
[java]private synchronized void removeChunkBlocks(final Set<Vector3Int> loading, final Vector3Int location) {
location.setY(0);
Set<Vector3Int> loaded = generator(location.clone(), settings.getWorldSize());
if (loaded.removeAll(loading)) {
chunkManager.removeChunks(loaded);
}
}[/java]