Beginning Android Games

Mario Zechner

Mentioned 13

Beginning Android Games offers everything you need to join the ranks of successful Android game developers. You'll start with game design fundamentals and programming basics, and then progress towards creating your own basic game engine and playable games. This will give you everything you need to branch out and write your own Android games. The potential user base and the wide array of available high-performance devices makes Android an attractive target for aspiring game developers. Do you have an awesome idea for the next break-through mobile gaming title? Beginning Android Games will help you kick-start your project. The book will guide you through the process of making several example games for the Android platform, and involves a wide range of topics: The fundamentals of game development The Android platform basics to apply those fundamentals in the context of making a game The design of 2D and 3D games and their successful implementation on the Android platform For those looking to learn about Android tablet game app development or want Android 4 SDK specific coverage, check out Beginning Android 4 Games Development, now available from Apress. What you’ll learn How to set up and use the development tools for developing your first Android application The fundamentals of game programming in the context of the Android platform How to use the Android's APIs for graphics (Canvas, OpenGL ES 1.0/1.1), audio, and user input to reflect those fundamentals How to develop two 2D games from scratch, based on the Canvas API and OpenGL ES. How to create a full-featured 3D game How to publish your games, get crash reports, and support your users How to complete your own playable 2D OpenGL games Who this book is for This book is for people with a basic knowledge of Java who want to write games on the Android platform. It also offers information for experienced game developers about the pitfalls and peculiarities of the platform. Table of Contents Android, the New Kid on the Block First Steps with the Android SDK Game Development 101 Android for Game Developers An Android Game Development Framework Mr. Nom Invades Android OpenGL ES: A Gentle Introduction 2D Game Programming Tricks Super Jumper: A 2D OpenGL ES Game OpenGL ES: Going 3D 3D Programming Tricks Droid Invaders: the Grand Finale Publishing Your Game What’s Next?

More on Amazon.com

Mentioned in questions and answers.

i need to tilt a layout in android about 45 degree anticlockwise. i want to implement a game in android as stackopolis .is there any method to align tiles as in the game.I have the tile image,and now i use canvas to align this.but there i cannot get the click event and it is difficult to align tiles.is grid view is compatable.any new idea. i have these images with me.this is the main background

main background grid

tile

i want to align this tile in grid.and need click event as in the stackopolis

If you dont know much about making a game the read this book

http://www.amazon.com/dp/1430230428/?tag=stackoverfl08-20

It will teach you EVERYTHING you should need to know to do what you want. It also sets up OpenGL ES for you so your app will be nice and smooth

I really would suggest OpenGL, using the Canvas class will not only be slower but it is going to be limited as well

i am new in android field m searching n learning android from last 1 month nw m having a problum with android game designing.... for the android concept and application designing i find Android 3.0 Application Development Cookbook and http://developer.android.com very help full now m having a good knowledge of android application designing but as i want to design game so i want any book or site who explain me complete concept of game designing in android step by step.

I have Beginning Android Games by one of the guys behind libgdx. I found it really easy to work through. It covers 2d game programming pretty well and even gets into some of the basic open gl stuff.

It gave me enough knowledge to branch off and start a project of my own with confidence.

I am reading Beginning Android Games (Mario Zechner) at the moment.

While reading about 2D games with OpenGL ES 1.0 the author introduces the concept of the SpriteBatcher that takes for each sprite it shall render the coordinates and an angle. The SpriteBatcher then calculates the final coordinates of the sprite rectangle and puts that into a single big buffer.

In the render method the SpriteBatcher sets the state for all the sprites once (texture, blending, vertex buffer, texture coordinates buffer). All sprites use the same texture but not the same texture coordinates.

The advantages of this behavior are:

  • The rendering pipeline does not stall, since there are no state changes while rendering all the sprites.
  • There are less OpenGL calls. (= less JNI overhead)

But I see a major disadvantage:

  • For rotation the CPU has to calculate the sine and cosine and perform 16 multiplication for each sprite. As far as I know calculating sine and cosine is very expensive and slow.

But the SpriteBatcher approach is lots faster than using lots of glRotate/glTranslate for rendering the sprites one by one.

Finally my questions:

  • Why is it faster? Are OpenGL state changes really that expensive?
  • The GPU is optimized for vector multiplications and rotations, while the CPU is not. Why doesn't that matter?
  • Would one use a SpriteBatcher on a desktop with a dedicated GFX-card?
  • Is there a point where the SpriteBatcher becomes inefficient?

But I see a major disadvantage:

  • For rotation the CPU has to calculate the sine and cosine and perform 16 multiplication for each sprite. As far as I know calculating sine and cosine is very expensive and slow.

Actually sin and cos are quite fast, on modern architectures they take 1 clock cycle to execute, if the pipeline has not been stalled before. However if the each sprite is rotated individually and an ordinary frustum perspective projection is used, the author of this code doesn't know his linear algebra.

The whole task can be simplified a lot if one recalls, that the modelview matrix maps linear local/world coordinates map to eye space. The rotation is in the upper left 3×3 submatrix, the column forming the local base vectors. By taking the inverse of this submatrix you're given exactly those vectors you need as sprite base, to map planar into eye space. In case of only rotations (and scaling, maybe) applied, the inverse of the upper left 3×3 is the transpose; so by using the upper left 3×3 rows as the sprite base you get that effect without doing any trigonometry at all:

/* populates the currently bound VBO with sprite geometry */
void populate_sprites_VBO(std::vector<vec3> sprite_positions)
{
    GLfloat mv[16];
    GLfloat sprite_left[3];
    GLfloat sprite_up[3];

    glGetMatrixf(GL_MODELVIEW_MATRIX, mv);

    for(int i=0; i<3; i++) {
        sprite_left[i] = mv[i*4];
        sprite_up[i]   = mv[i*4 + 4];
    }

    std::vector<GLfloat> sprite_geom;
    for(std::vector<vec3>::iterator sprite=sprite_positions.begin(), end=sprite_positions.end();
        sprite != end;
        sprite++ ){
           sprite_geom.append(sprite->x + (-sprite_left[0] - sprite_up[0])*sprite->scale);
           sprite_geom.append(sprite->y + (-sprite_left[1] - sprite_up[1])*sprite->scale);
           sprite_geom.append(sprite->z + (-sprite_left[2] - sprite_up[2])*sprite->scale);

           sprite_geom.append(sprite->x + ( sprite_left[0] - sprite_up[0])*sprite->scale);
           sprite_geom.append(sprite->y + ( sprite_left[1] - sprite_up[1])*sprite->scale);
           sprite_geom.append(sprite->z + ( sprite_left[2] - sprite_up[2])*sprite->scale);


           sprite_geom.append(sprite->x + ( sprite_left[0] + sprite_up[0])*sprite->scale);
           sprite_geom.append(sprite->y + ( sprite_left[1] + sprite_up[1])*sprite->scale);
           sprite_geom.append(sprite->z + ( sprite_left[2] + sprite_up[2])*sprite->scale);


           sprite_geom.append(sprite->x + (-sprite_left[0] + sprite_up[0])*sprite->scale);
           sprite_geom.append(sprite->y + (-sprite_left[1] + sprite_up[1])*sprite->scale);
           sprite_geom.append(sprite->z + (-sprite_left[2] + sprite_up[2])*sprite->scale);
    }
    glBufferData(GL_ARRAY_BUFFER, 
                 sprite_positions.size() * sizeof(sprite_positions[0]), &sprite_positions[0], 
                 GL_DRAW_STREAM);
}    

If shaders are available, then instead of rebuilding the sprite data on CPU each frame, one could use the geometry shader or the vertex shader. A geometry shader would take a vector of position, scale, texture, etc. and emit the quads. Using a vertex shader you'd send a lot of [-1,1] quads, where each vertex would carry the center position of the sprite it belongs to as an additional vec3 attribute.


Finally my questions:

  • Why is it faster? Are OpenGL state changes really that expensive?

Some state changes are extremely expensive, you'll try to avoid those, wherever possible. Switching textures is very expensive, switching shaders is mildly expensive.

  • The GPU is optimized for vector multiplications and rotations, while the CPU is not. Why doesn't that matter?

This is not the difference between GPU and CPU. Where a GPU differs from a CPU is, that it performs the same sequence of operations on a huge chunk of records in parallel (each pixel of the framebuffer rendered to). A CPU on the other hand runs the program one record at a time.

But CPUs do vector operations just as well, if not even better than GPUs. Especially where precision matters CPUs are still preferred over GPUs. MMX, SSE and 3DNow! are vector math instruction sets.

  • Would one use a SpriteBatcher on a desktop with a dedicated GFX-card?

Probably not in this form, since today one has geometry and vertex shaders available, liberating the CPU for other things. But more importantly this saves bandwidth between CPU and GPU. Bandwidth is the tighter bottleneck, processing power is not the number one problem these days (of course one never has enough processing power).

  • Is there a point where the SpriteBatcher becomes inefficient?

Yes, namely the CPU → GPU transfer bottleneck. Today one uses geometry shaders and instancing to do this kind of thing, really fast.

Ok my question is simple: How do I start working with 2D graphics on android? Untill now,I found on the how to make almost anything on Android,however with graphics I see is harder.

On the android sdk website,I get some simple codes that don't really help me,and I also searched for a book,guess what,I didn't find one that actually shows you how to make a little game explaining me what does every line of code.

So: How do I start working with 2D graphics? I'm interested because I want to make some games.I would be very happy to find a tutorial for ex. that shows you how to make a little character that just jumps over a box.I know you need an engine and all that stuff,and I would actually want to learn how to make a little engine.

If you are interested in android games(2d), I recommend this book http://www.amazon.com/dp/1430230428/

Can anyone suggest me the best place to start with for learning Open GL ES .... some good tutorial site or some pdf .....

Getting started with OpenGL... in Android

Pro Android Games

Beginning Android Games

Also check Related Links regarding to your question, which are bottom right of your question.

Some people says on SurfaceView, others on View. Which one is the best for a simple android game that uses drawable resources for its entities, and it does NOT uses OpenGL

Can you help me making a touchable image? And when you place it to its correct place, a pop up message will come and displays text. My game is a body parts game that you will be placing the body parts to its corresponding places... Help me please, I will highly appreciate your responses, thanks a lot....

if (event.getAction() == MotionEvent.ACTION_MOVE) {



        if (droid.isTouched()) {

            droid.setX((int)event.getX());
            droid.setY((int)event.getY());
        }
            if (roid.isTouched()) {

                roid.setX((int)event.getX());
                roid.setY((int)event.getY());
        }

Without a doubt the best answer to this question is:

Buy and read this book: http://www.amazon.com/Beginning-Android-Games-Mario-Zechner/dp/1430230428

It was written by the author of LibGDX which is one of the most popular android game engines. The book teaches you everything you need to know to build a complete game.

I am still very new to coding and Stack Overflow, this is my first question. I have been coding random apps, and experimenting with Android for the last 3-4 months. I recently published my first app. Just a day counter, with a widget. Still pretty basic, but my intention has always been to create Android games. My question is where do I start to make an Android game? Should I make games using just the default libraries and API on Android Studio or should I just make a Java game on Eclipse then try to import it to Android. I am trying to start by doing a simple 2D game. Can someone please explain to me what options do I have, or point me in the right direction please. Thank you.

You can use either Android standard API like Canvas API or OpenGL ES A good book explaining both aspects is Mario Zechner's Beginning Android Games. You can find a good examples of using both API in different games.

Also you can use third party frameworks and engines. Good one is Cocos2d-x (it is c++ framework) it is well known open source cross platform game development framework with good community and a lot of examples and books over the internet.

Also you can check Unity (C# or JavaScript) it is proprietary game engine extremely easy to learn and use (you can make many features like sprite animation without any code). This engine have large community and books. There is also a ton of well written documentation. (There are two licenses personal and pro).

Another good is Unreal Engine (C++) it is similar to Unity but from other developer. This is also good engine for game development and free to download and use (you must pay 5% royalty to epic games only if you earned more than 3k on your game).

There is also libGdx framework for cross platform development (java).

You can also check marmalade(C++), Corona SDK (Lua), AndEngine (pure android game engine).

My own opinion you should check Unity or Cocos2d-x. These are two most popular engines for Android/iOS game development so you will easily find books, videos and tutorials and even video courses (1 2) how to work with these engines.

I want develop a very simple android game, the main idea is to throw a ball to some target, something like basketball/football.

It should be in 3d like the famous "Paper Toss" (https://market.android.com/details?id=com.bfs.papertoss&feature=search_result)

what engine I should use? I read about many engines but it seems that most of them for more complex things.

Or maybe it would be enoughto use OpenGl ??

There are a lot of game engines you could use. Two popular engines are:

AndEngine is simpler and probably easier to get started on. Libgdx allows you to produce cross platform games that can be run under Linux and Windows.

I would actually suggest you get the book Beginning Android Games . It was written by the guy who made Libgdx and its walks you through the process of building an implementing a simple engine from scratch.

I have an app which shows two activities at the same time;

Because my application using surfaceview but surfaceview never accept behind view.

  • FrameLayout
    • ImageView
    • SufaceView at that time i never seen ImageView by every way.

But I solve that problem by Using Two Activities.

  1. Execute Activity A (Activity A (Game Background and Character))
  2. Activity A intent B (Activity B (Game UI & Game Enemy))

finally I can see that I want.

My Question:

Usual Activity B is faster than Activity A because A is not focused, just showing

  1. Some devices (Galaxy S2 that I know) Activity B is slower than Activity A

Plz Give me a favor I'm falling in this problem for 1 month....... T.T

I have an app which shows two activities at the same time;

I'm not sure what you mean by this because Android only allows 1 activity to run a time. When you start Activity B from Activity A, A gets paused.

My Question:

Usual Activity B is faster than Activity A because A is not focused, just showing

Some devices (Galaxy S2 that I know) Activity B is slower than Activity A

Again I'm not sure what this means, but as it looks like you're making a game I'd suggest you read this blogpost: http://www.rbgrn.net/content/54-getting-started-android-game-development

Maybe buy this book: http://www.amazon.com/Beginning-Android-Games-Mario-Zechner/dp/1430230428/ref=sr_1_1?ie=UTF8&qid=1304580271&sr=8-1

and look at the development process/source for this game: http://www.rbgrn.net/content/215-light-racer-3d-development-journal

Is Jogl the best way to create an Android Game such as a 2d game?

I can't say anything about the 'best' way, but you can use

  • andengine,

  • Corona,

  • Maoi,

  • Unity3d,

  • OpenGL ES 1.0/1.1/2.0, or

  • Draw to a Canvas or View.

Beginning Android Games is probably the single best resource (especially chapters 3&4). You should also look at the SDK samples (snake, lunar lander). Practical Android 4 Games Development is... not good (the copyediting is particularly shameful), but it is at least devoted to game development with OpenGL ES.

I already know the basics of Java but where should you go to learn android app development. I want to learn how to make pro apps and one day make a 3D game. Where do/did you go to learn android development? And please don't say university.

You can start using the Oficial resources:

2 Great books from Pragmatic Bookshelf:

Other Books:

Google should be your best friend.

I want to develop a game for Android platform. Which will be the best method to develop a 3-D game ?

I know something in flash. Can I use it in Android ?

This may help as a resource: http://www.amazon.com/dp/1430230428/?tag=stackoverfl08-20. I'd probably both read this to gain an understanding of the code and code around using david99world's answer

I would advise heavily against using Flash with Android; Android will be dropping support after 4.0.X