Populating the scene

Now you know where you are, and how to position and transform 3D objects. When you start populating the scene, you face the issue of handling possibly hundreds of objects. In order to keep track of all your stuff, the jMonkeyEngine provides you with a special data structure—the scene graph. The scene graph manages all elements in a 3D scene. We call the elements in the 3D scene spatials.

Spatials are attached to one another in a parent-child relationship. When we speak of attaching a spatial, we mean that we add the spatial to the scene. When we speak of detaching a spatial, we remove it from the scene.

One specific parent spatial in every game is the root node of the scene. Every spatial that is attached to this one root node, directly or indirectly, is part of the scene graph.

Tip

JMonkeyEngine speeds up large scenes by ignoring spatials that are out of view or behind the camera. This feature is called culling. To make a spatial disappear temporarily without detaching it from the scene, tell the engine to cull it by setting geom.setCullHint(CullHint.Always);; similarly, you can use CullHint.Never to keep a spatial, such as a sky box, always visible. To reset the culling behavior back to its default, set it to CullHint.Inherit.

Let's have a look at the BasicGame template. We extend all our games from SimpleApplication because we want to inherit a certain class field: spatial called rootNode. This rootNode object gives us access to the scene graph. You remember the rootNode from Main.java when we added the blue cube geom to the scene?

rootNode.attachChild(geom);

In the examples here, you have now encountered a geometry (the blue cube) and a node, the rootNode object. Both are derived from com.jme3.scene.Spatial, and both are elements of the scene graph. What is the difference between a geometry and a node?