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.
Create a jme3test.helloworld
package and a file HelloJME3.java
in it.
In the jMonkeyPlatform, you 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(); // start the game
}
@Override
public void simpleInitApp() {
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.
Congratulations, it works! How did we do that?
The code above has initialized the scene, and started the game.
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. |
In Java, the creation of a blue cube looks as follows:
public void simpleInitApp() { 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:
rootNode
.score
to 0, set health
to 100%, and so on.
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.
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:
simpleInitApp()
method where you create objects.rootNode
where you attach objects to make them appear in the scene.When developing a game application, you will now want to:
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: