Scaling a model not resizing BoundingSphere

Hi all, I’m scaling a generally spherical model that I’ve loaded, and I’m trying to scale it to a certain radius:-

float required_radius = 20f;
Spatial s = JMEFunctions.LoadModel(assetManager, "Models/Quaternius/Planet3.obj");
BoundingSphere bs = new BoundingSphere();
s.setModelBound(bs);
s.updateModelBound();
s.scale(required_radius/bs.getRadius()); // Scale it based on current radius and required radius
s.updateModelBound();
System.out.println("New radius: " + bs.getRadius());

However, after doing this, the new radius is still the same as the old radius. Am I doing something wrong? Thanks in advance.

Can it be, that you are using an old version of JME?
There also is a function called setRadius(); maybe that can help instead of scale().
Your Code seems kind of messed up a little bit …

Also have a look here:
http://javadoc.jmonkeyengine.org/com/jme3/bounding/BoundingSphere.html#setRadius-float-

Thanks for the reply. AFAIK I’m using a recent-ish version from June 2014. Will setRadius() change the size of the model? From the javadoc it seems it only changes the size of the BoundingSphere.

How should I improve the code?

Once you set your own bounding shape then you override any automatic calculations. So if you want a different size bounding sphere then you have to set one.

JME only calculates axis-aligned bounding boxes by default because they are simple and (relatively speaking) more accurate than automatically generated spheres.

I was only using the BoundingSphere() to calculate the radius of my model. I did try creating a new BoundingSphere after setting the scale, but it still returned the same radius.

I’m not sure what part of my post you didn’t understand so I will be painfully clear.

Bounding spheres are not calculated automatically. You set it. So if you want it bigger then you set it bigger. They are not calculatted automatically. There is no good accurate algorithm for calculating a bounding sphere… so it is not done automatically.

Then repeat this:

Because the only automatic bounding shape calculation you will get is bounding boxes.

There are no automatically calculated bounding spheres in JME. So you have to calculate them yourself… which is either easy because you know the data, or error prone and ugly if you don’t.

I see. Has this functionality changed? For years I’ve always been doing something like the following whenever creating an object:-

mySphere = new Sphere("mySphere", new Vector3f(0,0,0), 30, 30, 1 );
mySphere.setModelBound(new BoundingSphere());
mySphere.updateModelBound();

I always assumed thatupdateModelBounds() would resize the bounding sphere according to the size of the mesh. Are the last two lines not needed these days?

Means that:

Will never do anything every again until you set the model bound to null again.

Think of the math and code involved that would have to a) guess the bounding sphere from points, and b) know to do that when you update the bound versus doing the truly trivial math to do a bounding box.

Note: the code is only ever one click away and you can easily see all of this for yourself in less time than it takes to compose a forum post. :slight_smile:

Edit: or I can check the code and find out I may be full of crap…

Spatial.updateModelBound()… calls:
Mesh.updateBound() (correcting your version)… calls:
BoundingVolume.computeFromPoints()… which in the case of BoundingSphere calls:
BoundingSphere.calcWelzl(FloatBuffer points)

So it does recalculate the model bounds from the sphere points… though note that this may not really reflect your sphere accurately.

Now, your original post was dealing with Spatial scaling… which does not affect the mesh at all (see: any tutorial on how scene graphs work).

If you want the bounding volume scaled then you’d have to get the world bounding volume… which may or may not still be a BoundingSphere. You’d have to check.

Edit: my own experience with automatically calculating bounding spheres was really bad (15+ years ago, though the math hasn’t really changed) so I just assumed JME didn’t do it. I guess it does, with all of the relative “greatly affected by which points you pick first” inaccuracies included.

Thanks, it’s becoming clearer now. So my original code was fine, the only problem was that I should have been checking the radius of the WorldBounds() instead of the BoundingSphere().

Just a quick follow-up question: I’ve searched through the rutorial, and I can’t find anywhere that BoundingBox, BoundingSphere or UpdateModelBounds are used. Are they needed any more, or are they only used by JME internally now?

You only have to worry about them in special cases.

For example, if you want something other than a bounding box then you must set it yourself. If you change the mesh later then you must update its bound again. (and probably also clear collision data if you ever do picking… easy to forget that one.)

Else, JME will call them automatically in normal use.