gpt4 book ai didi

Java 3d API : Rotating a BoundingBox delivers a new Box with more volume

转载 作者:行者123 更新时间:2023-11-30 09:35:41 25 4
gpt4 key购买 nike

我正在用 Java 3D API 编写一个小游戏,我使用 javax.media.j3d.BoundingBox 的对象来存储模型的边界。

BoundingBox 的构造函数如下:

public BoundingBox(Point3d lower, Point3d upper) {
boundId = BOUNDING_BOX;
this.lower = new Point3d(lower);
this.upper = new Point3d(upper);
updateBoundsStates();
}

现在我想平移和旋转 BoundingBox,因为模型本身会做,我使用 BoundingBox 的以下方法:

/** 
* Transforms this bounding box by the given matrix.
* @param matrix a transformation matrix
*/
public void transform(Transform3D matrix) {
if(boundsIsInfinite)
return;

if (tmpP3d == null)
tmpP3d = new Point3d();

double ux, uy, uz, lx, ly, lz;
ux = upper.x;
uy = upper.y;
uz = upper.z;

lx = lower.x;
ly = lower.y;
lz = lower.z;

tmpP3d.set(ux, uy, uz);
matrix.transform( tmpP3d );

upper.x = tmpP3d.x;
upper.y = tmpP3d.y;
upper.z = tmpP3d.z;

lower.x = tmpP3d.x;
lower.y = tmpP3d.y;
lower.z = tmpP3d.z;

tmpP3d.set(lx, uy, uz);
matrix.transform( tmpP3d );

if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;

if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;

tmpP3d.set(lx, ly, uz);
matrix.transform( tmpP3d );

if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;

if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;

tmpP3d.set(ux, ly, uz);
matrix.transform( tmpP3d );

if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;

if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;

tmpP3d.set(lx, uy, lz);
matrix.transform( tmpP3d );

if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;

if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;

tmpP3d.set(ux, uy, lz);
matrix.transform( tmpP3d );

if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;

if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;

tmpP3d.set(lx, ly, lz);
matrix.transform( tmpP3d );

if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;

if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;

tmpP3d.set(ux, ly, lz);
matrix.transform( tmpP3d );

if ( tmpP3d.x > upper.x ) upper.x = tmpP3d.x;
if ( tmpP3d.y > upper.y ) upper.y = tmpP3d.y;
if ( tmpP3d.z > upper.z ) upper.z = tmpP3d.z;

if ( tmpP3d.x < lower.x ) lower.x = tmpP3d.x;
if ( tmpP3d.y < lower.y ) lower.y = tmpP3d.y;
if ( tmpP3d.z < lower.z ) lower.z = tmpP3d.z;

if (VirtualUniverse.mc.releaseBoundingBoxMemory) {
// Free memory
tmpP3d = null;
}
}

但是:如果您使用此方法旋转 BoundingBox,它会生成一个新的 BoundingBox,其体积可能比旧的更大。如果您恰好旋转 90、180、270 或 360 度,它只会提供相同的体积。

附加信息:我必须手动计算 BoundingBox,因为我通过在模型上调用 setAutoComputeBounds(false) 和 setBounds(XYZ) 使用手动边界(而不是太大的自动计算边界)(在这种情况下它实际上是一个实例指向 javax.media.j3d.SharedGroup 的 javax.media.j3d) 并且 Java 3d 引擎(至少在这种情况下)似乎不会在平移/旋转的情况下自动重新计算您的手动边界对应型号。

我的问题是:我必须如何(重新)实现上面显示的方法 transform() 以获得始终具有相同体积的边界框?

最佳答案

BoundingBox 实际上始终与轴对齐,无论它的两个点在哪里。两个点不能定义任意立方体。无论你如何重写转换函数,它都不会起作用。

您可能会考虑 BoundingPolytope 类。它提供了更通用的边界,由平面构成,尽管它确实有一个接受另一个 Bounds 对象的构造函数,因此您实际上不需要自己定义平面。

关于Java 3d API : Rotating a BoundingBox delivers a new Box with more volume,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11225407/

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