JME 3 Tutorial (1) - Hello SimpleApplication

Previous: Installing JME3, Next: Hello Node

This tutorial assumes that you have already downloaded and set up jMonkeyEngine3 in an IDE of your choice, and are able to run the bundled samples.

You are ready to create your first jMonkeyEngine3 game! You can generally use the tutorials in this introductory series with any integrated development environment (IDE), such as the jMonkeyPlatform, NetBeans, Eclipse, or run them straight from the commandline.

Writing a SimpleApplication

Create a jme3test.helloworld package and a file HelloJME3.java in it.

In NetBeans, you would right-click the Source Packages node

Sample Code

Replace the contents of the HelloJME3.java file with the following code:

package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
/** Sample 1 - how to get started with the most simple JME 3 application.
 * Display a blue 3D cube and view from all sides by
 * moving the mouse and pressing the WASD keys. */
public class HelloJME3 extends SimpleApplication {
    public static void main(String[] args){
        HelloJME3 app = new HelloJME3();
        app.start();
    }
    @Override
    public void simpleInitApp() {
        Box(Vector3f.ZERO, 1, 1, 1);
        Geometry geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        geom.setMaterial(mat);
        rootNode.attachChild(geom);
    }
}

Build and run the HelloJME3 class. If a jme settings dialog pops up, confirm the default settings.

  1. You should see a simple window displaying a 3-D cube.
  2. Use the WASD keys and the mouse to navigate around.
  3. Press Escape to close the application.

Congratulations, it works! How did we do that?

Understanding the Code

Here some basic rules that are valid for all JME3 games:

Starting the Game

Note that the HelloJME3.java class extends com.jme3.app.SimpleApplication, which is a subclass of com.jme3.app.Application. Every JME3 game is an instance of com.jme3.app.Application (directly, or indirectly).

To run a JME3 game, you first instantiate your Application-based class, and then call its start() method:

HelloJME3 app = new HelloJME3();
app.start();

Usually, you do that from your Java application's main method.

Tip: Advanced Java developers may want to make a copy of SimpleApplication and use it as a template for a custom application class.

Initializing the Scene

This simple "game" consists of nothing but a cube. Here is how we create it, position it, give it a color, and attach it to the scene. (We will have a closer look at the details later.)

    public void simpleInitApp() {
        Box(Vector3f.ZERO, 1, 1, 1); // create cube shape
        Geometry geom = new Geometry("Box", b);  // create cube geometry from the shape
        Material mat = new Material(assetManager,
         "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
        mat.setColor("Color", ColorRGBA.Blue); // set color of material
        geom.setMaterial(mat);                   // set the cube's material
        rootNode.attachChild(geom);              // attach the cube to the scene
    }

The simpleInitApp() method is automatically called once at the beginning of every JME3 game. In this method you create or load game objects before the game starts! Here is the usual process:

  1. Initialize game objects:
    • Create or load all objects, and position them.
    • To make a geometry (like the box) appear in the scene, attach it to the rootNode.
    • Examples: Load player, terrain, sky, enemies, obstacles, and place them in their start positions.
  2. Initialize game variables
    • Game variables track the game state. Set them to their start values.
    • Examples: Here you set the score to 0, and health to 100%, and so on.
  3. Initialize navigation
    • The following key bindings are pre-configured by default:
      • W,A,S,D keys – Move around
      • Mouse and arrow keys – Turn the camera
      • Escape key - Quit game

The important part is: The JME3 Application has a rootNode object. Your game automatically inherits the rootNode. Everything attached to the rootNode appears in the scene. Or in other words: An object that has been created, but is not attached to the rootNode, remains invisible.

Conclusion

These few lines of code do nothing but display a static object in 3-D, but they already allow you to navigate around in 3D. You have learned that a SimpleApplication is a good starting point because it provides you with:

In a real game, you will want to:

  1. Initialize the game world,
  2. Trigger actions in the event loop,
  3. Respond to user input.

In the following tutorials you will learn how these tasks are accomplished with the jMonkeyEngine 3!

Continue with the Hello Node tutorial, where we will first show you more details about how to initialize the game world, also known as the scene graph.


See also: SimpleApplication from the commandline

beginner,, beginner, intro, documentation, init

view online version