jMonkeyEngine 3.0 Beginner’s Guide
上QQ阅读APP看书,第一时间看更新

Time for action – node versus geometry

Geometries are the objects in your scene. They represent loaded models, characters, terrains, and all kinds of other visible objects.

Nodes are a typical means of grouping other nodes and geometries. The typical use case for nodes is transformation, where they are used as handles for groups of spatials. If you group a driver geometry and a car geometry by attaching them to a common vehicle node, you can move both of them by simply translating the vehicle node.

Another use case for nodes is rotation. Do you intend to rotate around the geometry's center, or around another point? If you intend to rotate around a non-central point, create an invisible pivot node and attach the geometry to it. Then, apply the rotation to the pivot node and not to the geometry itself.

The following example shows two geometries rotated around a common, invisible center node, pivot:

  1. Add the following lines at the end of your simpleInitApp() method:
    Node pivot = new Node("pivot node");
    pivot.attachChild(geom);
    pivot.attachChild(geom2);
    pivot.rotate(00, 0, FastMath.DEG_TO_RAD * 45);
    rootNode.attachChild(pivot);
  2. Attaching geom and geom2 to a new parent node implicitly detaches them from their previous parent node.
  3. Clean and build the BasicGame template, and run the file.

Both geom and geom2 are now attached to the pivot node, and the pivot node is attached to the rootNode. The rotation affects both the nodes that are grouped under the pivot node.

What just happened?

Spatials are Java objects that are used to load and save information about elements of the scene graph. There are two distinct types of spatials in the jMonkeyEngine—nodes and geometries.

Remember that you never create a new Spatial() instance—spatials are abstract. You only use the Spatial data type in methods that return either nodes and geometries (for example, the load() method), or in methods that accept either nodes and geometries as arguments (for example, the save() method). In these cases, you create a Node or Geometry object, and the method implicitly casts it to the Spatial data type.

Pop quiz – the truth about spatials

Q1. Combine the following sentence fragments to form six true statements:

a. A node ... b. A geometry ... c. A spatial ...

  1. … is an element of the scene graph.
  2. … groups nodes and geometries.
  3. … contains a mesh (shape) and a material.
  4. … can be loaded and saved.
  5. … is a visible object in the scene.
  6. … is used as an invisible handle.