Skip to content

Commit

Permalink
Fixed BoundingBox construction when Object3D has vertices as well as …
Browse files Browse the repository at this point in the history
…children
  • Loading branch information
JehandadK authored and Jehandad Kamal committed Sep 30, 2016
1 parent 68dec6d commit b7b1938
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions rajawali/src/main/java/org/rajawali3d/Object3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,31 +348,45 @@ public void render(Camera camera, final Matrix4 vpMatrix, final Matrix4 projMatr
}
}

/**
* Returns a {@link BoundingBox} for this Object3D and creates it if needed.
* Utilizes children's bounding values to calculate its own {@link BoundingBox}.
* @return
*/
public BoundingBox getBoundingBox() {
if (getNumChildren() > 0 && !mGeometry.hasBoundingBox()) {
Vector3 min = new Vector3(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE);
Vector3 max = new Vector3(-Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE);

for (int i = 0; i < getNumChildren(); i++) {
for (int i = 0; i < getNumChildren(); i++) {
Object3D child = getChildAt(i);
Vector3 maxVertex = child.getBoundingBox().getMax();

if (maxVertex.x > max.x) max.x = maxVertex.x;
if (maxVertex.y > max.y) max.y = maxVertex.y;
if (maxVertex.z > max.z) max.z = maxVertex.z;

Vector3 minVertex = child.getBoundingBox().getMin();
updateMaxMinCoords(min, max, child);
}

if (minVertex.x < min.x) min.x = minVertex.x;
if (minVertex.y < min.y) min.y = minVertex.y;
if (minVertex.z < min.z) min.z = minVertex.z;
if (mGeometry.getVertices() != null) {
updateMaxMinCoords(min, max, this);
}

mGeometry.setBoundingBox(new BoundingBox(min, max));
}
return mGeometry.getBoundingBox();
}

private void updateMaxMinCoords(Vector3 min, Vector3 max, Object3D child) {
Vector3 maxVertex = child.getBoundingBox().getMax();

if (maxVertex.x > max.x) max.x = maxVertex.x;
if (maxVertex.y > max.y) max.y = maxVertex.y;
if (maxVertex.z > max.z) max.z = maxVertex.z;

Vector3 minVertex = child.getBoundingBox().getMin();

if (minVertex.x < min.x) min.x = minVertex.x;
if (minVertex.y < min.y) min.y = minVertex.y;
if (minVertex.z < min.z) min.z = minVertex.z;
}


/**
* Renders the object for color-picking
*
Expand Down

0 comments on commit b7b1938

Please sign in to comment.