# Spatial

This is an introduction to the concept of of Spatials, the elements of the 3D scene graph. The scene graph is a data structure that manages all objects in your 3D world. For example it keeps track of the 3D models that you load and position. When you extend a Java class from com.jme3.app.SimpleApplication, you inherit the scene graph and rootNode.

The main element of the scene graph is a Spatial called rootNode. All other Spatials are attached to the rootNode in a parent-child relationship. If you think you want to understand the scene graph better, please read Scenegraph for dummies first.

## Node versus Geometry

A Spatial is either a Node or a Geometry.

| Spatials
Purpose: | A Spatial is an abstract data structure that stores transformations (translation, rotation, scale) of elements of the scene graph. A Spatial can be saved and loaded using the AssetManager.
| com.jme3.scene.Geometry | com.jme3.scene.Node
Visibility: | A Geometry represents a visible 3-D object in the scene graph. | A Node is an invisible "handle" for a group of objects in the scene graph.

Purpose:

Use Geometries to represent an object's looks: Every Geometry contains a polygon mesh and a material, specifying its shape, color, texture, and opacity/transparency.
You can attach a Geometry to an Node.

Use nodes to structure and group Geometries and other Nodes. Every Node is attached to parent node, and the node can have children attached to itself. When you transform a parent node, all its children are transformed as well.

Content: | Transformations, mesh, material. | Transformations. No mesh, no material.
Examples: | A box, a sphere, player, a building, a piece of terrain, a vehicle, missiles, NPCs, etc… | The rootNode, the guiNode, an audio node, a custom grouping node, etc…

Important: You never create a Spatial with `Spatial s = new Spatial();` – it's abstract. Instead you create (e.g. load) a Node or Geometry object, and cast it to Spatial. You use the Spatial type in methods that accept both Nodes and Geometries as arguments.

### Mesh

The polygon Mesh inside a Geometry can be one of three things:

- Shapes: The simplest type of Meshes are jME's default Shapes such as cubes and spheres. You can use several Shapes to build complex Geometries. Shapes are built-in and can be created without using the AssetManager.
- 3D Models: 3D models and scenes are also made up of meshes, but are more complex than Shapes. You create Models and Scenes in external 3D Mesh Editors and export them as Ogre XML or Wavefront OBJ. Use the Asset Manager to load models into a your jME3 game.
- Custom Meshes: Advanced users can create Custom Meshes programmatically.

## How to Access a Named Sub-Mesh

Often after you load a scene or model, you need to access a part of it as an individual Geometry in the scene graph. Maybe you want to swap a character's weapon, or you want to play a door-opening animation. First you need to know the unique name of the sub-mesh.

1. Open the model in a 3D mesh editor, or in the jMonkeyPlatform's viewer.
2. Find out the existing names of sub-meshes in the model.
3. Assign unique names to sub-meshes in the model if neccessary.

In the following example, the Node `house` is the loaded model. The sub-meshes in the Node are called its children. The String, here `door 12`, is the name of the mesh that you are searching.

    Geometry submesh = (Geometry) houseScene.getChild("door 12");

view online version