问题
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());
Log shows:
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 !
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
回答1:
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;
}
And 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)));
}
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.
EDIT:
It seems it was already fixed on the git repository. You can use the latest nightly or wait for the next release.
来源:https://stackoverflow.com/questions/26775364/libgdx-min-and-max-of-a-rotated-bounding-box