hello_simpleapplication.adoc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. = jMonkeyEngine 3 Tutorial (1) - Hello SimpleApplication
  2. :revnumber: 3.0
  3. :revdate: 2022/03/22
  4. :keywords: beginner, intro, documentation, init, simpleapplication, basegame
  5. *Prerequisites:* This tutorial assumes that you have <<ROOT:documentation.adoc#install,downloaded the jMonkeyEngine SDK>>.
  6. In this tutorial series, we assume that you use the jMonkeyEngine xref:sdk:sdk.adoc[SDK]. As an intermediate or advanced Java developer, you will quickly see that, in general, you can develop jMonkeyEngine code in any integrated development environment (NetBeans IDE, Eclipse, IntelliJ) or even from the xref:ROOT:getting-started/simpleapplication_from_the_commandline.adoc[command line].
  7. OK, let's get ready to create our first jMonkeyEngine3 application.
  8. == Create a project
  9. In the jMonkeyEngine SDK:
  10. . Choose `menu:File[New Project]` from the main menu.
  11. . In the New Project wizard, select the template `menu:JME3[Basic Game]`.
  12. . Click btn:[Next].
  13. .. Specify a project name, e.g. "`HelloWorldTutorial`".
  14. .. Specify a path where to store your new project, e.g. a `jMonkeyProjects` directory in your home directory.
  15. . Click btn:[Finish].
  16. This will create a basic jme3 application for an easy start with jme3. You can click the run button to run it: You will see a blue cube.
  17. If you have questions, read more about xref:sdk:project_creation.adoc[Project Creation] here.
  18. [TIP]
  19. ====
  20. We recommend to go through the steps yourself, as described in the tutorials. Alternatively, you can create a project based on the xref:sdk:sample_code.adoc[JmeTests] template in the jMonkeyEngine SDK. It will create a project that already contains the `jme3test.helloworld` samples (and many others). For example, you can use the JmeTests project to verify whether you got the solution right.
  21. ====
  22. == Code Sample
  23. Main.java contains the following example code.
  24. [source,java]
  25. ----
  26. package mygame;
  27. import com.jme3.app.SimpleApplication;
  28. import com.jme3.material.Material;
  29. import com.jme3.scene.Geometry;
  30. import com.jme3.scene.shape.Box;
  31. import com.jme3.math.ColorRGBA;
  32. /** Sample 1 - how to get started with the most simple JME 3 application.
  33. * Display a blue 3D cube and view from all sides by
  34. * moving the mouse and pressing the WASD keys. */
  35. public class Main extends SimpleApplication {
  36. public static void main(String[] args){
  37. Main app = new Main();
  38. app.start(); // start the game
  39. }
  40. @Override
  41. public void simpleInitApp() {
  42. Box b = new Box(1, 1, 1); // create cube shape
  43. Geometry geom = new Geometry("Box", b); // create cube geometry from the shape
  44. Material mat = new Material(assetManager,
  45. "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
  46. mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue
  47. geom.setMaterial(mat); // set the cube's material
  48. rootNode.attachChild(geom); // make the cube appear in the scene
  49. }
  50. }
  51. ----
  52. btn:[RMB] select the `Main` class and choose `Run`. If a jME3 settings dialog pops up, confirm the default settings.
  53. . You should see a simple window displaying a 3D cube.
  54. . Press the kbd:[W] kbd:[A] kbd:[S] kbd:[D] keys and move the mouse to navigate around.
  55. . 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.)
  56. . Press kbd:[Esc] to close the application.
  57. Congratulations! Now let's find out how it works!
  58. == Understanding the Code
  59. The code above has initialized the scene, and started the application.
  60. === Start the SimpleApplication
  61. Look at the first line. Your Main.java class extends `com.jme3.app.SimpleApplication`.
  62. [source,java]
  63. ----
  64. public class Main extends SimpleApplication {
  65. // your code...
  66. }
  67. ----
  68. Every JME3 game is an instance of the `com.jme3.app.SimpleApplication` class. 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.
  69. You start every JME3 game from the main() method, as every standard Java application:
  70. . Instantiate your `SimpleApplication`-based class
  71. . Call the application's `start()` method to start the game engine.
  72. [source,java]
  73. ----
  74. public static void main(String[] args){
  75. Main app = new Main(); // instantiate the game
  76. app.start(); // start the game!
  77. }
  78. ----
  79. The `app.start();` line opens the application window. Let's learn how you put something into this window (the scene) next.
  80. === Understanding the Terminology
  81. [cols="2", options="header"]
  82. |===
  83. a|What you want to do
  84. a|How you say that in JME3 terminology
  85. a|You want to create a cube.
  86. a|I create a Geometry with a 1x1x1 Box shape.
  87. a|You want to use a blue color.
  88. a|I create a Material with a blue Color property.
  89. a|You want to colorize the cube blue.
  90. a|I set the Material of the Box Geometry.
  91. a|You want to add the cube to the scene.
  92. a|I attach the Box Geometry to the rootNode.
  93. a|You want the cube to appear in the center.
  94. a|I create the Box at the origin = at `Vector3f.ZERO`.
  95. |===
  96. If you are unfamiliar with the vocabulary, read more about xref:concepts/the_scene_graph.adoc[the Scene Graph] here.
  97. === Initialize the Scene
  98. Look at rest of the code sample. The `simpleInitApp()` method is automatically called once at the beginning when the application starts. Every JME3 game must have this method. In the `simpleInitApp()` method, you load game objects before the game starts.
  99. [source,java]
  100. ----
  101. public void simpleInitApp() {
  102. // your initialization code...
  103. }
  104. ----
  105. The initialization code of a blue cube looks as follows:
  106. [source,java]
  107. ----
  108. public void simpleInitApp() {
  109. Box b = new Box(1, 1, 1); // create a 1x1x1 box shape
  110. Geometry geom = new Geometry("Box", b); // create a cube geometry from the box shape
  111. Material mat = new Material(assetManager,
  112. "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
  113. mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue
  114. geom.setMaterial(mat); // set the cube geometry 's material
  115. rootNode.attachChild(geom); // make the cube geometry appear in the scene
  116. }
  117. ----
  118. A typical JME3 game has the following initialization process:
  119. . You initialize game objects:
  120. ** You create or load objects and position them.
  121. ** You make objects appear in the scene by attaching them to the `rootNode`.
  122. ** *Examples:* Load player, terrain, sky, enemies, obstacles, …, and place them in their start positions.
  123. . You initialize variables:
  124. ** You create variables to track the game state.
  125. ** You set variables to their start values.
  126. ** *Examples:* Set the `score` to 0, set `health` to 100%, …
  127. . You initialize keys and mouse actions:
  128. ** The following input bindings are pre-configured:
  129. *** kbd:[W] kbd:[A] kbd:[S] kbd:[D] keys – Move around in the scene
  130. *** Mouse movement and arrow keys – Turn the camera
  131. *** kbd:[Esc] key – Quit the game
  132. ** Define your own additional keys and mouse click actions.
  133. ** *Examples:* Click to shoot, press kbd:[Space] to jump, …
  134. == Conclusion
  135. You have learned that a SimpleApplication is a good starting point because it provides you with:
  136. * A `simpleInitApp()` method where you create objects.
  137. * A `rootNode` where you attach objects to make them appear in the scene.
  138. * Useful default input settings that you can use for navigation in the scene.
  139. When developing a game application, you want to:
  140. . Initialize the game scene
  141. . Trigger game actions
  142. . Respond to user input.
  143. *See also:*
  144. * xref:sdk:project_creation.adoc[Create a JME3 project]