[SOLVED] How to get intersection areas?

Hello, I’m created 2 Geometries, And i want to take intersection area of this two geometries. Something like this: (Yes, i’m a master of Paint)


How i can take intersection areas?
At least the coordinates of the corners will be good too. This is my code:

@Override
public void simpleInitApp() {
    Geometry obj1 = attachShape(new Box(1, 1, 1), new Vector3f(0, 0, 0), ColorRGBA.Blue, "Box1");
    Geometry obj2 = attachShape(new Box(.5f, 1, .5f), new Vector3f(0, 1, 0), ColorRGBA.Green, "Box2");

    CollisionResults results = new CollisionResults();
    rootNode.getChild(0).collideWith(rootNode.getChild(1).getWorldBound(), results);
}

private Geometry attachShape(Mesh shape, Vector3f pos, ColorRGBA color, String name) {
    Geometry g = new Geometry(name, shape);
    g.setLocalTranslation(pos);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", color);
    g.setMaterial(mat);
    rootNode.attachChild(g);
    return g;
}

And sorry for my English.

CSG

For example this Another CSG solution
Or this Solid geometry (not hollow) - #6 by justry

You could also write special purpose code for cubes, but CSG is for any geometry type.

Thank you.

You’re welcome. :chimpanzee_smile:

Now i have some problems with update. After rotate my shapes and regenerate my GSM object, it dont take changes of second shape.

public void simpleInitApp() {
    CSG_obj1 = attachShape(new Box(1, 1, 1), new Vector3f(0, 0, 0), ColorRGBA.Blue, "Box1");
    CSG_obj2 = attachShape(new Box(.5f, 2, .5f), new Vector3f(0, 0, 0), ColorRGBA.Yellow, "Box2");
    CSG_obj = new CSGGeometry("Geom");
    CSG_obj.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md"));
    rootNode.attachChild(CSG_obj);
    CSG_obj.addShape(CSG_obj1);
    CSG_obj.addShape(CSG_obj2, CSGGeometry.CSGOperator.INTERSECTION);
    CSG_obj.regenerate();
}

private CSGShape attachShape(Mesh shape, Vector3f pos, ColorRGBA color, String name) {
    CSGShape g = new CSGShape(name, shape);
    g.setLocalTranslation(pos);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", color);
    g.setMaterial(mat);
    rootNode.attachChild(g);
    return g;
}

public void simpleUpdate(float tpf) {
    i++;
    if (i == 10) {
        CSG_obj1.rotate(tpf, tpf, tpf);
        CSG_obj2.rotate(-tpf, -tpf,  -tpf);

        CSG_obj.regenerate();
        i = 0;
    }
}

I did not try that CSG library yet - maybe @wcomohundro can answer your question?

I guess it’s something simple like you not calling updateGeometricState or updateBounds or something like that…

There was a bug in the CSG code where the updated Meshes were not being refreshed on a secondary call to .regenerate( ). This has been fixed in b07112015, available on SourceForge.

Dima - your code is just fine and should now work. It was strictly a problem inside CSG. Your sample makes a nice demo of how CSG is supposed to work. Please contact me if you need anything else. I will also be monitoring this thread from now on.

Ogli - thanks for pointing Dima to my code and for referencing me so that I saw the problem. I would love to have more people exercising the code and reporting problems back to me. The SourceForge ticket system is the best way to get my attention.

CSG work on Android?

I have no Android test environment so I have never tried it.

It is all just Java, but I do not know what kind of constraints Android imposes.
CSG is about the Mesh construction with nothing special about Rendering. So
long as Android can deal with standard Meshes, I would hope it would all work.

Let me know about any results you get with Android, good or bad, and I will
include it in my documentation.

I’m had NoClassDefFoundError when i create CSGGeometry and CSGShape objects. I think problem is in gradle generated JME3 Libs. Your libs i connect native.