Chapter 4, Adding Character to Your Game, Shows How to Load and Convert Models
Total Page:16
File Type:pdf, Size:1020Kb
jMonkeyEngine 3.0 Beginner's Guide Ruth Kusterer Chapter No. 2 "Creating Your First 3D Scene" In this package, you will find: A Biography of the author of the book A preview chapter from the book, Chapter NO.2 "Creating Your First 3D Scene" A synopsis of the book’s content Information on where to buy this book About the Author Ruth Kusterer became intrigued by Java and open source software while completing her degree in computational linguistics. In 2005, she joined Sun Microsystems, Inc. as a technical writer for netbeans.org where she wrote 100 NetBeans IDE Tips & Tricks. Since 2010, she has been working for CA Technologies, Inc. where she's a senior technical writer for security software. In her spare time, she hangs out on jMonkeyEngine.org and strives eternally to write the ultimate Java game. I would like to thank the jMonkeyEngine core team for answering a BigInteger of newbie questions and helping out with non-trivial code samples. I would also like to thank all the jMonkeys posting inspiring videos of completed games, and a big shout-out to the NetBeans community whose NetBeans platform is the base of the jMonkeyEngine SDK. For More Information: www.packtpub.com/jmonkeyengine-3-0-with-java-programming-beginners- guide/book jMonkeyEngine 3.0 Beginner's Guide "You, my brave hero, are about to embark on an adventure full of challenges and risks, but the reward at the end of your journey will be plentiful and will restore peace on earth. Are you ready?" You have probably played many games before reading this book, and gladly accepted challenges such as this one! Now you will face a new adventure. You will create your own video game. There too will be challenges, but jMonkeyEngine gives you the tools to overcome them. This book introduces Java developers to 3D game development and shows how jMonkeyEngine can make a game developer's life easier. Note that this book does not cover 3D model or sound design, nor the creative process of coming up with an original game concept—read the appendix for some related resources. By the end of this book, you will be ready to develop a 3D game, and have fun doing it! What This Book Covers Chapter 1, Installing jMonkeyEngine, helps you install the software and run a sample application. Chapter 2, Creating Your First 3D Scene, teaches you how to add objects and transform them. Chapter 3, Interacting with the User, reveals how to control game mechanics in the main loop. Chapter 4, Adding Character to Your Game, shows how to load and convert models. Chapter 5, Creating Materials, demonstrates how to manipulate the surface of objects. Chapter 6, Having Fun with Physics, teaches you how to make objects act solid or heavy. Chapter 7, Adding Spark to the Game, shows basic types of decorative effects. Chapter 8, Creating Landscapes, introduces terrains and environmental effects. Chapter 9, Making Yourself Heard, teaches how to integrate sounds and music. For More Information: www.packtpub.com/jmonkeyengine-3-0-with-java-programming-beginners- guide/book Chapter 10, Showing Your Game to the World, shows how to save, load, build, and distribute games. Appendix A, What's Next?, reveals how to make your games fun and challenging. Appendix B, Additional Resources for Fellow jMonkeys, introduces you to more advanced user interfaces. Free Download Chapter, Playing on the Network, explains network communication in multiplayer games. This chapter is available as a free download chapter at http://www.packtpub.com/sites/default/files/downloads/6464OS_F ree_Download_Chapter_Playing_on_the_Network.pdf For More Information: www.packtpub.com/jmonkeyengine-3-0-with-java-programming-beginners- guide/book 2 Creating Your First 3D Scene Now that you have installed the engine and understood the physical fi le structure of a project, we will take a look inside to understand the fl ow of a jMonkeyEngine game. In this chapter, you will learn: How to ini alize a scene How to refer to objects in the 3D scene How to posi on, rotate, and resize objects How to confi gure the way the scene is displayed How to navigate the scene using the keyboard and mouse It's me to look at some code. A basic template to initialize scenes From the previous chapter, you'll remember that we executed the BasicGame template to test whether the installa on was successful. This project template demonstrates the structure of a typical jMonkeyEngine main class that ini alizes the scene and starts the applica on. For More Information: www.packtpub.com/jmonkeyengine-3-0-with-java-programming-beginners- guide/book Crea ng Your First 3D Scene Time for action – initializing a scene step by step If you no longer have the BasicGame template open, open it again in the jMonkeyEngine SDK. Remember that you can always recreate this project template using the New Project wizard in the File menu. Let's look at the main class of the BasicGame project: 1. Open the BasicGame project in the Projects window. 2. Double-click on the mygame/Main.java fi le in the Source Packages folder. Main.java opens in the source editor. 3. When you look at the Main.java template, you see a standard Java class that extends the com.jme3.app.SimpleApplication class. SimpleApplication is the base class that we use to write all our 3D games. The basic code sample is short enough so that you can type it yourself—we just use the BasicGame template because it is quicker: import com.jme3.app.SimpleApplication; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import com.jme3.renderer.RenderManager; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Box; /** Basic jMonkeyEngine game template. */ public class Main extends SimpleApplication { @Override /** initialize the scene here */ public void simpleInitApp() { // create a cube-shaped mesh Box b = new Box(Vector3f.ZERO, 1, 1, 1); // create an object from the mesh Geometry geom = new Geometry("Box", b); // create a simple blue material Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.Blue); // give the object the blue material geom.setMaterial(mat); // make the object appear in the scene rootNode.attachChild(geom); } @Override /** (optional) Interact with update loop here */ [ 24 ] For More Information: www.packtpub.com/jmonkeyengine-3-0-with-java-programming-beginners- guide/book Chapter 2 public void simpleUpdate(float tpf) {} @Override /** (optional) Advanced renderer/frameBuffer modifications */ public void simpleRender(RenderManager rm) {} /** Start the jMonkeyEngine application */ public static void main(String[] args) { Main app = new Main(); app.start(); } } Downloading the example code You can download the example code fi les for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub. com/support and register to have the fi les e-mailed directly to you. You can right-click on the fi le and click on Run, if you want to see the applica on again. The Main class generates a simple scene containing a blue cube. Let's take a closer look at the code template to learn how to structure a jMonkeyEngine applica on. What just happened? The scene displayed by our BasicGame template only contains a blue cube, but this is enough to introduce you to the basic process that all scenes have in common, whether it's a haunted dungeon or an alien jungle. The cube, like any other scene object, is ini alized in the simpleInitApp() method . Let's walk through the simpleInitApp() method line by line to understand what is happening. The com.jme3.scene.shape.Box class provides us with a quick and simple box shape for tes ng. The Box() constructor expects a loca on, and three values defi ning the size of the box—we create the shape in the center of the scene, which we express in Java as Vector3f.ZERO. We choose to extend the box's size by 1 in every direc on, which results in a 2x2x2 cube. We refer to our Box()object with the variable b. The Java code that creates the box looks as fol lows: Box b = new Box(Vector3f.ZERO, 1, 1, 1); Internally, a Box() object contains numbers specifying the corner points of the cube. The corners describe the shape (mesh) of the object. But a cube is not just corners, it also has faces. The faces are the surfaces that make the cube visible in the fi rst place. In 3D graphics, you must specify a shape and a surface for every object. [ 25 ] For More Information: www.packtpub.com/jmonkeyengine-3-0-with-java-programming-beginners- guide/book Crea ng Your First 3D Scene jMonkeyEngine has a data type that combines the shape and surface informa on of an object. This data type is called geometry (com.jme3.scene.Geometry), and we will be using it a lot. Let's create a geometry geom for our cube shape b, and label the geometry Box. In larger scenes, you can use these labels to access geometries. In code, this step looks as follows: Geometry geom = new Geometry("Box", b); Our geometry now has a cube shape; next, we must add the surface informa on for the cube's faces. In 3D graphics terms, the surface of a geometry has a material (com.jme3. material.Material). In this example, we use a type of material called Unshaded. j3md. This default material defi ni on is built into the engine, and we access it using the assetManager object . You create the material using the following line of code: Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); Every material has parameters that specify how the surface looks.