jMonkeyEngine 3 Tutorial (1) - Hello SimpleApplication¶
Prerequisites: Know basic Java and create a project with the Initializer. Open the generated Gradle project in your preferred IDE.
In this tutorial you will create your first jMonkeyEngine application, display a cube, and learn how SimpleApplication initializes a scene.
Create a project¶
- Open the jMonkeyEngine Initializer, choose a project name and package, and select the desktop platform.
- Download and unpack the generated project, then open it as a Gradle project.
- In the shared
appmodule, createHelloJME3.javain the packagejme3test.helloworld, or use your own package and change thepackageline in the sample to match.
Tip
The engine examples contain related tutorials. You can use any Java IDE supported by the generated Gradle project.
Extend SimpleApplication¶
The class below extends SimpleApplication. Replace the contents of your HelloJME3.java file with the sample. Configure the desktop launcher to start this class, or run its main method from your IDE. Follow the generated project's README for its platform-specific run task.
Code Sample¶
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.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(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 to blue
geom.setMaterial(mat); // set the cube's material
rootNode.attachChild(geom); // make the cube appear in the scene
}
}
Run HelloJME3 from your IDE or use your project’s desktop launcher. If a jMonkeyEngine settings dialog appears, confirm the default settings.
-
You should see a simple window displaying a 3D cube.
-
Press the W A S D keys and move the mouse to navigate around.
-
Look at the FPS text and object count information in the bottom left. You will use this information during development, and you will remove it for the release. (To read the numbers correctly, consider that the 14 lines of text counts as 14 objects with 914 vertices.)
-
Press Esc to close the application.
Congratulations! Now let’s find out how it works!
Understanding the Code¶
The code above has initialized the scene, and started the application.
Start the SimpleApplication¶
Look at the first line. Your HelloJME3.java class extends com.jme3.app.SimpleApplication.
public class HelloJME3 extends SimpleApplication {
// your code...
}
A game can extend com.jme3.app.SimpleApplication as a convenient starting point. The SimpleApplication class is the simplest example of an application: It manages a 3D scene graph, checks for user input, updates the game state, and automatically draws the scene to the screen. These are the core features of a game engine. You extend this simple application and customize it to create your game.
You start every JME3 game from the main() method, as every standard Java application:
-
Instantiate your
SimpleApplication-based class -
Call the application’s
start()method to start the game engine.
public static void main(String[] args){
HelloJME3 app = new HelloJME3(); // instantiate the game
app.start(); // start the game!
}
The app.start(); line opens the application window. Let’s learn how you put something into this window (the scene) next.
Understanding the Terminology¶
| What you want to do | How you say that in JME3 terminology |
|---|---|
| You want to create a cube. | I create a Geometry with a Box with half-extents of 1. |
| You want to use a blue color. | I create a Material with a blue Color property. |
| You want to colorize the cube blue. | I set the Material of the Box Geometry. |
| You want to add the cube to the scene. | I attach the Box Geometry to the rootNode. |
| You want the cube to appear in the center. | I create the Box at the origin = at Vector3f.ZERO. |
If you are unfamiliar with the vocabulary, read more about the Scene Graph here.
Initialize the Scene¶
Look at rest of the code sample. The simpleInitApp() method is automatically called once at the beginning when the application starts. An application that extends SimpleApplication implements this method. In the simpleInitApp() method, you load game objects before the game starts.
public void simpleInitApp() {
// your initialization code...
}
The initialization code of a blue cube looks as follows:
public void simpleInitApp() {
Box b = new Box(1, 1, 1); // create a box with half-extents of 1
Geometry geom = new Geometry("Box", b); // create a cube geometry from the box 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 geometry 's material
rootNode.attachChild(geom); // make the cube geometry appear in the scene
}
A typical JME3 game has the following initialization process:
-
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.
-
-
You initialize variables:
-
You create variables to track the game state.
-
You set variables to their start values.
-
Examples: Set the
scoreto 0, sethealthto 100%, …
-
-
You initialize keys and mouse actions:
-
The following input bindings are pre-configured:
-
W A S D keys – Move around in the scene
-
Mouse movement and arrow keys – Turn the camera
-
Esc key – Quit the game
-
Define your own additional keys and mouse click actions.
-
Examples: Click to shoot, press Space to jump, …
-
Organizing a Larger Application¶
SimpleApplication creates useful default application states for statistics, the fly camera, audio listening and debug keys. You can supply your own initial states to its constructor. When you do, the custom list replaces the defaults, so include any default states you still need.
public class MyGame extends SimpleApplication {
public MyGame() {
super(new MyCustomState(), new AnotherState());
}
public static void main(String[] args) {
new MyGame().start();
}
@Override
public void simpleInitApp() {
// Initialize the application, or delegate work to application states.
}
}
MyCustomState and AnotherState are placeholders for application states you implement. See Application States for initialization, updates and cleanup.
Conclusion¶
You have learned that a SimpleApplication is a good starting point because it provides you with:
-
A
simpleInitApp()method where you create objects. -
A
rootNodewhere 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 want to:
-
Initialize the game scene
-
Trigger game actions
-
Respond to user input.
See also: