JMonkeyEngine:Beginner

JME 3 Tutorial (1) - Hello SimpleApplication

edit

Previous: Installing JME3, Next: Hello Node

Prerequisites: This tutorial assumes that:

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. In the following, we will use the jMonkeyPlatform.

Writing a SimpleApplication

edit

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

In the jMonkeyPlatform, you right-click the Source Packages node:

  • Select New… > Java Class to create a new file.
  • Enter a class name: HelloJME3
  • Enter a package: jme3test.helloworld
  • Click Finish.

Sample Code

edit

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(); // start the game
    }
 
    @Override
    public void simpleInitApp() {
        Box b = new Box(Vector3f.ZERO, 1, 1, 1); // create cube shape at the origin
        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 to blue
        geom.setMaterial(mat);                   // set the cube's material
        rootNode.attachChild(geom);              // make the cube appear in the scene
    }
}

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

edit

The code above has initialized the scene, and started the game.

Understanding the Terminology

edit
What you want to do
How you say it in JME3 terminology
You want to create a cube. You create a Geometry with a 1x1x1 Box shape.
You want to use a blue color. You create a Material with a blue Color property.
You want to colorize the cube blue. You set the Geometry's Material.
You want the cube to appear in the scene. You attach the cube to the rootNode.

Initializing the Scene

edit

In Java, the creation of a blue cube looks as follows:

    public void simpleInitApp() {
        Box b = new Box(Vector3f.ZERO, 1, 1, 1); // create cube shape at the origin
        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 to blue
        geom.setMaterial(mat);                   // set the cube's material
        rootNode.attachChild(geom);              // make the cube appear in the scene
    }

In the simpleInitApp() method, you create or load all game objects before the game starts. The simpleInitApp() method is automatically called once at the beginning of every JME3 game.

A typical JME3 game has the following initialization process:

  1. You initialize game objects:
    • You create or load objects and position them.
    • You make objects appear in the scene by attaching them to the rootNode.
    • Examples: Load player, terrain, sky, enemies, obstacles, and place them in their start positions.
  1. You initialize variables
    • You set variables to their start values.
    • You want to use variables track the game state.
    • Examples: Set the score to 0, set health to 100%, and so on.
  1. You initialize keys and mouse actions
    • The following input bindings are pre-configured by default:
      • W,A,S,D keys – Move around in the scene
      • Mouse movement and arrow keys – Turn the camera
      • Escape key – Quit the game
    • Add your own keys and mouse clicks.
    • Examples: Click to shoot, press Space to jump, etc.

Starting the Game

edit

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.SimpleApplication.

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

    public static void main(String[] args){
        HelloJME3 app = new HelloJME3();
        app.start(); // start the game
    }

Typically you start a game from your Java application's main() method.

Conclusion

edit

These few lines of code simply display a static 3D cube. You can navigate around in this 3D scene.

You have learned that a SimpleApplication is a good starting point because it provides you with:

  • A simpleInitApp() method where you create objects.
  • A rootNode where you attach objects to make them appear in the scene.
  • Useful default input settings that you can use for navigation in the scene.

When developing a game application, you will now want to:

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

In the following tutorials you learn how accomplish these tasks 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: