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.
Create a jme3test.helloworld
package and a file HelloJME3.java
in it.
In NetBeans, you would right-click the Source Packages node
New… > Java Class
to create a new file.HelloJME3
jme3test.helloworld
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.
Congratulations, it works! How did we do that?
Here some basic rules that are valid for all JME3 games:
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.
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:
rootNode
.score
to 0, and health
to 100%, and so on. 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.
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:
simpleInitApp()
method to initialize the game objectsrootNode
where you attach geometries to make them appear in the sceneIn a real game, you will want to:
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