JMonkeyEngine:Beginner

= JME 3 Tutorial (1) - Hello SimpleApplication = Previous: Installing JME3, Next: Hello Node

Prerequisites: This tutorial assumes that:


 * You have downloaded the jMonkeyPlatform (or set up the jMonkeyEngine3 in another IDE of your choice)
 * You have created a Java SE project with the JME3 libraries on the classpath.

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
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
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
The code above has initialized the scene, and started the game.

Initializing the Scene
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 <tt>score</tt> to 0, set <tt>health</tt> 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
The HelloJME3.java class extends <tt>com.jme3.app.SimpleApplication</tt>, which is a subclass of <tt>com.jme3.app.Application</tt>. Every JME3 game is an instance of <tt>com.jme3.app.SimpleApplication</tt>.

To run a JME3 game, you first instantiate your <tt>SimpleApplication</tt>-based class, and then call its <tt>start</tt> 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
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 <tt>simpleInitApp</tt> method where you create objects.
 * A <tt>rootNode</tt> 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:


 * Install the jMonkeyEngine
 * SimpleApplication From the Commandline
 * Create a JME3 project.