# building bounding boxes....what is wrong?

Hello,

I’m trying to calculate the bounding boxes of my objects. Four cubes.

If the cube is located at positive coordinates all is OK. If the object is located in a negative coordinate then the bounding box looks like the image below

[ATTACH=CONFIG]1441[/ATTACH]

Topleft cube is ok.

In the other cubes the negatives coordinates of the bounding box are changed to zero…why?

Inside the everyframe render method, this is the code when I arrange the matrix to rotate-translate the bounding box

``````....
....
float bbMatrix[]=new float;
Matrix.setIdentityM(bbMatrix,0);
Matrix.translateM(bbMatrix, 0, mesh.x, mesh.y, mesh.z);

Matrix.rotateM(bbMatrix, 0, mesh.ax, 1, 0, 0);
Matrix.rotateM(bbMatrix, 0, mesh.ay, 0, 1, 0);
Matrix.rotateM(bbMatrix, 0, mesh.az, 0, 0, 1);

Matrix.translateM(bbMatrix, 0, 0, 0, 0);

mesh.getBoundingBox().transformMinsAndMax(bbMatrix);
....
....

``````

I pass the matrix to this method. The points of bounding box are rotated and the minimum and maximum coordinate values are calculated:

``````public void transformMinsAndMax(float[] vMatrixArray) {

float pointTransformed[]=new float;

Log.d(TAG,"yMin="+ymin+", yMax="+ymax);

for(int i=0;i<p.length;i++) {
Matrix.multiplyMV(pointTransformed, 0, vMatrixArray, 0, p[i].asFloat(), 0);
pc[i]=new Point(pointTransformed,pointTransformed,pointTransformed,1,1,0);
}

xmin=Float.MAX_VALUE;ymin=Float.MAX_VALUE;zmin=Float.MAX_VALUE;
xmax=Float.MIN_VALUE;ymax=Float.MIN_VALUE;zmax=Float.MIN_VALUE;

for(int i=0;i<p.length;i++) {
//x
if(pc[i].x<xmin){
xmin=pc[i].x;
}

if(pc[i].x>xmax){
xmax=pc[i].x;
}
...
...

``````

p array contains the original points of the bounding box. The points are calculated one time when the cube is loaded.
p[i].asFloat return an array with the point coordinates (x,y,z,1)
pc array contains the bounding box points.

What is wrong? Can you help me please?

Thanks.

Perhaps the way you initialize xmin and xmax is the problem. Try something like below.

``````
xmin = pc.x;
xmax = pc.x;

for (i = 1; i < p.length; i++)  {
if (pc[i].x < xmin)  xmin = pc[i].x;
if (pc[i].x > xmax) xmax = pc[i].x;
}

``````

Should not, according to the java doc.

No, Float.MIN_VALUE is the smallest number > 0. It should be -Float.MAX_VALUE or Float.NEGATIVE_INFINITY (used with Float.POSITIVE_INFINITY).

However Carmine’s solution looks prettier (but you have to make sure pc has at least one element).

[QUOTE=Carmine;1285746]Perhaps the way you initialize xmin and xmax is the problem. Try something like below.

``````
xmin = pc.x;
xmax = pc.x;

for (i = 1; i < p.length; i++)  {
if (pc[i].x < xmin)  xmin = pc[i].x;
if (pc[i].x > xmax) xmax = pc[i].x;
}

``````

[/QUOTE]

Thanks Carmine.

Nothing changed…same results