Mobile Game Development on Android Matthew R. Young Problem Report submitted to the Benjamin M. Statler College of Engineering and Mineral Resources at West Virginia University in partial fulfillment of requirements for the degree of Master of Science in Computer Science Frances L. Van Scoy, Ph.D., Chair James D. Mooney, Ph.D. Arun A. Ross, Ph.D. Lane Department of Computer Science and Electrical Engineering Morgantown, West Virginia 2012 Keywords: Android, OpenGL, Java, Game Development Copyright 2012 Matthew R. Young ABSTRACT Mobile Game Development on Android Matthew R. Young Mobile device popularity has flourished in the past two years and continues to gain momentum. Devices utilizing multi-touch screens such as tablets and smartphones have created a new environment that opens up many possibilities for gaming. The purpose of this project is to explore computer graphics and gaming on mobile devices. To effectively explore this area, an arcade shooter game, similar to Asteroids, was created and the development process has been detailed to allow the reader to recreate the game while learning about graphics programming on the Android platform. iii Acknowledgments I would like to thank Dr. Van Scoy for being my committee chairperson and for her guidance and feedback during this endeavor. I also want to thank Dr. Mooney and Dr. Ross for being on my committee. Lastly, I want to express my gratitude to the Lane Department of Computer Science and Electrical Engineering for the financial assistance received during my graduate studies by being awarded a graduate teaching assistantship for CS101 and for the invaluable teaching experience gained. iv Table of Contents Acknowledgments........................................................................................................ iii Table of Contents ......................................................................................................... iv 1. Introduction ............................................................................................................... 1 2. Mobile Devices .......................................................................................................... 2 3. Matrix Primer ............................................................................................................. 3 3.1 World Space and Transformations ......................................................................... 4 3.1.1 Translation ....................................................................................................... 4 3.1.2 Scaling ............................................................................................................. 4 3.1.3 Rotation ........................................................................................................... 5 3.1.4 Other Transformations ..................................................................................... 5 3.2 Eye Space and the View Matrix ............................................................................. 6 3.3 Screen Space and the Projection Matrix ................................................................ 6 3.3.1 Orthographic Projection Matrix ........................................................................ 6 3.3.2 Perspective Projection Matrix .......................................................................... 7 3.4 Putting it all together .............................................................................................. 8 4. OpenGL ...................................................................................................................... 8 4.1 OpenGL Shading Language (GLSL) .................................................................... 10 4.1.1 Uses of Shaders ............................................................................................ 10 4.1.2 GLSL Data Types .......................................................................................... 11 4.1.3 Vertex Shaders .............................................................................................. 11 4.1.4 Fragment Shaders ......................................................................................... 11 4.1.5 Shader Example ............................................................................................ 12 4.2 OpenGL ES 2.0 .................................................................................................... 12 5. Gaming Environments ............................................................................................ 13 6. Developing Demise ................................................................................................. 14 7. Game Components ................................................................................................. 15 7.1 User Input ............................................................................................................ 15 7.2 Overview of Game Classes .................................................................................. 15 7.2.1 GameManager Class ..................................................................................... 16 7.2.2 Level Class .................................................................................................... 16 7.2.3 MovingObjects Class ..................................................................................... 17 7.2.4 Player Class .................................................................................................. 17 7.2.5 HUD Class ..................................................................................................... 17 8. Demise: A Tutorial ................................................................................................... 18 8.1 Creating the Renderer .......................................................................................... 18 8.2 Creating the GLHelper Class ............................................................................... 20 8.3 Vector3d Class ..................................................................................................... 21 8.4 Drawing Objects ................................................................................................... 21 8.5 Background .......................................................................................................... 23 8.6 Model Class ......................................................................................................... 27 8.7 Joystick Movement ............................................................................................... 28 8.8 MovingObject Classes ......................................................................................... 30 8.8.1 PlayerObject Classes .................................................................................... 32 8.8.2 EnemyObject Classes ................................................................................... 32 v 8.9 Explosion ............................................................................................................. 33 8.10 Game Levels ...................................................................................................... 34 8.11 GameManager ................................................................................................... 35 8.12 Player Class ....................................................................................................... 35 8.13 Heads-up Display ............................................................................................... 37 8.14 SoundManager................................................................................................... 38 9. Future Work / Expansion ........................................................................................ 39 10. Challenges ............................................................................................................. 40 11. References ............................................................................................................. 41 Appendix A: Source Code .......................................................................................... 42 Background.java ........................................................................................................ 42 Enemy1.java .............................................................................................................. 45 Enemy2.java .............................................................................................................. 46 Enemy3.java .............................................................................................................. 48 EnemyObject.java ...................................................................................................... 49 EnemyShip.java ......................................................................................................... 49 Explosion.java ............................................................................................................ 49 GameActivity.java ...................................................................................................... 52 GameManager.java.................................................................................................... 53 GameOverActivity.java ............................................................................................... 54 GLESRenderer.java ................................................................................................... 55 GLESSurfaceView.java .............................................................................................. 56 GLHelper.java ............................................................................................................ 56 HUD.java .................................................................................................................... 60 Joystick.java ............................................................................................................... 66 JoystickListener.java .................................................................................................. 69 Level.java ................................................................................................................... 70 Level1.java ................................................................................................................. 74 MainMenuActivity.java ............................................................................................... 75 Model.java .................................................................................................................. 76 MovingObject.java ..................................................................................................... 80 Player.java ................................................................................................................. 86 PlayerBullet.java ........................................................................................................ 90 PlayerObject.java ....................................................................................................... 90 PlayerShip.java .......................................................................................................... 90 PlayerShip1.java ........................................................................................................ 92 SoundManager.java ................................................................................................... 93 TutorialActivity.java .................................................................................................... 94 Vector3d.java ............................................................................................................. 94 1 1. Introduction The purpose of this project is to better understand computer graphics on mobile devices. Computer graphics have been a strong interest of mine in the past several years which led to choosing this area of study. One of the most effective ways to demonstrate graphics concepts is to create an interactive video game. Due to a strong personal interest in game development, this medium was chosen for the project. Developing a game involves the same software engineering principles as a traditional computer program. However, instead of displaying text to the user as output, graphics are rendered and presented to the user. The paper is divided into two major sections. The first consists of background information in computer graphics to establish some understanding of the concepts used in the project. The second portion consists of an explanation of the game Demise created for this project. The game structure is discussed as well as a guide on creating the game from scratch. Demise is homage to retro 2D arcade shooters such as Asteroids and similar to modern games such as Geometry Wars and Waves. The player controls a ship and is allowed to move around the map and can fire in any direction while defending against a never ending onslaught of enemies. The goals of the game are simple—survive the longest and achieve the highest score. Many of the prominent computer graphics concepts are implemented in Demise. The game objects are three dimensional models so the game must implement a model loader in order to load the attributes such as vertex and normal information from external files. Textures are used heavily in order to create a visually appealing, multiple layer background where the gameplay takes place. To provide graphical effects shaders are implemented that allow model information to be modified at runtime by the GPU. The intended goal of this project is to provide some base understanding to those interested in mobile game development and to provide an example game as a guide. 2 There are many features that can be implemented that are currently not included in the finished product and the reader is invited to use this project as a building block for future development. 2. Mobile Devices Mobile devices are becoming increasingly prevalent in today’s society after the rising popularity of smartphones such as the iPhone, Android, and Windows based phones. Due to the success of the mobile operating systems and the multi-touch capable screens, tablet devices are also becoming very popular by providing a much larger screen and offering the same functionality as their smaller phone counterpart. The multi-touch screens of modern mobile devices have created an environment that allows for effective human-computer interaction without the need for physical input devices such as a mouse and keyboard. The mouse has been replaced by the ability to press the screen to click and the keyboard has been replaced with an onscreen keyboard. This allows for a more compact mobile device, and many of the current devices are more than capable of performing many everyday personal computing needs such as email, web, document processing, and even software development. As with any new technology, the gaming industry always finds a way to utilize it to create new and innovative ways to develop and play games. Gaming on cell phones have been popular since the 1990s and with the power and capability of current mobile devices, it is no surprise that gaming has been tremendously successful. The multi- touch screens allow for direct interaction with the screen and can be used to move avatars around a computer generated world or play a game of Scrabble by dragging letters across the board just like the physical game. There is an endless possibility for games to utilize the technology in mobile devices. According to a study conducted by ABI Research, it is estimated that the revenue generated from mobile gaming was $5 billion in 2011 and is estimated to increase to $16 billion by 2016 [6]. This revenue potential can be very appealing to independent developers who are looking for a platform that allows for self-publishing and for which 3 the only development cost is the initial development license and a portion of revenue earned. Most successful mobile games can be classified as casual games. Casual games often refer to those that do not require large time commitments and have a low learning curve. The appeal of casual games is the ability to play them for a few minutes at a time while still providing entertainment in that short window of free time. There are many platform choices for developers including Apple iOS, Android, and Windows phone. Most successful mobile applications utilize technologies that allow for high portability across platforms for maximum success. Android was chosen as the platform for this project for a few reasons. The leading factor is that Android devices control 48.6% of the smartphone market share followed by Apple iOS at 29.5% [4]. The popularity of Android devices can be contributed to the fact that multiple phone and tablet manufacturers, such as Samsung, Motorola, and LG, are utilizing the Android operating system whereas iOS is an Apple-only operating system on the iPhone and iPad. Two other contributing factors for choosing Android were the availability of a current Android device and the ease of use of the Android SDK. 3. Matrix Primer A crucial element of graphics programming is the ability to utilize and manipulate matrices to achieve a desired transformation. Typical transformations include translation, rotation, and scaling. Within each of these transformations, a function exists that takes a point and maps it to another point [1]. Matrices are not only used for applying transformations to objects of a scene but are essential to converting a three dimensional world space to a two dimensional screen. This is referred to as the projection matrix and is discussed further in this section. 4 3.1 World Space and Transformations To begin, an object contains a list of vertices, known as a vertex array. This is often referred to by model coordinates and is located in what is called object space. These are simply the coordinates before being placed into the scene. World space refers to objects whose local coordinates are within the scene. Within world space several transformations can be applied to manipulate an object’s vertices to produce desired effects. These transformations change the size of the object, its position in the world, or both. 3.1.1 Translation A translation displaces a homogeneous point P = (X, Y, Z, 1) by a factor of D = (αX, αY, αZ, 1). The new location, P’, is the result of the displacement. For example, if we wanted to move the point (1, 1, 10, 1) five units in the X direction and three units in the Y direction, our displacement vector would be (5, 3, 0, 1) which would give us a new point of (6, 4, 10, 1). In Figure 3.1, we can see how we can use matrices to achieve this transformation. ( ) ( ) ( )( ) Figure 3.1 - Translation Matrix [1] 3.1.2 Scaling A scaling transformation simply increases or decreases the point by a specified factor. This transformation is performed based on an origin point which can either be the true origin or one that has been translated previously. Scaling can be performed in one or all axis directions. If we wanted to transform a sphere to be an ellipsoid shape we could multiple the X and Z coordinates by a factor of 0.5 which would result in the height of the shape being twice the length of the width and depth. The scaling matrix is shown in Figure 3.2. 5 ( ) ( ) ( )( ) Figure 3.2 - Scaling Matrix [1] 3.1.3 Rotation The last key transformation is rotation. With this transformation, a given point is rotated about an origin (world or translated) by a given angle. For example we may have a point (4, 5, 9) and we want to rotate this point 45 degrees around the Y axis and 30 degrees around the Z axis. This transformation requires a little more calculation compared to translation and scaling because it must be performed for each axis rotation. It is also very important to note that the order of the rotations determine the final result of the transformation (e.g. rotating 90 degrees around the X axis then rotating 90 degrees around the Y axis produces a different result in reverse). The three rotation transformations are provided in Figure 3.3. ( ) ( )( ) ( ) ( )( ) ( ) ( )( ) Figure 3.3 - Rotation Matrices [1] 3.1.4 Other Transformations Translation, scaling, and rotation are the most common transformations used in graphics. However, these are not the only basic transformations that exist. Others include shearing and reflection. Additional transformations can be achieved by combining these basic types of transformations [1].
Description: