jMonkeyEngine 3.0 Cookbook Over 80 practical recipes to expand and enrich your jMonkeyEngine skill set with a close focus on game development Rickard Edén BIRMINGHAM - MUMBAI jMonkeyEngine 3.0 Cookbook Copyright © 2014 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2014 Production reference: 1060814 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78328-647-8 www.packtpub.com Cover image by Aniket Sawant ([email protected]) FM-2 Credits Author Project Coordinator Rickard Edén Aaron. S. Lazar Reviewers Proofreaders Abner Coimbre Simran Bhogal Benjamin Jakobus Ameesha Green Nicolas Legeay Paul Hindle Nicholas Mamo Indexer Glauco Márdano Priya Subramani Commissioning Editor Graphics Rebecca Youé Abhinash Sahu Acquisition Editor Production Coordinator Rebecca Youé Aparna Bhagat Content Development Editor Cover Work Manasi Pandire Aparna Bhagat Technical Editors Sushma Redkar Manal Pednekar Anand Singh Ankita Thakur Copy Editors Gladson Monteiro Adithi Shetty Stuti Srivastava Laxmi Subramanian FM-3 About the Author Rickard Edén is a Java and Android development consultant, game developer, and lifelong game enthusiast, based in Gothenburg, Sweden. Having worked in the past as a designer in the gaming industry, Rickard has worked on a number of different genres and picked up tricks of the trade from many disciplines. He has a pragmatic approach to programming and has developed games for many years using jMonkeyEngine. It is now his intention to help other developers by sharing what he's learned so far with this book. Writing this book has been a tremendous experience, and it certainly could not have been possible without other people clearing the way. If I have been the storyteller, the heroes of the book are the people who have created jMonkeyEngine. From its first incarnation until now, the third, when it's a fully fledged game development environment. FM-4 About the Reviewers Abner Coimbre, before becoming a technical reviewer for this book, was a lead developer for an educational game funded by the National Science Foundation (NSF Award No. 0835990) where he used jMonkeyEngine to achieve most of the technical milestones that were required for the project. At the time of this writing, he is a system software engineer at NASA, working with a Command and Control System. His home page is abnercoimbre.com. Sending out warm regards to some of the principal investigators while I was working for the National Science Foundation: Dr. Agustin Rullán, Dr. Bienvenido Vélez, Dr. Cristina Pomales, and Dr. Félix Zapata. It was awesome working with you guys. I appreciate the guidance and scoldings of Linda Crawford, an amazing mentor and human being. I thank all the staff involved in the making of this book, particularly Aaron. S. Lazar for putting up with my erratic work schedule. And, of course, I thank the author many times over for having written a book that will truly help people leverage the interesting features that jMonkeyEngine provides. Benjamin Jakobus graduated with a BSc in Computer Science from University College Cork, after which he cofounded an Irish start-up. He returned to the university one year later and obtained an MSc in Advanced Computing from Imperial College, London, after which he took up a position as a software engineer in IBM, Ireland. He currently lives in Brazil where he is pursuing a PhD at PUC-Rio. FM-5 Nicolas Legeay discovered jMonkeyEngine in 2004, during his studies, when he had to choose an environment to elaborate on and develop a 3D online game project that he had to execute on his own. It has been really exciting for him to be here at the start of this wonderful engine, and Nicolas then developed several projects with it, to explore all the possibilities that JME allowed then. Originally a software developer, Nicolas had the opportunity to discover quality assurance in the industrial sector of the rail in 2006. Over the QA activity, he especially appreciates test automation. His professional experience helped him acquire a proven expertise in this practice. Indeed, in charge of elaborating test strategies in various contexts, test automation always proved a relevant choice; also, he knew when to use the most adapted tools. Being tech-savvy, Nicolas succeeded in elaborating innovative ways to build automata, such as image recognition and CAPTCHA hacking, making him a very well-appreciated resource regarding his highly sought skills. Nicolas has had the privilege to work with various and prestigious companies, such as National Opera of Paris, France and PMU, France, on big and crucial projects. Nicholas Mamo, born in Malta in 1996, is a full-time student and a freelance writer. He is a self-taught game developer and has been developing games at Nyphoon Games since 2008, regularly blogging about his projects on nyphoon.com. His articles have been published on Eye For Games, IGDA, and Tuts+, among others. Nicholas is also passionate about football and his opinion pieces have appeared on a number of websites. Glauco Márdano is 23 years old, and graduated in Systems Analysis. He works as a Java programmer in a private company in Brazil. He has studied a lot about game programming just for fun, and he dreams of building his own business someday. He was also the technical reviewer for jMonkeyEngine 3.0 Beginners Guide and Augmented Reality for Android Application Development for Packt Publishing. Well, I'd like to thank everybody from jMonkeyEngine's forum, because I've learned a lot from them; the forum is very active. I'd also like to thank Aaron. S. Lazar from Packt Publishing for his help while reviewing this book. FM-6 www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. FM-7 Table of Contents Preface 1 Chapter 1: SDK Game Development Hub 7 Introduction 7 Setting up a project 8 Importing a model 9 Using Scene Composer 9 Modifying heightmaps with Terrain Editor 12 Adding a sky box and lighting 14 Adding water using a filter 16 Adding some ambient audio 18 Creating bitmap fonts with Font Creator 20 Retrieving an attachment node 21 Using ParticleEmitter – Soaring Birds 22 An advanced ParticleEmitter class 25 Chapter 2: Cameras and Game Controls 31 Introduction 31 Creating a reusable character control 32 Attaching an input AppState object 37 Firing in FPS 40 Firing non-instant bullets 42 Creating an RTS camera AppState object 45 Selecting units in RTS 50 Making the camera follow units 54 Following a character with ChaseCamera 56 Adding a game controller or joystick input 58 Leaning around corners 60 Detecting cover automatically in a third-person game 63