I'm making a small voxel-based multiplayer WebGL game with a Node.JS server which handles player positions and sends terrain chunk data to the clients. This is an example of how I'm currently sending the chunks:
var viewDistance = 6;
for (var x = playerPosX - viewDistance; x < playerPosX + viewDistance; x++)
for (var z = playerPosZ - viewDistance; z < playerPosZ + viewDistance; z++)
for (var y = playerPosY - viewDistance; y < playerPosY + viewDistance; y++) {
client.send( getChunk(x, y, z) );
}
This simply loads and sends the chunks within the view distance, however I'm looking for a way to prioritize sending the chunks nearest to the player first. Obviously it's no good loading chunks that are far away before loading the one the player is standing on.
Assuming playerPosX
, playerPosZ
and playerPosY
store the positions of the player and client.send( getChunk(x, y, z) )
will send the chunk, does any one have any ideas how I would do this? Thanks.