Thursday 4 April 2013

Overview the website and where to start.


Welcome you to GdxEngine.blogspot.com, I hope you will receive many interests here.

About Libgdx framework

When i need a way to Develop games in mobile platforms like Android or iOS, i have seen the libgdx framework and i quickly find acquaintance with how it works. I think it similar to XNA Framework that i have enjoyed for a long time.

Libgdx framework is really useful because it is based on the study of game development cross-platform. That is your game when writing by Libgdx framework, it will be played on Desktop, Web, Android, iOS, and in the future can add other platform! When you code the game use Libgdx framework, you can develop game and debug code on the desktop, after the game completed, you can completely take the game to run on different platforms without or very least the need for modifing source. Libgdx framework optimize your game by design essential elements such as resource management is written in native code. Libgdx framework ’s documentation is great and their support forum quickly and enthusiastically. Libgdx framework has many game example, source code, and there are many commercial and non-commercial game has used Libgdx framework.

About Gdx Engine and website

This website contains  everything i Developed on libgdx. There is something i call it 'Gdx Engine': D. I have Developed the engine's structure nicely from it could be used in many game projects, varying in Genres and game logics. You can rest assured that the entire Gdx Engine are based on Libgdx framework completely, meaning that every features in Libgdx framework you can apply in Gdx Engine and even more easily.

Gdx Engine was born with the purpose of to help you manage your game source code as you write game use Libgdx framework. Of course you might not need to Gdx Engine but I believe the process of developing your game development process will be a lot easier if you use GDX Engine. Because [The Engine Architecture] of Gdx Engine is very flexible and efficient, and is suitable for both the project scale from small, medium to large commercial projects. Gdx Engine is really just born, so it was pretty much limited in terms of features: as the lack of nice effects, camera smoother or more unique shader. So Gdx Engine still is a long way to grow, so I would need more support from you! Any contribution of you are warmly welcome! Please see the [contribution].

This website will inform you about the GDX Engine, how it works and how to use the engine in your game projects. I tried to do the documentation in detail and easy to understand as possible. Including the architecture of the engine, the articles explains the essential components and step-by-step tutorials, tutorials about the features that Gdx Engine support through the game example which I have prepared. All source code for the GDX Engine, documents, and of the example games are free and you can download easily in the [Download and setup]. You're welcome to mail me on MrThanhVinh168@gmail.com or you can read the [online documents].

If you publish your game using my engine code, all you need to receive the usage’s rights is display the logo of engine, It can be laid on your splash screen. Your support will help me by promoting the engine very well.

Contribution

Because GDX Engine is developed in time is not long, so it's still a lot of shortcomings, your support will be an important source of motivation for the development of GDX Engine! I am currently looking for members to continue with me to develop this engine. If you have the following conditions, please do not hesitate to email me on the address MrThanhVinh168@gmail.com I always look forward to the contribution from the you!
+ General Requirements: Passion in the field of game development and open-source game engine
+ Engine Programmer: Improve the coding style of the game engine and create more engine features.
+ Graphic programmer: Fluent OpenGL ES Shading Language to create more effect for engine
+ Document Writer: Create your own games using GDX Engine and write tutorials about your games!
You can also support the for GDX Engine simple by tell your friends about this engine and generate backlinks about GDX homepage Engine. If you can create game use GDX Engine, please let me know via email, I am very happy to hear from you!

About author

Hi everyone have visited my website. My first name is Vinh and game making is one of my hobbies beside I work in. NET Framework, Java, Wordpress, Web design and I'm working for a out-sourcing software company in my country. Before i start learing Libgdx, I have researched for the XNA Framework and [here] is some my minor project. English is not my native language so my tutorials maybe have some Gramma errors, if you spot anything wrong, please report me by writing comments. Your comments will be highly appreciated. As you see my nickname is 'Akemi-san' I named that from my favorite anime (cartoon in Japan) If you interest this, you can check the film in [link]. When you need to contact me, you can call me Vinh or 'Akemi-san', both names are fine. You 're welcome to mail me on my email, Maybe i can not reply to all emails you sent to me at once but i will read all of them.

Here is some content in website, you should follow this order if you are the beginner in game developments.

The Engine Architecture

Introduce to GdxEngine, how it works and how to develop yours libgdx game using GDX Engine

Introduction Game Service

Introduction Game Service, Game service is vital of the engine. This post will inform you how to use game service of GDX Engine


Introduce to SpaceWar

Spacewar is a game 2D example that using GDX Engine. I will inform you some important 2D graphic features of engine by the code of the example game.

Introduce to SpaceWar3D

Spacewar3D is a 3D version of spacewar example game. I will inform you some important 3D graphic features of gdx engine using in the game.

I list some engine packages belows:

Introduce to Gdx Engine Packages


This tutorial will describe lightly about packages of Gdx Engine.

You can see almost base classes and interfaces in here. Most of the engine are abstract classes and interfaces. With an abstract class, we often have the prefix "Base" before the class name to let you know it is an abstract class. To the interface, we will have a prefix of 'I' is an interface signal. Also engine has built standard class inheritance and implementation from an abstract class and interface, we usually have prefix 'Default' before the name to the class. You can create a new class by inheriting from the 'Default' class (the fastest way) or from the abstract class and implements other interfaces, if necessary optimize the class.
  1. Com.gdxengine.camera

Contains the camera is built exclusively for the game 3D. Camera should be added to the CameraManager Object to use
  1. Com.gdxengine.effect

contains the effect that GDX Engine was built. Particle Effect Manager used to create particle effects only for 2D games. Addition class ShaderManager will manage shaders, to render 3D objects, just for 3D gaming.
  1. Com.gdxengine.material

not built much. Material mainly used to store the values ​​of parameters passed to the shader. These are the parameters used for the texture when rendering objects. These parameters may be the model textures, TextureSampler, NormalTexture ...
  1. Com.gdxengine.light

No built many. Light mainly used to store the values ​​of parameters passed to the shader. These are the parameters used for the light to render objects. These parameters can be type LightColor, LightDirection, LightPosition ...
  1. Com.gdxengine.object3d

Contains the standard extension classes based on the parent class in the package framework or framework.Interfaces. You can rely on the classes in this package to build 3D objects for your own 3D games
  1. Com.gdxengine.ui

Contains the class supports the creation of user interface components in the game. I have built the class to create a custom menu. Other components such as dialog, input text, checkbox, ... will continue to be added in the update of GDX engine.
  1. Com.gdxengine.test. *

Contains classes show some individual features of GDX Engine or contains sub package contains the source code of the example game is available that GDX Engine has provided.

Download and install the engine


Download and Install guide

Updated version 1.2
Download full sourcecode and documment

I uploaded full projects from eclipse so you can import the projects directly.
If projects miss some library like android sdk or libgdx, please get them from link i posted below.
Contact me if you have any problem.

======================================
You can read the document online here or download the source code and documents to study offline [here]. Gdx Engine is absolutely free for download, amendment or making your games.After you extract the downloaded archive, you will have the document in pdf and folders is Asset, Testclasses and SourceCode.

The document for online reading, you can see here: http://gdxengine.site50.net/document.html

Install guide
Step 1: Download the source of engine to your computer.
Step 2: Install following software:
Java JDK
Eclipse
Android SDK for android, Google Web Toolkit for Web, MonoTouch for iOS (Not required entirely)
+ Download libgdx framework, extract it, You will have libgdx library, and the tool setup libgdx project: Gdx-setup-ui.
Step 3: Create new projects using Gdx-setup-ui tool like below figure:



Import projects just created into eclipse like below figure:



The following dialog will be shown like below figure:



The following dialog will be shown like below figure:



You copy everything in SourceCode và Asset folder from downloaded archive into src và asset folder in eclipse like below figure:



You select the "test..." class in back-end project to check if engine is working. Press F11 to run the Test class.



Game have run properly, congratulation !


Basic Game tutorials: SpaceWar 3D


In the the the tutorials, I will show you how to make the 3D version of game spacewar. I presume you've done [ Introduce to game SpaceWar ]. In the 3D version of spacewar, we will replace the meteor (capital only move randomly) into the enemy that is the UFO aliens, They can move, change direction and firing toward the player. Most of the resources in the game we will by this time taken from the game Droid Invader 3D of libgdx, you see [here]. A spectacular explosion will take effect to describes the collisions of objects in the game.
Principles of Game scene, Collection item and object collection, asset and the Background in similar spacewar3D in the the The coal spacewar so we will not repeat more.

Game Optimization

With 3D Game, the optimization is extremely IMPORTANT because so much of the equipment, especially portable devices is limited the 3D graphics processing Capabilities. So if you want your game to be played on as many devices as possible, then you must optimize for the game.

If you use game engine to make the game, the positive is really you develop game very fast but together with that, if you have not used properly engine will result in a waste of memory and CPU load increases. Of course GDX Engine is no exception.
 Class DefaultObject3D of GDX Engine was built very Carefully. It allows you to render Model, support translate, rotate or scale the object in the three axis x, y, z and has full support for programmable shaders simulate light shining on the object. But Because it contains many features so i can not call this class is optimized for gaming. Therefore you should refrain from creating new objects from this class. Instead, with the large number of objects, such as bullets and enemies,

I recommend using CollectionItem andObjectCollection to optimize the game instead of just
using DefaultObject3D and add the object to the scene directly. , However, DefaultObject3D could be used easily to create the model of the player, because the player needs more features that really is supported in DefaultObject3D. On the other hand you usually only need a single player in the game. Thus wasting memory and CPU load is negligible.

Make Bullet and BulletCollection

purposes [ Game Optimization ], we're going to make bullet inherited from collection items and make BulletCollection inherit from the the object collection.
In Bullet, only Those attributes needed for each new separate bullet is declared. Resource for the bullet as Model or Shader'll Just be declared or called in BulletCollection to limit the waste of RAM.
Public class Bullet extends CollectionItem {

       public Sphere sphere;
       public final Vector3 position = new Vector3 ();

       public Bullet (Vector3 position, boolean isPlayerBullet) {
              this.position.set (position);
              this.isBulletOfPlayer = isPlayerBullet;
              sphere = new Sphere (position, 0.2f);
       }

sphere variable used to Simulate a sphere that cover globally the bullet in the 3D space. The sphere objects will help us check collisions in the 3D space by using the overlaps () method of the class. The technique here is the same as in the Rectangle class.
Next, declare class BulletCollection too simple:
public class BulletCollection extends Object3DCollection <Bullet> implements IService {
       public BulletCollection (IGame3DService services) {
       super.(services);

You see BulletCollection has overridden methods to render as renderGL1 () and renderGL2 (). In these methods, the entire bullets will be rendered on the screen:
@Override
       public void renderGL2(float gameTime)
       {
              //set color shader is active to render bullets
              shader.setActiveShader("color");
              //call begin() method here to reduce number of the method calling that lead to better performance
              shader.begin();
              //render all bullets if the bullet is not dead
              for(Bullet obj : getObjectCollection())
                     if(obj.isVisible())
                     {
                           if(obj.isBulletOfPlayer)
                                  shader.setUniformf("u_color", 1f, 1f, 0.5f, 1f);
                           else
                                  shader.setUniformf("u_color", 0.5f, 1f, 1f, 1f);
                           //set transformation matrix for shader
                           transform.set(camera.getCombined());
                           transform.translate(obj.position.x, obj.position.y, obj.position.z);
                           shader.setUniformMatrix("u_projView"transform);
                           Asset.bulletModel.render(shader.getShader());
                     }
              shader.end();
              //set textLight shader is active to render player and invaders later
              shader.setActiveShader("textLight");
       }
As you can see, to render all the bullets, you just call the begin () method of the shader only once. Limit number of call begin() method of shader will bring your game to a better performance.

Make the Enemy and EnemyCollection

to optimize game, the enemy can be built is similar bullet. But here i intend to show you how to use the DefaultObject3D class and DefaultObject3DCollection class, We will apply two class to build the Enemy and EnemyCollection class.
Example: public class Enemy extends DefaultObject3D {/ / ... }
Enemy, you can even completely customize the rendering stage model, simply you need to override method renderGL1 () for OpenGL ES 1.x or method renderGL2 () - OpenGL ES 2.0 for the class DefaultObject3D and get rid of unnecessary handling from parent class.
Example: Reduce render processing of Enemy overriden the process of rendering the DefaultObject3D.     

@Override
       public void renderGL2(float gameTime)
       {
              texture.bind();
              shader.begin();
              setLightParameters();
              normal.idt();
              normal.rotate(0, 1, 0, rotate.y);
              normal3.set(normal.toNormalMatrix());
              shader.setUniformMatrix("u_normal"normal3);
              transform.set(camera.getCombined());
              transform.translate(position.xposition.yposition.z);
              transform.rotate(0, 1, 0, rotate.y);
              shader.setUniformMatrix("u_projView"transform);
              model.render(shader.getShader());
              shader.end();
       }
objects DefaultObject3D also has its own collection class, that DefaultObject3DCollection. Simply after create new Default object 3D, you add the object to the instance of DefaultObject3DCollection instead add to scene directly. Of course then you still have to add the DefaultObject3DCollection to the game scene for make this collection can auto-update, auto-render ...
But in spacewar3D game, the Enemy will be added directly into Game scene, so you don’t see the class EnemyCollection.

Using Light Manager in GDX 3D game engine.

I can assure you that the light is always Indispensable when you develop 3D game. Because if there are no lights, then, do you distinguish a 3D space? You can not see the bright and dark regions on the object, the object is no drop shadow on the terrain or the object itself, the worse is the whole 3D space can only be a dark color. A 3D game no lights is not different from a 2D game.
When you use LightManager, you need ambientLight parameter, there is a small color value, is added to the final synthesis of color on each pixel in the Fragment Processor of the shader. If you have knowledge of [OpenGL ES shader language], you can see the file in light-tex-fs.glsl in the asset / data / shader folder ambientLight used to PREVENT your 3D Scene too dark Because somewhere on scene lights may not work.
Example: LightManager lightManager = new LightManager(new Vector3 (0.1f, 0.1f, 0.1f));
One problem is that how much light in a 3D space? There are a lot and how you can manage all of the lights, took out the light you need and remove the ones you do not need them? Meanwhile, the role of LightManager will be shown. You can create a new light, add light and LightManager and get light also from LightManager. LightManager is a 3D Services over it is always present in any 3D object. LightManager solve the problem of having a variety of types of lights in Scene. Light maybe Direction Light, PointLight, Spotlight ... but all lights always using the same parent class, BaseLight class, you can cast BaseLight to Special Light class you need with method get (key, Class) of the LightManager.
Example: cast a light to DirectionLight directlight = light.get("MainLight",DirectionLight.classes) ;// light is LightManager
Of course, To make get a light possible, you had to add the light before:
this.light.clear (); / / light is
   
when you get the light, you can use light to transmit values ​​for the shader ‘s light parameters by overriding method setLightParameters () or pass a default object ISetLight to the 3D object.
@Override
       public void setLightParameters() {
             
              shader.setUniformVector("u_light_ambient_color"light.ambientColor);
              shader.setUniformVector("u_light_color"directlight.getLightColor());
              shader.setUniformVector("u_light"directlight.getLightDirection());
       }
spacewar3D, both Player and Enemy needs to light, and set values ​​for lightParameters. So neat to program, we create class SpaceWarGameObject3D from the DefaultObject3D and override method setLightParameters () of DefaultObject3D in the class SpaceWarGameObject3D.
When Player spacewar3D the take damage, mainLight will turn red for 0.5 seconds. , This is very easily done by set the color in reference variable mainLight in the Player class. mainLight direct reference direction light in the game so it will influence the light that shine on the whole objects of the enemy and the player object.
However, handle light with shaders in game3D always is hard work for the GPU. Especially when the game has many different light sources. Thus let ’s reduce the amount of lights you must use at a minimum. Also you can throw away the light feature for the object is not so Important, for example the bullets are lighted unnecessary, you only need to render the model of the bullet up and Determine to give it a color. Shader is only supported in OpenGL ES 2.0, If the device does not support OpenGL version, to create light, you must use the method that OpenGL ES 1.x has built for you. You can not make the option by shader.

Use the ShaderManager Control different render process of objectes

Above the mentioned bullet I will render without lighting, mà Means the process of rendering of the bullet will be different from the Player or Enemy so bullets will use different shader to render. Sometime in the 3D games have usage of different shaders to render different object groups!
ShaderManager used to manage all the shader in your game. similar lightManager, you also add shader, get shader, set active a shader, set the value for shader parameters, call the method begin () of the shader ...
Example: Render Bullets in BulletCollection use a different shader rendering Player or Enemy
       @Override
       public void renderGL2(float gameTime)
       {
              //set color shader is active to render bullets
              //shader is instance of ShaderManager
              shader.setActiveShader("color");
              //call begin() method here to reduce number of the method calling that lead to better performance
              shader.begin();
              //render all bullets if the bullet is not dead
              for(Bullet obj : getObjectCollection())
                     if(obj.isVisible())
                     {
                           if(obj.isBulletOfPlayer)
                                  shader.setUniformf("u_color", 1f, 1f, 0.5f, 1f);
                           else
                                  shader.setUniformf("u_color", 0.5f, 1f, 1f, 1f);
                           //set transformation matrix for shader
                           transform.set(camera.getCombined());
                           transform.translate(obj.position.x, obj.position.y, obj.position.z);
                           shader.setUniformMatrix("u_projView"transform);
                           Asset.bulletModel.render(shader.getShader());
                     }
              shader.end();
              //set textLight shader is default active shader to render player and invaders later
              shader.setActiveShader("textLight");
       }

Here, to render all bullets, we have used shader "color", specified in method setActiveShader(). then you need to call begin () method of shader. You transmit values for shader parameters. then you render the model of bullet by method getShader () that will retrieve the current active shader. The last, you call method end() of the shader and set active shaders is "textLight”- It is default shader used when you render the player or enemy in the game scene.

Using CameraManager control many Cameras in the game

Like light, I can assure you that the camera is always Indispensable when you develop 3D game. Cameras like the "eyes" of the players, observed every object in your 3D space. Of course a 3D game will have at least one or more cameras. example of a role-playing game, you can have ThirdPersonShooter (TPS) Camera that follows Player, or FixedCamera to the observer a fixed point in the game ... Camera Manager will manage all in-game cameras. Camera is very necessary because there is a critical parameter when you render the 3D object, which is combined Matrix. This Matrix is combination of the Matrix View and Projection Matrix of a camera3D. Combined Matrix used as the genesis of the 3D transformation matrix of the object, and the shader using the transformation matrix to render objects:
For example:           //set combined matrix for the transformation matrix from CameraManager
                     transform.set(camera.getCombined ());
/ / Do more stuffs for transformation matrix like rotate, scale, ...
/ / set the transformation matrix for shader
shader. setUniformMatrix("u_projView", transform);

You can add the camera, get the camera and set the active camera using setActive...() method like the shader manager and light manager. Method getCombined () Camera Manager is get the Combined matrix of the active camera. You could refer source code of the engine for more information.
cameras need to be updated constantly, often a camera will be using the update () method by default, but these special cases, you will have to write using the update () method. example in spacewar3D , we use TPS Camera that follow player object, so we will need to update the TPS camera in the Player class
Example: activeCamera.Update (rotate.y, up, position, gameTime); / / activeCamera is TPS Camera
In BaseGameScene3D class, method update () method of the camera is not called, so you need to call update () to the camera in the sub-class of BaseGameScene3D to update camera.

Render an animated 2D texture on 3D

When there is a collision between objects in spacewar3D , usually there will be an explosion to illustrate the collision. During game2D spacewar, we use the particle effect, but the current particle effect can only be used for 2D games. Besides particle effects are consuming so much resources and make lower performance. Consequently we can use animated texture to represent an explosion occurred, as shown below:
 If you already have the basic concepts of 3D graphics programming , you will easily understand rendering techniques. 2D image here above has been split out and draw a square up from the pairs of vertices, is what has created in variable Mesh of game asset. 4 vertices will pair up to render a square contained texture from file explode.png
example: square Render:
Asset.explosionMesh.render(shader.getShader (), GL10.GL_TRIANGLE_FAN,
               (int)(obj.aliveTime / Explosion.EXPLOSION_LIVE_TIME * 15) * 4, 4);

The code to render explosions located in the ExplosionCollection.java file
Example: Render all explosion
      Gdx.gl.glEnable(GL10.GL_BLEND); //Enable Alpha blend to support transparent
       Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
              Asset.explosionTexture.bind();
              shader.setActiveShader("text");
              shader.begin();
              shader.setUniformi("u_diffuse", 0);
              transform .set(camera.getCombined());
              for(Explosion obj : objectCollection)
                     if(obj.isVisible())
                     {
                          
                      transform.translate(obj.position.x, obj.position.y, obj.position.z);
                      shader.setUniformMatrix("u_projView"transform);
                      Asset.explosionMesh.render(shader.getShader(), GL10.GL_TRIANGLE_FAN,(int)(obj.aliveTime / Explosion.EXPLOSION_LIVE_TIME * 15) * 4, 4);
                     }
             
              shader.end();
              Gdx.gl.glDisable(GL10.GL_BLEND);
              shader.setActiveShader("textLight");
As you can see in this code using the "text" shader to render using texture. Unlike the "color" shader which used to render bullet, the “text” shader supports texture-based render while “color” shader only supports color-based render. After the render is done, you set active shaders is "textLight” shader - It is default shader which used when you render the player or enemy in the game scene.