libgdx:boundsinfrustrum和boundingbox未按预期工作

qltillow  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(222)

我正在从json文件加载一个box2d场景。此场景包含一个固定装置,用于标记允许摄影机在其中移动的边界框。使用这种机制可以很好地处理左下界,但是完全不能处理右上界,这很奇怪。
以下是从文件加载边界框的部分:

PolygonShape shape = ((PolygonShape) fixture.getShape());
Vector2 vertex = new Vector2();
float boundLeft = world.startX, boundRight = world.startX, boundUp = world.startY, boundLow = world.startY; // The location of the camera as initial value

for (int i = 0; i < shape.getVertexCount(); i++) { // Itarate over each vertex in the fixture and set the boundary values
    shape.getVertex(i, vertex);
    vertex.add(body.getPosition());
    boundLeft = Math.min(vertex.x, boundLeft);
    boundLow = Math.min(vertex.y, boundLow);
    boundRight = Math.max(vertex.x, boundRight);
    boundUp = Math.max(vertex.y, boundUp);
}

// Build the bounding boxes with enough thickness to prevent tunneling on fast pans
world.boundLeft = new BoundingBox(new Vector3(boundLeft - 5, boundLow - 5, 0).scl(RenderingSystem.PPM), new Vector3(boundLeft, boundUp + 5, 0).scl(RenderingSystem.PPM));
world.boundRight = new BoundingBox(new Vector3(boundRight, boundLow - 5, 0).scl(RenderingSystem.PPM), new Vector3(boundRight + 5, boundUp + 5, 0).scl(RenderingSystem.PPM));
world.boundUp = new BoundingBox(new Vector3(boundLeft - 5, boundUp, 0).scl(RenderingSystem.PPM), new Vector3(boundRight + 5, boundUp + 5, 0).scl(RenderingSystem.PPM));
world.boundLow = new BoundingBox(new Vector3(boundLeft - 5, boundLow - 5, 0).scl(RenderingSystem.PPM), new Vector3(boundRight + 5, boundLow, 0).scl(RenderingSystem.PPM));
// world is a class containing some properties, including these BoundingBoxes
// RenderingSystem.PPM is the amount of pixels per metre, in this case 64

当相机被平移时,调用以下部分:

public void pan(float x, float y) {
    Vector3 current = new Vector3(camera.position);
    camera.translate(-x, y);
    camera.update(true);
    if (camera.frustum.boundsInFrustum(world.boundLeft) || camera.frustum.boundsInFrustum(world.boundRight)) {
        camera.position.x = current.x; // Broke bounds on x axis, set camera back to old x
        camera.update();
    }
    if (camera.frustum.boundsInFrustum(world.boundLow) || camera.frustum.boundsInFrustum(world.boundUp)) {
        camera.position.y = current.y; // Broke bounds on y axis, set camera back to old y
        camera.update();
    }
    game.batch.setProjectionMatrix(camera.combined);
}
4smxwvx5

4smxwvx51#

嗯,我想出来了。猜猜我的world.startx和world.starty的定义是什么?没错,它们在屏幕坐标中:

world.startX = start.getPosition().x * RenderingSystem.PPM;
world.startY = start.getPosition().y * RenderingSystem.PPM;

这导致math.max总是选择world.startx和world.starty,因为相比之下,这些值绝对是巨大的。

相关问题