How to create a bounding box in code which looks like in SDK?

Hello monkeys,

for a pickup trigger I want the BoundingBox of an item to appear when the player is close enough to pick up that item.
It should be exactly like when you click on the root node of your model (in SceneComposer).

Here you see, what I want to have in the end (captured in SceneComposer):

To create BoundingBoxes I use this relevant code:

public static Geometry getBoundingBoxGeometryForPickup(Spatial spatial) {
    if (spatial == null) {
        return null;        
    }        
    Geometry boundingVolume = WireBox.makeGeometry((BoundingBox) spatial.getWorldBound());
    boundingVolume.setMaterial(getBoundingBoxMaterialForPickup());        
    boundingVolume.setLocalTranslation(spatial.getLocalTranslation());
    boundingVolume.setLocalRotation(spatial.getLocalRotation());
    boundingVolume.setLocalScale(spatial.getLocalScale());
    return boundingVolume;
}

The created geometry is attached to the parent node of the models root node.

The following screenshot shows how it looks ingame:

It seems, like the BoundingBox geometry could need an offset with half of the models size. Am I wrong? But I don’t want to this for all the pickupable items.

Could someone show me how the BoundingBox creation is done in the SDK? That would be really helpful!

With much greetings,
Domenic

Looks like this issue overlaps with Problem with box collision shape

Thanks, for your answer, but I thinks this is more physics related, isn’t it?

…you got the bounds in world space… then arbitrarily moved it to Spatial-local space… then attached it to the Spatial’s parent. Some offset was lost in this process.

If you get the bound in world space (and you should because there is nothing else you can do) then you will have to worldToLocal() its location on whatever Spatial you want to attach it to. You’re going to have to do that with its rotation, also. Scale is trickier but the math isn’t hard.

1 Like

To be honest, I didn’t really know what I did here :blush:

Thanks, I tried that and it does work for the location. In the next days, I will do the same for rotation, and scale!

There also was something I did wrong in the background what lead to that problem.

Thank you very much for your help :slight_smile:

“bounding box” often means “Axis-Aligned bounding box in worldspace”. That’s because that leads to the fastest computation in many cases (“static geometry that fits tight into a AA box”). I think there should be a non-axis-aligned bounding box like those often found in level-editors and 3D modeling software. Back when I started (with jME2) jME used bounding spheres per default (which can be great in some cases but are slow when geometry changes or is not of isotropic shape).

You can use your own geometry to make a wireframe box. For example see “Wireframe Cube” in the wiki: https://github.com/jMonkeyEngine/wiki/blob/master/src/docs/asciidoc/jme3/advanced/debugging.adoc
It uses the WireBox class, which is a very simple geometry: jmonkeyengine/WireBox.java at master · jMonkeyEngine/jmonkeyengine · GitHub
(Note: I don’t know which is the correct wiki-URL anymore since there was confusion about that.)

1 Like

He’s already using the wire box that JME provides.

You are right that he probably wants to be using the Geometry’s model bound (assuming he doesn’t want it axis-aligned)… and in that case it will already be oriented properly (and located properly) for adding to the Geometry’s parent.

Ah okay, then nevermind. I was irritated by the fact that there is a Spatial.getWorldBound and no “Spatial.getModelBound” - so I must have missed something.

Hey cool, thank you for that link. Quite interesting! :grinning:

world bound is needed by the scene graph itself so is calculated. Model bound is not propagated because 99% of users will never need it and it’s actually really difficult to calculate (what is the ‘model bound’ of two children, one with a rotated cube and the other with a sphere?.. etc.). If you have the Geometry (which OP does) then you can get the Geometry model bound with… Geometry.getModelBound()

2 Likes

Hm … must have used search term getModleBound or something. Okay, now I see it. :slight_smile: