Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

The overall goal of this book is to provide you with an extensive toolbox of hands-on development tips for jMonkeyEngine that will help you be well prepared for a wide range of projects.

The recipes are written from a practical point of view, and I strived to make sure that each recipe has an outcome that can be used directly in a game. An exception to this is Chapter 7, Networking with SpiderMonkey, where we will start from the absolute ground level and work ourselves upwards. Chapter 1, SDK Game Development Hub, also stands out as it contains a few more general tips applicable to development in general.

Due to the variation in game projects, another principle has been used to create recipes that have a wide usage potential. Some of the more advanced recipes are exceptions to this rule. They have a more narrow use but contain techniques that can be applied to other implementations. FPS, RTS, and RPG games will be explored in many of the recipes. Naturally, within these genres, games differ widely as well, but hopefully you will find that the examples can be used in your game project with a minimum amount of modification.

In general, I hope that this book will provide you with many tips on how to overcome common hurdles in game projects so that you can focus on the creative parts that make your game stand out.

Common development concepts in jMonkeyEngine

Some common development concepts in jMonkeyEngine are as follows:

  • Spatial: Central to all things in the scene graph is the Spatial. In jMonkeyEngine, it's an abstract class, defining translation (location), rotation, and scale of an object. Imagine it as a purely logical object without a physical body. A Spatial is extended into either a Geometry or Node.

  • Geometry: This extends Spatial. This class is what gives a Spatial its physical presence in the world. It has a Mesh defining its form and shape, and a Material, telling us what the surface of the Mesh looks like.

  • Node: This extends Spatial. It can have several children attached, and can in turn be attached to a parent. Since it's a Spatial, it has a translation, rotation, and scale, which it will propagate to its children. Unlike a Geometry, it can't have a Mesh. It doesn't have a visible presence in itself.

  • Transforms: Translation, rotation, and scale are commonly referred to as a Spatial's transforms. A Spatial has both local and world transforms. The local transform is always in relation to its parent (if any). The world transform is the absolute transform with all possible parent transforms propagated together. As an example, imagine your own local translation being the position you have on Earth. Your world translation could be your local translation added to the Earth's local translation in its orbit around the Sun. Normally, you will only work with local transforms. World transforms are handled by the engine.

  • Mesh: This is made up of triangles. In a Mesh, you will find long lists called buffers detailing the points of these triangles (vertices), how the surfaces of these triangles are made (indices), their colors, normal, and other data. Normally, you will load a model from disk, not having to care about what it looks like on this level, but some recipes will create meshes from scratch, and I recommend having a basic understanding of meshes when you do 3D game development.

  • Material: This defines the surface of the Mesh. It's backed by a Material Definition (MatDef), usually containing a vertex shader and a fragment shader. The complexity stretches from simply setting a color or texture for a mesh, to ones that alter the shape of the Mesh. You will get far by using jMonkeyEngine's predefined MatDef.

These are some of the basic must-know concepts of the engine. The following concepts can be considered nice-to-know. I know from experience that these are the ones you wish you had known when your applications were in the process of being developed. They will also be used quite frequently throughout the chapters of this book:

  • AppState: This is an object that affects the behavior of the whole application, and you can, for example, use them to specify logic for different parts of the application. An example could be if you have a game that is played out both on a campaign map and on the ground, both player input and game logic would probably differ quite a lot. By using the AppState class, you can manage the code more easily and avoid monstrous classes.

  • Control: Like AppState, this is a good way to manage your code. The Control affects the Spatial it's attached to, and is commonly used to give Spatials special functionalities. You will see several examples of Control behavior in many of the chapters, so if you prefer learning-by-doing, you're in for a treat.

What this book covers

Chapter 1, SDK Game Development Hub, will take a tour around the SDK, always with the game in mind. Learn about built-in functions and plugins that make your life easier.

Chapter 2, Cameras and Game Controls, contains a number of concrete ways to use cameras and control avatars for a variety of game types.

Chapter 3, World Building, explores different methods you can use to create and modify environments for games.

Chapter 4, Mastering Character Animations, enables you to learn all you need to know about controlling character animations.

Chapter 5, Artificial Intelligence, contains a look at the basics and common challenges of AI for games.

Chapter 6, GUI with Nifty GUI, contains techniques to develop a lot of the common user interfaces a game needs.

Chapter 7, Networking with SpiderMonkey, is an introduction to UDP/TCP networking for games in jMonkeyEngine.

Chapter 8, Physics with Bullet, will teach you the Bullet implementation and how to apply it to your games.

Chapter 9, Taking Our Game to the Next Level, will tell you what to do when your game mechanics are in and the game is playable. Still, you will feel the game lacks something. This chapter shows different methods to advance your game further in quality.

Appendix, Information Fragments, contains some generic pieces of code and instructions that can be used across chapters. It also has some full-code segments for recipes that are too long to include in the chapters themselves.

What you need for this book

This book will assume that you already have some experience with either jMonkeyEngine or similar scene-graph-based engines. If you're completely new to this, we'll go through some common concepts in the following pages to give you a basic introduction to 3D game development. If you'd like more in-depth descriptions of these (and other) concepts, I recommend reading and performing the tutorials found at hub.jmonkeyengine.org.

Who this book is for

This book is ideal for intermediate to advanced users of the jMonkeyEngine 3.0 game development engine.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Start by creating a new class called GameCharacterControl, which extends BetterCharacterControl."

A block of code is set as follows:

public void update(float tpf) {
  super.update(tpf);
  Vector3f modelForwardDir = spatial.getWorldRotation().mult(Vector3f.UNIT_Z);
  Vector3f modelLeftDir = spatial.getWorldRotation().mult(Vector3f.UNIT_X);
  walkDirection.set(0, 0, 0);

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

camLocation.setY(checkHeight() + camDistance);
cam.setLocation(camLocation);

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Go to the File menu and select Import Model."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files 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 files e-mailed directly to you.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/6478OS_ColoredImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.