我正在用Java制作Voxel引擎。我有块系统工作,但我担心我没有以最有效的方式加载块。
我的块存储在哈希映射中,如下所示:
Map<Vector2f, Chunk> chunkList = new HashMap<Vector2f, Chunk>(); // Create an array for the chunks
这样我就有了一个2D数组块,可以这样访问它们:
chunkList.get(new Vector2f(0, 0));
这是我如何加载玩家周围的所有块:
public void updateChunks( Vector3f cameraPosition ) { // Update all the chunks
Thread chunkThread = new Thread() {
public void run() {
int playerChunkX = (int) Math.floor(cameraPosition.x / ( Chunk.CHUNK_SIZE * BlockVertices.BLOCK_SIZE )); // Get the current chunk that the player is in on the X - Axis
int playerChunkZ = (int) Math.floor(cameraPosition.z / ( Chunk.CHUNK_SIZE * BlockVertices.BLOCK_SIZE )); // Get the current chunk that the player is in on the Z - Axis
System.out.println( playerChunkX + ", " + playerChunkZ );
for (int i = 0; i < Camera.view_distance; i++) { // X - Axis iteration
for (int j = 0; j < Camera.view_distance; j++) { // Z - Axis iteration
if( !(chunkList.containsKey( new Vector2f( playerChunkX + i, playerChunkZ ) ) )) { // If the chunk manager doesn't have a chunk at the specified location
chunkList.put(new Vector2f(playerChunkX + i, playerChunkZ), new Chunk(perlin, playerChunkX + i, playerChunkZ)); // Create a new chunk
chunkList.get(new Vector2f(playerChunkX + i, playerChunkZ)).load(); // Load the new chunk
}
if (!(chunkList.containsKey(new Vector2f(playerChunkX + i, playerChunkZ + j)))) { // If the chunk manager doesn't have a chunk at the specified location
chunkList.put(new Vector2f(playerChunkX + i, playerChunkZ + j), new Chunk(perlin, playerChunkX + i, playerChunkZ + j)); // Create a new chunk
chunkList.get(new Vector2f(playerChunkX + i, playerChunkZ + j)).load(); // Load the new chunk
}
if (!(chunkList.containsKey(new Vector2f(playerChunkX + i, playerChunkZ - j)))) { // If the chunk manager doesn't have a chunk at the specified location
chunkList.put(new Vector2f(playerChunkX + i, playerChunkZ - j), new Chunk(perlin, playerChunkX + i, playerChunkZ - j)); // Create a new chunk
chunkList.get(new Vector2f(playerChunkX + i, playerChunkZ - j)).load(); // Load the new chunk
}
if (!(chunkList.containsKey(new Vector2f(playerChunkX - i, playerChunkZ - j)))) { // If the chunk manager doesn't have a chunk at the specified location
chunkList.put(new Vector2f(playerChunkX - i, playerChunkZ - j), new Chunk(perlin, playerChunkX - i, playerChunkZ - j)); // Create a new chunk
chunkList.get(new Vector2f(playerChunkX - i, playerChunkZ - j)).load(); // Load the new chunk
}
if (!(chunkList.containsKey(new Vector2f(playerChunkX - i, playerChunkZ + j)))) { // If the chunk manager doesn't have a chunk at the specified location
chunkList.put(new Vector2f(playerChunkX - i, playerChunkZ + j), new Chunk(perlin, playerChunkX - i, playerChunkZ + j)); // Create a new chunk
chunkList.get(new Vector2f(playerChunkX - i, playerChunkZ + j)).load(); // Load the new chunk
}
}
}
}
};
chunkThread.run();
}
正如您所看到的,我正在使用单独的线程,以获得更好的性能。 有没有更好的方法呢? 如果是这样,在播放器周围加载块的最有效方式是什么?
答案 0 :(得分:0)
我认为,通过这种方式,线程应始终处于执行状态,以“观察”玩家的移动。
我不知道对你来说是否可能,但我建议在动作本身上切换控制,这样你就可以在while(true)循环中避免这个线程。 因此,准时执行计算以加载块。