= jMonkeyEngine 3 Tutorial (2) - Hello Node :author: :revnumber: :revdate: 2020/07/06 :keywords: beginner, rootNode, node, intro, documentation, color, spatial, geometry, scenegraph, mesh In this tutorial we will have a look at the creation of a 3D scene. * This tutorial assumes that you know what xref:concepts/the_scene_graph.adoc[the Scene Graph] is. * For a visual introduction, check out xref:concepts/scenegraph_for_dummies.adoc[Scene Graph for Dummies]. When creating a 3D game . You create some scene objects like players, buildings, etc. . You add the objects to the scene. . You move, resize, rotate, color, and animate them. You will learn that the scene graph represents the 3D world, and why the rootNode is important. You will learn how to create simple objects, how to let them carry custom data (such as health points), and how to "`transform`" them by moving, scaling, and rotating. You will understand the difference between the two types of "`Spatials`" in the scene graph: Nodes and Geometries. == Code Sample [source,java] ---- package jme3test.helloworld; import com.jme3.app.SimpleApplication; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.Node; import com.jme3.scene.shape.Box; /** Sample 2 - How to use nodes as handles to manipulate objects in the scene. * You can rotate, translate, and scale objects by manipulating their parent nodes. * The Root Node is special: Only what is attached to the Root Node appears in the scene. */ public class HelloNode extends SimpleApplication { public static void main(String[] args){ HelloNode app = new HelloNode(); app.start(); } @Override public void simpleInitApp() { /** create a blue box at coordinates (1,-1,1) */ Box box1 = new Box(1,1,1); Geometry blue = new Geometry("Box", box1); blue.setLocalTranslation(new Vector3f(1,-1,1)); Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat1.setColor("Color", ColorRGBA.Blue); blue.setMaterial(mat1); /** create a red box straight above the blue one at (1,3,1) */ Box box2 = new Box(1,1,1); Geometry red = new Geometry("Box", box2); red.setLocalTranslation(new Vector3f(1,3,1)); Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat2.setColor("Color", ColorRGBA.Red); red.setMaterial(mat2); /** Create a pivot node at (0,0,0) and attach it to the root node */ Node pivot = new Node("pivot"); rootNode.attachChild(pivot); // put this node in the scene /** Attach the two boxes to the *pivot* node. (And transitively to the root node.) */ pivot.attachChild(blue); pivot.attachChild(red); /** Rotate the pivot node: Note that both boxes have rotated! */ pivot.rotate(.4f,.4f,0f); } } ---- Build and run the code sample. You should see two colored boxes tilted at the same angle. == Understanding the Terminology In this tutorial, you learn some new terms: [cols="2", options="header"] |=== a|What you want to do? a|How you say it in JME3 terminology a|Lay out the 3D scene. a|Populate the scene graph. a|Create scene objects. a|Create Spatials. (e.g. create Geometries) a|Make an object appear in the scene. a|Attach a Spatial to the rootNode. a|Make an object disappear from the scene. a|Detach the Spatial from the rootNode. a|Position/move, turn, or resize an object. a|Translate, or rotate, or scale an object = transform an object. |=== Every JME3 application has a rootNode: Your game automatically inherits the `rootNode` object from SimpleApplication. Everything attached to the rootNode is part of the scene graph. The elements of the scene graph are Spatials. * A Spatial contains the location, rotation, and scale of an object. * A Spatial can be loaded, transformed, and saved. * There are two types of Spatials: Nodes and Geometries. [cols="15,35,50", options="header"] |===