Libgdx-旋转边界框的最小值和最大值 [英] Libgdx - min and max of a rotated Bounding Box

查看:107
本文介绍了Libgdx-旋转边界框的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行下面的简单代码:

I am running the simple code below:

BoundingBox bounds = new BoundingBox();
Vector3 vmin = new Vector3();
Vector3 vmax = new Vector3();

vmin.x = -1;
vmin.y = -2;
vmin.z = 0;  
vmax.x = 1;
vmax.y = 2;
vmax.z = 0;
bounds.set(vmin,vmax);

Matrix4 mrot = new Matrix4();
mrot.setToRotation(0, 0, 1, 90);
bounds.mul(mrot);

Gdx.app.log("xxx","minx " + bounds.min.x);
Gdx.app.log("xxx","maxx " + bounds.max.x);
Gdx.app.log("xxx","miny " + bounds.min.y);
Gdx.app.log("xxx","maxy " + bounds.max.y);
Gdx.app.log("xxx","dimx " + bounds.getWidth());
Gdx.app.log("xxx","dimy " + bounds.getHeight());

日志显示:

minx -2.0//正常,如预期
maxx 2.0//符合预期,
miny -2.0//我期望-1!
maxy 2.0//我期望1!
dimx 4.0//符合预期,
dimy 4.0//我期望2!

minx -2.0 // ok, as expected
maxx 2.0 // ok, as expected
miny -2.0 // I would expect -1 !
maxy 2.0 // I would expect 1 !
dimx 4.0 // ok, as expected
dimy 4.0 // I would expect 2 !

我的理解是,上面的代码应该简单地围绕Z轴旋转90°的2D矩形.日志中的结果表明情况并非如此(即y坐标没有变化)

My understanding would that the code above should simply rotate a 2D rectangle of 90° around the Z-axis. Results from the Log show that it is not the case (i.e. no change to the y coordinates)

有人可以帮助我了解我在哪里弄错了吗? 非常感谢

Does anyone can help me understand where I am mistaken? Many thanks

推荐答案

在对一些代码进行了解剖之后,我认为这是一个libGDX错误.那就是BoundingBox

After some code dissecting I believe it's a libGDX bug. That's the mul function of BoundingBox

public BoundingBox mul (Matrix4 transform) {
        final float x0 = min.x, y0 = min.y, z0 = min.z, x1 = max.x, y1 = max.y, z1 = max.z;
        ext(tmpVector.set(x0, y0, z0).mul(transform));
        ext(tmpVector.set(x0, y0, z1).mul(transform));
        ext(tmpVector.set(x0, y1, z0).mul(transform));
        ext(tmpVector.set(x0, y1, z1).mul(transform));
        ext(tmpVector.set(x1, y0, z0).mul(transform));
        ext(tmpVector.set(x1, y0, z1).mul(transform));
        ext(tmpVector.set(x1, y1, z0).mul(transform));
        ext(tmpVector.set(x1, y1, z1).mul(transform));
        return this;
    }

ext:

public BoundingBox ext (Vector3 point) {
        return this.set(min.set(min(min.x, point.x), min(min.y, point.y), min(min.z, point.z)),
            max.set(Math.max(max.x, point.x), Math.max(max.y, point.y), Math.max(max.z, point.z)));
    }

如果仔细观察,ext函数会对当前点进行一些minmax运算.这就是导致您出错的原因.该函数应在执行任何操作之前将最小和最大点重置(分别重置为POSITIVE_INFINITY和NEGATIVE_INFINITY).

If you look closely, the ext function does some min and max with the current points. That is what causes your error. That function should reset the min and max points (to POSITIVE_INFINITY and NEGATIVE_INFINITY respectively) before any operation.

似乎它已经在 git存储库中修复.您可以使用最新的每晚版本,也可以等待下一个版本.

It seems it was already fixed on the git repository. You can use the latest nightly or wait for the next release.

这篇关于Libgdx-旋转边界框的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆