Gui(Node) and collideWith

Hi,

I suppose this topic would interest people like me that want to create HUD with 3D objects located in the GuiNode.

I try to have a user interface - in 3D - where some objects in the inventory (in the GuiNode so) can be clicked, have rotation, etc …

So rotation no pb. I can animate them.

Clicking, well, that’s my problem.

So guiNode has 2 boxes

[java]Box box = new Box(new Vector3f(0, 0, 0), 50, 50, 5);

Geometry cube = new Geometry(“box1”, box);

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

//mat1.setColor(“Color”, ColorRGBA.Blue);

mat1.setTexture(“ColorMap”,assetManager.loadTexture(“Models/b2_tk.jpg”));

cube.setMaterial(mat1);

cube.move(350,50,2);

guiNode.attachChild(cube);

box = new Box(new Vector3f(0, 0, 0), 50, 50, 5);

cube = new Geometry(“box2”, box);

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

mat1.setTexture(“ColorMap”,assetManager.loadTexture(“Textures/interface_1.png”));

cube.setMaterial(mat1);

cube.move(500,50,1);

guiNode.attachChild(cube);[/java]

to calculate if one of these elements is clicked I am doing this :

[java]Vector2f click2d = inputManager.getCursorPosition(); // from mouse

Vector3f click3d = cam.getWorldCoordinates( new Vector2f(click2d.getX(), click2d.getY()), 0f).clone();

Vector3f dir = cam.getWorldCoordinates( new Vector2f(click2d.getX(), click2d.getY()), 1f).subtractLocal(click3d);

Ray ray = new Ray(click3d, dir);

guiNode.collideWith(ray, interfaceResults);[/java]

But unfortunatelly, the interfaceResults is empty.

I am pretty sure it is due to a bad calculation of the Ray - due to maybe be the fact the guiNode is a node with :

[java]guiNode.setQueueBucket(Bucket.Gui);

guiNode.setCullHint(CullHint.Never); [/java]

But I do not see the trick.

I try to change the last number (Z coordinate) of click3d and dir but I do not see any difference.

Please note that methods works perfectly for nodes NOT in guiNode.

Please note as well that when I click on the top right of the screen, I have a collide with the “BitmapFont” that is the object used to display the “Frame per second” in the default stats element (located in the GuiNode on the left down of the screen)

Can someone give me his light on it?

Kind regards,

ok I found - I had to do something simple like that:

[java]Vector3f ori = new Vector3f(inputManager.getCursorPosition().x, inputManager.getCursorPosition().y , 1f);

Vector3f dest = new Vector3f(0f,0f,-1f);

Ray ray = new Ray(ori, dest);[/java]

to make it work … yes restarted from scratch … all the time it is the best solution … thank you thebreton for this excellent initiative :wink:



Becare : This method apply only for GuiNode element however. For the other 3D node (playable area) previous algo is correct.

hmmm I try to load a Spatial (the Teapot - so it is not coming from the object ) in the guiNode - but I have an issue.

If I am including the Spartial in the rootNode :

[java]Spatial spatial = assetManager.loadModel("Models/Teapot/Teapot.obj");

rootNode.addLight(sun);

rootNode.attachChild(spatial);[/java]

this is working fine - the spatial is added to the scene.

but if I tried to do the same with the guiNode instead, it does not appear:

[java]Spatial spatial = assetManager.loadModel("Models/Teapot/Teapot.obj");

guiNode.addLight(sun);

guiNode.attachChild(spatial);[/java]

because it crashed :

10-juil.-2011 22:49:54 com.jme3.app.Application handleError
GRAVE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.jme3.light.LightList.sort(LightList.java:221)
at com.jme3.scene.Geometry.updateWorldTransforms(Geometry.java:269)
at com.jme3.scene.Spatial.updateGeometricState(Spatial.java:659)
at com.jme3.scene.Node.updateGeometricState(Node.java:176)
at com.jme3.scene.Node.updateGeometricState(Node.java:176)
at com.jme3.scene.Node.updateGeometricState(Node.java:176)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:259)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:144)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:218)
at java.lang.Thread.run(Thread.java:619)

At the line 12 of the class LightList the null is detected:
[java]public void sort(boolean transformChanged) {
if (listSize > 1) {
// resize or populate our temporary array as necessary
if (tlist == null || tlist.length != list.length) {
tlist = list.clone();
} else {
System.arraycopy(list, 0, tlist, 0, list.length);
}
if (transformChanged){
// check distance of each light
for (int i = 0; i < listSize; i++){
list.computeLastDistance(owner);
}
}
// now merge sort tlist into list
SortUtil.msort(tlist, list, 0, listSize - 1, c);
}
}[/java]
The light (sun) is declared as following :
[java]private Vector3f lightDir = new Vector3f(-4.9236743f, -1.27054665f, -500.896916f);
private DirectionalLight sun = new DirectionalLight();
...
sun.setDirection(lightDir);
sun.setColor(ColorRGBA.White);[/java]
and finally I saw that the problem is coming from the addition of a light in guiNode
[java]guiNode.addLight(sun);[/java]
... do someone has the same problem?
And if not, what to do to prevent this exception?
If there is something wrong? is it possible to load a button modelised under Blender like (the wiew will be ortho -but not pb)?
Thanks,

Nobody has an idea with this point? :’(

is it possible that someone tries or even is aware about this issue?

I feel little alone is this thread … :cry:

is aware but doesn’t know the solution

ah … so already I feel little less alone - thx Ozo.

However nobody has solution … workaround for this ? :frowning:

I haven’t investigate it yet, properly :slight_smile: but try add mesh to pictures, and set for it unshaded material- may be you found smth useful.

Lighting in guiNode indeed doesn’t work correctly but i think it all right cause main idea of this node - 2d space for text info. imho.

May be it will be a good idea to add some node like gui but with a full 3d coordinating system.

Well, indeed.

Maybe I am doing it wrong, but what is it the proper way to do a 3D interface, where this interface will stay all the time in the position even if the camera is moving? (like the guiNode)

Any idea?

Here is more about it

http://hub.jmonkeyengine.org/groups/gui/forum/topic/attach-mesh-to-guinode/

http://hub.jmonkeyengine.org/groups/gui/forum/topic/3d-model-over-the-gui/