Note quite. It is design to catch gaps in knowledge. How you group objects in the scene graph. Where and when you update. And how you manage transforms. The 101 of scene graphs. More importantly you would have to manually construct/understand a very simple scene graph.
I see. I feel like if I did this on my own I’d potentially make a mess and not learn “properly”. Would you check my code if I were to do it and give me a couple pointers please?
Of course.
Thank you very much. Here is my code:
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
private Node centralNode;
private Box cubeSpatial;
private Material material;
private Geometry geom, geomArray[];
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(10);
flyCam.setRotationSpeed(3);
viewPort.setBackgroundColor(ColorRGBA.White);
//Initializing central node
centralNode = new Node();
//Creating "template" cube
cubeSpatial = new Box(0.5f, 0.5f, 0.5f);
geom = new Geometry("cube", cubeSpatial);
material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
material.setColor("Color", ColorRGBA.Cyan);
geom.setMaterial(material);
geom.setLocalTranslation(0, 0, 0);
//Creating 8 cubes based on template and attaching cubes to the central node
geomArray = new Geometry[8];
centralNode.setLocalTranslation(0, 0, 0);
for (int i = 0; i < geomArray.length; i++){
geomArray[i] = geom.clone();
centralNode.attachChild(geomArray[i]);
}
geomArray[0].setLocalTranslation(2, 0, 0);
geomArray[1].setLocalTranslation(2, 0, 2);
geomArray[2].setLocalTranslation(0, 0, 2);
geomArray[3].setLocalTranslation(-2, 0, 2);
geomArray[4].setLocalTranslation(-2, 0, 0);
geomArray[5].setLocalTranslation(-2, 0, -2);
geomArray[6].setLocalTranslation(0, 0, -2);
geomArray[7].setLocalTranslation(2, 0, -2);
//Attaching central node to rootNode
rootNode.attachChild(centralNode);
}
@Override
public void simpleUpdate(float tpf) {
//Rotating the cubes around a central node
centralNode.rotate(0, FastMath.sin(tpf), 0);
}
Excellent. Now consider how you would manipulate this scene if you wanted to access some of the objects in a method but only pass the rootNode. Also consider if you wanted to have half the objects move up and down slightly as the whole thing is rotated. This is how you move around a scene graph. Or traverse it and “capture” things.
Generally i wouldn’t use visitor pattern if i didn’t want to do something in common to a class of nodes/subnodes. I would just use plain old recursion to traverse the tree to find what i want.
Thanks. When you say plain old recursion do you mean simply getting the node’s children and avoiding the SceneGraphVisitor object altogether?
Manipulating objects only passing rootNode:
//Shrinking three cubes using only rootNode private void shrinkThreeCubes(){ Node tempNode = new Node(); tempNode = (Node) rootNode.getChild(0); for(int i = 0; i < 6; i += 2){ tempNode.getChild(i).scale(2f); } }
Moving half up and down (in the simpleUpdate method)
//Moving half objects up and down with sin function where y += tpf for(int i = 0; i < 4; i++){ centralNode.getChild(i).setLocalTranslation(centralNode.getChild(i).getLocalTranslation().getX(), FastMath.sin(y), centralNode.getChild(i).getLocalTranslation().getZ()); }
Utilizing y this way is probably bad though since it just gets bigger and bigger. I’d have to reset it at some point.