max-mapper / voxel-engine

3D HTML5 voxel game engine
http://maxogden.github.com/voxel-engine
BSD 3-Clause "New" or "Revised" License
1.29k stars 220 forks source link

Chunks disapear #99

Open CapsE opened 10 years ago

CapsE commented 10 years ago

I wrote some code for a free camera movement without a player. The voxel-engine code states that if there is no player voxel.js will take the position of the camera to find chunks that are far away and remove them. So far everything is working fine BUT the chunks won't load when the camera gets close to them again. I tried to force voxel.js to show them but that won't work too. cp = game.camera.position.toArray(); var chunkX = Math.floor(cp[0]/32); var chunkY = Math.floor(cp[1]/32); var chunkZ = Math.floor(cp[2]/32);

console.debug("Update Map around: " + [chunkX, chunkY, chunkZ].toString());

game.showChunk(game.getChunkAtPosition([chunkX, chunkY, chunkZ]));
game.showChunk(game.getChunkAtPosition([chunkX + 1, chunkY, chunkZ]));
game.showChunk(game.getChunkAtPosition([chunkX + 1, chunkY , chunkZ - 1]));
game.showChunk(game.getChunkAtPosition([chunkX, chunkY, chunkZ -1]));
game.showChunk(game.getChunkAtPosition([chunkX + 1, chunkY, chunkZ + 1]));
game.showChunk(game.getChunkAtPosition([chunkX -1, chunkY, chunkZ - 1]));
game.showChunk(game.getChunkAtPosition([chunkX -1, chunkY, chunkZ]));
game.showChunk(game.getChunkAtPosition([chunkX -1, chunkY, chunkZ + 1]));
game.showChunk(game.getChunkAtPosition([chunkX, chunkY, chunkZ + 1]));

I used game.setBlock([msg["x"],1 ,msg["y"]], c) to create the blocks. Is this the right way or will it just display the blocks until the chunk is hidden the first time?

deathcap commented 10 years ago

By default voxel-engine has no persistence layer; setBlock() only modifies the voxel in a loaded chunk but once chunks are unloaded they are not saved anywhere. You can handle this by persisting the chunks to network/disk/memory as they unload, or just regenerate them again. Try using the missingChunk event on game.voxels, and the game.showChunk() method; this example might be helpful: https://github.com/deathcap/voxel-land

CapsE commented 10 years ago

Thanks for the feedback. That explains alot :D