Attach a 3D rotating geometry to the GUI

As the title says I tried to attach a 3D geometry which i constantly rotate with simpleupdate() to the GUI node needless to say it failed to show up.



Any ideas how to do this?

Attach it to the root node instead? The GUI node is only for gui stuff

Well I want to make sure it always stays at the same spot in the screen, even if the player is moving.



Is this doable if I attach it to the rootnode? How?

You can create a new viewport with a stationary camera

Yes you need a 2nd root node for this.

You should call the methods on the root node after you rotate the box

1 Like

I got a problem showing the box in the viewport.

[java]Node n = new Node(“box”);

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

geom = new Geometry(“Box”, b);

geom.updateModelBound();

Material mat = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat.setColor(“m_Color”, ColorRGBA.Blue);

geom.setMaterial(mat);

n.attachChild(geom);

Camera cam2 = cam.clone();

cam2.setViewPort(0.4f, 0.6f, 0, 0.2f);

cam2.setLocation(new Vector3f(2f, 2f, 4f));

cam2.setRotation(new Quaternion (-0.07680723f, 0.92299235f, -0.2564353f, -0.27645364f));

ViewPort view2 = renderManager.createMainView(“test”, cam2);

//view2.setBackgroundColor(ColorRGBA.randomColor().set(1, 1, 1, 1f));

view2.setClearColor(true);

view2.setClearEnabled(true);

view2.setBackgroundColor(ColorRGBA.DarkGray);

view2.attachScene(n.getChild(“Box”));[/java]

The result I want is that I can attach a geometry to the viewport without having it visible in my rootnode. The code works when I do view2.attachScene(rootNode.getChild(“box”));. But I don’t want the box visible in my main scene.

Any ideas or even improvements for this code?

Forgot to add, I also use this code to keep the box rotating.



[java]public void simpleUpdate(float tpf) {

geom.rotate( 0.0f , tpf , 0.0f );

}[/java]

Make sure to call updateLogicalState and updateGeometricState on the root node of your other viewport, those calls are necessary to update the scene graph. By default SimpleApplication takes care of calling those methods on the default rootNode so you don’t have to worry about it, but if you’re creating new root nodes they are your responsibility to update.

I tried implementing the 2 methods but I still get the same error



Make sure scene graph state was not changed after

rootNode.updateGeometricState() call.

Problem spatial name: Box



The error is caused by this line



view2.attachScene(n.getChild(“Box”));



I didn’t really want to implement a second rootnode… Isn’t there any other more accessible way to do this? I got my viewport set up and I just want to add a geometry in it, that shouldn’t be visible in my main scene.

Finally managed to pull it off… Here is the working code for some people that are stuck on this too.

[java]Node n = new Node("inventory");

Geometry geom;[/java]

[java]public void simpleInitApp() {

Box b = new Box(new Vector3f(0, 0, 0), 1, 1, 1);

geom = new Geometry("Box", b);

Material mat = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");

mat.setColor("m_Color", ColorRGBA.Blue);

geom.setMaterial(mat);

n.attachChild(geom);

Camera cam2 = cam.clone();

cam2.setViewPort(0.4f, 0.6f, 0, 0.2f);

cam2.setLocation(new Vector3f(2.50f, 2f, 4f));

cam2.setRotation(new Quaternion (-0.07680723f, 0.92299235f, -0.2564353f, -0.27645364f));

final ViewPort view2 = renderManager.createMainView("test", cam2);

//view2.setBackgroundColor(ColorRGBA.randomColor().set(1, 1, 1, 1f));

view2.setClearColor(true);

view2.setClearEnabled(true);

view2.setBackgroundColor(ColorRGBA.DarkGray);

view2.attachScene(n.getChild("Box"));

}[/java]

[java]public void simpleUpdate(float tpf) {

geom.rotate( 0.0f , tpf , 0.0f );

n.updateGeometricState();

n.updateLogicalState(tpf);

}[/java]

Thanks a lot for the info Momoko! :slight_smile:

1 Like