Wednesday, 30 January 2013

#4.2 Tutorial (LibGDX & MTX) Test_08_SmartModels

This is where thing gets exiting. SmartModel is designed for and easy life for LibGDX Scene2D and Live Wallpaper developers. Lets talk about it.


Some of the codes are too long, so I separated this test to 2 classes (Test_08_SmartModels will be out UI and Test_08_SmartModelsHelpers will be our manager)


SmartModel class simply extends AbstractActor, it adds lots of smart actions to our actor.Each smart action has ability of make their own decision once they completed its action, and a randomizer helps us to make things natural as it can be. Don't worry, everything happens in the background, so only thing you need to do is identify your action and give parameters you like to increase variation. Smart actions simply are LibGDX Scene2D Actions which brought together to create effective effects.

For example
  • Move actor around the screen freely (Example: background balloons)
  • Move actor side to side (Clouds:)
  • Move actor to specific direction from one point in a paradox (Example: Snow flakes)
  • Rotate actor randomly (Snow flakes rotates)
  • Scale actor randomly (Snow flakes scales to give 3D feeling)
  • Fade in/out randomly (Snow flakes vanishes to give more natural feeling)


I will not go through each smart action, everything explained in codes, furthermore you can check the codes of SmartAction to see how things are working in the back-end if you want.

First things first

  public ArrayList<SmartModel> smartModelList;

I created an array of smart model (each will present a single snow flake, or metal balloon in our example) in my "Test_08_SmartModels" class, and then I pass this list to "Test_08_SmartModelsHelper" class to create the effect/

I will focus on our Snow Flake example

*60 SnowFlakes will be created (NOTE: it may say 20 bats in your code, i forget the fix that comment)

This is just for cleaning stage and smartModelList.

int rndSizeWidthHeight = rnd.nextInt(50) + 10;
SmartModel currentSmartModel = new SmartModel(rndSizeWidthHeight, rndSizeWidthHeight, rnd, true);
I want my snow flakes in different sizes and also DIPActive is true for auto-resizing for each different resolution

int[] topRangeX = { 0, (int) AppSettings.WORLD_WIDTH };
int[] bottomRangeX = { 0, (int) AppSettings.WORLD_WIDTH };
currentSmartModel.startActionMoveToDirection(topRangeX,bottomRangeX, (int) AppSettings.WORLD_HEIGHT + 200, -200,9, 3, true, true);
Our first smart action, this will move snow flakes in a forever loop

  • TopRangeX, a range of X-axis for start position of a snow flake, each time randomly number will be choosen in this range
  • BottomRangeX, a range of X-axis for destination of a snow flake, each time randomly number will be choosen in this range
  • (int) AppSettings.WORLD_HEIGHT + 200 is for Y-axis start position
  • -200 is for Y-axis end position, this is where the action ends and repeating itself
  • 9 is seeped (Duration), 3 is minimum speed (Duration), so it will take a random number between 9 seconds and 3 seconds for a snow flake to reach its destination point
  • isTopDown true, so it will snow top down, in my metal balloon example this is false, so metal balloons moves from down to up like floating bubbles
  • last parameter is for randomizing rangeX, to use or not.

currentSmartModel.startActionScale(10, 2f, 2f, 3, true);
currentSmartModel.startActionRotate(5, 360, 2, true);
currentSmartModel.startActionFadeInOut(10, 3, true); 

The rest of actions are for rotating, scaling and fade in/out, just read method description or play around to see different results

Things may not be clear at first, but it will make sense in time, and you will be using these single line commands for creating many type of effects for your games and live wallpapers.


  1. hi, i love your tutorials and your libgdx cool library. can you plz make some Box2D tutorial.

    1. Thanks, I follow a pattern in my tutorials (fundementals > tools > fun things > game structure > box2d)

      So, box2d in my plans, I am still merging scene2d and box2d together for very easy implementations, I will publish more tutorials soon.

    2. that's so cool , i have wrote a comment in earlier tutorial asking for box2d and now i find this , so before you integrate box2d into mtx , can i just go ahead and use box2d with mtx , with actors , stage ... ?