Retrieving Bounding Box Issue

I am attempting to get a bounding box for a Geometry object.



As I understand it, by default a BoundingSphere is set for a given objects modelBounds. This is no good for me since I need to position objects based on their width, and they are often taller than they are wide.



What I did was to set the modelBounds to a new BoundingBox, and then update the modelBounds:


geometry.setModelBound( new BoundingBox() );
geometry.updateModelBound();



however, this produces:

java.lang.ClassCastException
   at com.jme.bounding.BoundingBox.transform(BoundingBox.java:211)
   at com.jme.scene.Geometry.updateWorldBound(Geometry.java:854)
   at com.jme.scene.Geometry.updateModelBound(Geometry.java:769)



it appears that BoundingBoxes don't like transforming BoundingSpheres (and vice versa). In the transform method, they cast the passed BoundingVolume to their own type.

to make the whole thing work I have to do this:

geometry.setWorldBound( new BoundingBox() );
geometry.setModelBound( new BoundingBox() );
geometry.updateModelBound();



Am I doing something silly or is there an easier route to the result I want ?

In the different bounding volume implementations (transform method):


BoundingBox box = (BoundingBox) store;
if (box == null)
    box = new BoundingBox(new Vector3f(0, 0, 0), 1, 1, 1);



is done... (or casting to the specific bounding type it's implemented in).

it really should be something more like:


BoundingBox box;
if (store == null || !store instanceof BoundingBox) {
    box = new BoundingBox(new Vector3f(0, 0, 0), 1, 1, 1);
} else {
    box = (BoundingBox) store;
}



Anyone see is problems with that fix?

The root of the problem is that when you change model bounds with setModelBound() it doens’t change world bounds. So while your model bounds may be a Box now, your world bounds is still a Sphere. I suggest you set the worldBound to null when the modelBound is set so that it can be updated freshly during updateModelBound(). This should be done in conjunction with your fix.

Heh, suggested the same approach.

committed.