gpt4 book ai didi

java - Libgdx 计算 2d 中的 PerspectiveCamera View

转载 作者:行者123 更新时间:2023-12-02 00:29:45 28 4
gpt4 key购买 nike

我需要计算二维相机 View 边界,x y 宽度高度

enter image description here

在这个屏幕截图中,网格是每个1个单位,我需要计算3D View 的边界框,在上面的示例图像中,结果应该是:

float x = -3;
float y = 0;
float width = 14;
float height = 6;

最佳答案

假设你的相机始终俯视你的世界,因此地平线不可见,并且相机始终与 map 的 Y 轴平行,因此水平线永远不会弯曲,我认为这是计算 View 内最远线的宽度的问题,因为这是透视将向您显示最多水平瓷砖的地方。这将为您提供完全覆盖平截头体的平铺 map 的矩形区域,尽管您将在近角处绘制一些额外的图 block 。

private final Ray tmpRay = new Ray();
private final Vector3 tmpVec = new Vector3();
private final Rectangle visibleTilesRegion = new Rectangle();

private void updateVisibleTilesRegion (){
// Define a ray that is a projection of the direction the camera is looking onto the
// tile plane (assuming it is a Z=0 plane).
tmpRay.origin.set(camera.position.x, camera.position.y, 0f);
tmpRay.direction.set(0f, 1f, 0f);

//Find top and bottom
Intersector.intersectRayPlane(tmpRay, camera.frustum.planes[4], tmpVec);
float yTop = tmpVec.y;
Intersector.intersectRayPlane(tmpRay, camera.frustum.planes[5], tmpVec);
float yBottom = tmpVec.y;

// Find left and right at the top of the screen by intersecting that line with the left
// and right planes
tmpRay.origin.set(camera.position.x, yTop, 0f);
tmpRay.direction.set(-1f, 0f, 0f);
Intersector.intersectRayPlane(tmpRay, camera.frustum.planes[2], tmpVec);
float xLeft = tmpVec.x;
tmpRay.direction.set(1f, 0f, 0f);
Intersector.intersectRayPlane(tmpRay, camera.frustum.planes[3], tmpVec);
float xRight = tmpVec.x;

visibleTilesRegion.set(xLeft, yBottom, xRight - xLeft, yTop - yBottom);
}

关于java - Libgdx 计算 2d 中的 PerspectiveCamera View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58025552/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com