I am fairly new to the jMonkeyEngine, and I really think it is an amazing game engine. I was just looking down the lists of the beginner tutorials in documentation and this one part stumped me and I was wondering what I was doing wrong and if I could get a little more insight on ‘Nodes’ since they don’t seem to make sense to me.
In the Hello_Picking section of the tutorials this little exercise has been irritating me for a day or so since i can’t figure it out.
Magic Spell
Change the color of the closest clicked target!
Here are some tips:
Go to the line where the closest target is indentified, and add you changes after that.
To change an object’s color, you must first know its node. Identify the node by identifying the target’s name.
Use rootNode.getChild(closest.getGeometry().getName())
Create a new color material and set the node’s Material to this color.
Look inside the makeCube() method for an example of how to set random colors.
It says to find the node by identifying the target’s name, but I don’t really understand what it means by that.
I made the material, which was the easy part, and I just don’t understand the ‘nodes’ portion of it.
My code looks like:
[java]if (results.size() > 0){
// The closest collision point is what was truly hit:
CollisionResult closest = results.getClosestCollision();
// Let’s interact - we mark the hit with a red dot.
mark.setLocalTranslation(closest.getContactPoint());
rootNode.attachChild(mark);
rootNode.getChild(closest.getGeometry().getName());
Material m = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
m.setColor(“Color”, ColorRGBA.randomColor());[/java]
Can someone help me with this, or help me better understand nodes? I realize they are what everything hooks into, but for some reason that confuses me.
Imagine a Node as a logic tree.
Just like a branch, a Node can only have one parent (the bigger branch it’s attached to), but it can have many children (smaller branches that grow out of it).
The Root Node is the biggest branch, or the tree trunk everything else grows out of.
Nodes are logical entities, and don’t have a physical representation.
Geometries share the same interface “Spatial”, and are physical entities. Geometries can’t have children, so imagine them as leaves on the tree.
Each frame, the engine parses through the children of the root node recursively, this is why attaching something to the root node, or a child node will make the engine “aware” of it.
Think of nodes as logic elements of the scene graph, but with no render.
They are empty shells that can contains child nodes or geometries.
When you transform a node (move, rotate, scale) all it’s children are transformed in the same way.
On the other hand, geometries are graphical objects,with a mesh, a material etc…, they are part of the scene graph like nodes, but can’t have children.
For example, let’s say you have a character model made of 3 geometries : the body, the hair, and a weapon. If you want to move your character you’ll have to move the 3 geometries. Attach them to a parent Node, then you’ll just have to move the node and the children will follow.
Only geometries have a material, but setting a material to a node sets the material to all its child geometries.
In the example above, the sentence is a bit misleading, it should have been "To change an object’s color, you must first know its geometry. Identify the geometry by identifying the target’s name.
so here is what you have to do
// The closest collision point is what was truly hit:
CollisionResult closest = results.getClosestCollision();
// Let’s interact - we mark the hit with a red dot.
mark.setLocalTranslation(closest.getContactPoint());
rootNode.attachChild(mark);
Geometry geom = rootNode.getChild(closest.getGeometry().getName()); //find the geometry
Material m = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
m.setColor(“Color”, ColorRGBA.randomColor());
geom.setMaterial(m); //set the material to the geometry
I hope that is clear enough
EDIT : er you beat me Rickard
Well, you were more thorough.
Guys, I was months faster than you
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies
normen said:
Guys, I was months faster than you ;)
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies
Yes but when I first joined it made no sense when I saw it ;) It does now so. Thank you? Haha
Thanks for the help Rickard and Nehon, I think I understand nodes now. Haha and I never thought of putting the geometry as the rootNode portion so you kinda opened my eyes to some new ways to do things, thanks guys! :)
EDIT:
I typed this out on my phone this morning heading to work, at work now, irrelevant, but I have the engine on my computer here. I typed the geometry portion in and it gives me an error that says:
"required: geometry
found: Spatial"
It most certainly is a geometry though, is it a bug, did i miss something?