📜 ⬆️ ⬇️

Trying 3D with jMonkeyEngine

Practically everyone who has been involved in game-devoting understands that the best performance in this area, for obvious reasons, can only be achieved in C / C ++ / asm. I will not argue with this statement in this article, and even before I didn’t even think about creating real-time games in other languages. However, the games are different, and you need to be aware of the extent to which labor costs for the implementation of the next product are justified. If the project is monstrous, with a “heavy” graphic component, even after carrying out a very aggressive optimization, it still requires top-end hardware on board from end users (gamers), then there are no special competitors among the development languages ​​of C / C ++ in this case . But if a toy is no more than an ordinary kazualka, for launch of which a netbook will be enough, then you can allow yourself a lot more room for imagination in the matter of choosing tools for development. Based on this principle, in this article we will try to figure out what this, it would seem, not quite suitable for this, but generally convenient Java language, using the example of the jME engine, can boast in the field of gamedev.

Intro


jMonkeyEngine . There's nothing you can do about it, the engine is really called that, and even the slogan on the site says “Serious monkeys. Serious engine "(" Serious monkeys. Serious engine "). Some developers obviously have something to do with these animals (the same RenderMonkey is remembered for developing and debugging shaders).

However, the only comic in the engine is the name. jME supports everything you need to create games in a comfortable and reasonably fast manner, and it isn’t just a graphical visualizer there is also a input processing system and a library of physics simulation and audio playback. In addition to the standard set of tools inherent in many other engines, you can note such features as:

Installation and the simplest example of use


We load the ready engine , or we merge source codes from repositories and we collect. In both cases, the output we obtain the necessary library for work, which we place in any convenient place on the disk.
Now we create an empty project in Eclipse and go to Properties -> Java build path:

image
')
Poke the Add External JARs and add the libraries from the folders / path / to / the / engine / lib (the engine packages themselves) and / path / to / the / engine / lib / lib / lwjgl (the visualizer packages that will be used by the engine in this case lwjgl). Of course, it is not necessary to add all the packages, only those that are actually used are sufficient. It should look like this:

image

In the properties of the LWJGL packages we specify the native libraries necessary for their work, located at / path / to / the / engine / lib / lib / lwjgl / native / <your_os>. For example:

image

Initial preparations are completed, now we will create a new class (Program, for example). We will make this class inheriting the SimpleGame class, which is well suited for test programs, since automatically adjusts all necessary parameters and creates a first-person camera. The program will do two simple things - create a cube and load the model from the .obj file.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Box;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.ObjToJme;

public class Program extends SimpleGame
{
public static void main( String [] args)
{
Program game = new Program();
//
game.setConfigShowMode(ConfigShowMode.AlwaysShow);
game.start();
}

@Override
protected void simpleInitGame()
{
//
Box b = new Box( "Testbox" , new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
//
b.setModelBound( new BoundingBox());
//
b.updateModelBound();
// (rootNode SimpleGame)
rootNode.attachChild(b);

// .obj
ObjToJme converter = new ObjToJme();
URL full_mesh_path = Program. class .getResource( "mesh.obj" );
TriMesh mesh = null ;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try
{
converter.convert(full_mesh_path.openStream(), output);
mesh = (TriMesh)BinaryImporter.getInstance().load(output.toByteArray());
}
catch (IOException e)
{
System.exit(1);
}
mesh.setModelBound( new BoundingBox());
mesh.updateModelBound();
mesh.setLocalTranslation(Vector3f.UNIT_Y.mult(5));
rootNode.attachChild(mesh);
}
}

* This source code was highlighted with Source Code Highlighter .

Nothing complicated. It is worth paying attention to the line

URL full_mesh_path = Program.class.getResource ("mesh.obj");

So we tell the program to look for the mesh.obj file in the same place as the Program.class file. This can be convenient, for example, if game resources will lie inside the .jar archive containing the program's .class files.

Now the program should be launched, display a dialog box with a choice of graphic settings, create a window and display in it a manually created cube and a loaded grid model. The SimpleGame class has the ability to display runtime statistics, which is enabled by pressing F4. Of course, in real projects, such default things are hardly needed by anyone, so for games it is more serious than Hello, world , it is better to use the StandardGame class, or if you go up even higher in the hierarchy, BaseGame, where you have to write all the initialization yourself.

Now prepare a program for distribution. To do this, run File -> Export -> Runnable JAR file. In the window that appears, select the exported program, the destination folder, check the Package required libraries into generated JAR. After clicking finish, we have a .jar file containing the executable program code and all the necessary engine libraries. To start, you must also copy the native library from the folder that was specified at the beginning, to the program folder (for example, in ./lib). To run run
java -Djava.library.path =. / lib -jar program.jar

The program will be able to download the mesh.obj file, if it is located either in the folder with program.jar, or directly in the program.jar file. You can add a file inside the archive with any archiver.

It is quite a natural question here - and how quickly does it work? .. It’s quite a natural answer - see for yourself . The site has a certain number of demos downloaded via jnlp, including those not as primitive as this example. On them it is possible to estimate the speed of work.

The finish


At this introductory article can be considered complete. A simple example clearly shows the ease of creating games with jME. On the other hand, he does not disclose the real capabilities of the engine, but the wiki on the off-site, as well as javadoc , reveals them, so that those who are interested in ahead of them have room for creativity and all available platforms for conquest.

Work safe. Work smart. Your future depends on it © Half Life railway

Source: https://habr.com/ru/post/88254/


All Articles