首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Libgdx在2d中计算PerspectiveCamera视图

Libgdx在2d中计算PerspectiveCamera视图
EN

Stack Overflow用户
提问于 2019-09-20 17:26:53
回答 1查看 186关注 0票数 1

我需要计算2d,x y widthheight中的摄像机视图边界

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

代码语言:javascript
复制
float x = -3;
float y = 0;
float width = 14;
float height = 6;
EN

回答 1

Stack Overflow用户

发布于 2019-09-21 01:59:56

假设你的相机总是向下看你的世界,所以地平线是不可见的,并且相机总是平行于地图的Y轴,所以水平线永远不会弯曲,我认为这是一个计算视野中最远的线的宽度的问题,因为在那里透视将水平地向你显示最多的瓦片。这将使您获得一个矩形区域的平铺贴图,该区域完全覆盖了圆锥体,尽管您将在近角绘制一些额外的平铺。

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58025552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档