简体   繁体   English

块不加载

[英]Chunks don't load

I try to create a game like minecraft but I have a problem with chunk loading.. Right now, I only check if the player is in an unloaded chunk and if this is true, I load this chunk: 我尝试创建类似我的世界的游戏,但是我在块加载方面遇到了问题。.现在,我仅检查玩家是否在未加载的块中,如果是,则加载此块:

Vector2f cameraPos = new Vector2f(camera.getTransform().getPos().getX(), camera.getTransform().getPos().getZ());
Vector3f currentChunkPos = new Vector3f((float) Math.floor(cameraPos.getX() / Chunk.WIDTH - 0.499f) * Chunk.WIDTH, 0, (float) Math.floor(cameraPos.getY() / Chunk.LENGTH - 0.499f) * Chunk.LENGTH);
System.out.println(currentChunkPos);
if(findChunk(currentChunkPos) == null) {
    loadedChunks.add(new Chunk(currentChunkPos, noise));
}

findChunk(): findChunk():

public Chunk findChunk(Vector3f pos) {
    for(Chunk chunk : loadedChunks) {
        Vector3f cpos = chunk.getTransform().getPos();
        if(pos.getX() == cpos.getX() && pos.getY() == cpos.getY() && pos.getZ() == cpos.getZ()) {
            return chunk;
        }
        if((pos.getX() < cpos.getX()) || (pos.getZ() < cpos.getZ()) || (pos.getX() > cpos.getX() + Chunk.WIDTH) || (pos.getZ() > cpos.getZ() + Chunk.WIDTH)) {
            continue;
        }
        return chunk;
    }
    return null;
}

But the problem is, sometimes the chunk you are in gets loaded directly when you go in, sometimes the chunk gets loaded if you are in the middle and half of the chunks don't load... Does someone know what I'm doing wrong? 但是问题是,有时您进入时会直接加载其中的块,如果您位于中间,则有时会加载该块,而一半则不会加载...有人知道我在做什么吗?错误?

As you're only storing a single X , Y and Z position for the chunk plus the sizes then comparing the users position in the world, chunks will only be loaded in front of the user if the user is moving 'forward' in the correct X , Y and Z position, otherwise your if condition won't be met because the user's X will be higher than the chunk X . 因为您只存储块的单个XYZ位置以及大小,然后比较用户在世界上的位置,所以只有当用户以正确的方向“向前”移动时,块才会加载到用户的前面XYZ位置,否则将不满足您的if条件,因为用户的X将高于块X

What you need to do is have each chunk store a 'Chunk number' for X and Y . 您需要做的是让每个块都为XY存储一个“块编号”。 Let's say your chunks are 50x50, then you might have them numbered like so: 假设您的块是50x50,那么您可能将它们编号为:

块数

Then to work out the chunk from the world position you'd use: 然后从您将要使用的世界位置算出一部分:

final int chunkSize = 50;

final int[] worldPositions = {241, 186, 30};
final int chunkX = worldPositions[0] / chunkSize;
final int chunkY = worldPositions[1] / chunkSize;
System.out.println("X: " + chunkX + ", Y: " + chunkY); //Output: X: 4, Y: 3

So you can then load that chunk and every chunk that surrounds it too by adding and subtracting 1 from each value. 因此,您可以通过从每个值中减去1来加载该块以及围绕它的每个块。 You also shouldn't need a chunk Z as a chunk is everything in the X and Y area regardless of how high it is. 您也不需要块Z因为块XY区域中的所有东西都不管它有多高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM