Sunday, December 15, 2013

Importing Mesh and Animation from 3D Studio Max to Unity3D

In past some post I tried to show how we can perform 2D animation and collision detection.

In this post I am will show I we can import 3D mesh with animation created from 3D studio max to Unity3D and animate the same.

So lets start, I created a sample mesh with animation with 3D Studio max.


We need to export it using FBX file format, which we can easily import in Unity3D.


To import it, you can drop the created FXB format file to Asset folder in Unity3D. Once you import it click imported mesh to display its property in Inspector window. In Inspector window click on Rig tab and set animation type to Legacy.

This is required else animation will not play and you will get error "Animation needs to be marked as Legacy"




Now we can use this mesh and perform animation, you can also change its property like play automatically or loop animation.


Now lets add a script, which make model jump when pressing some button.


And here is script.
using UnityEngine;
using System.Collections;

public class JumpAnimation : MonoBehaviour {

 // Update is called once per frame
 void Update () {
  if(  Input.anyKeyDown ) {
   animation.Play("Jump");
  }
 }
}

So now its ready,if you press any button now, it should jump. Here is video how it looks.

 

Saturday, December 7, 2013

SteelSeries controller Controller Un-Boxing

I also received SteelSeries controller along with MogaPro gontroller, from BlackBerry Gamepad offer. You can find MogaPro controller's unboxing here.

Following are few unboxing pics from SteelSeries controller.

Box in which it came.


Back side of box.

Front side of controller.

Came with a carry pouch and micro USB cable..

Looks quite small in my hand.

Close up of front side.

Only two button at backside.

Micro USB port.

MogaPro gamepad Un-Boxing

So I received my MogaPro gamepad tanks to BlackBerry GamePad promotion event. Here is un-boxing pictures for the same.

Box in which it came.

It came with micro USB cable to charge controller and a nice stand to place tablets.

The back side of controller.

Micro USB port to charge controller.

Front side.

Power on button and holder for Phones.

Sunday, December 1, 2013

BlackBerry gamepad offer

As you might know BB10 support gamepad, that mean you can connect gamepad to Device via Bluetooth and play games using it. And to promote gamepad enabled game, Blackberry also announced an offer. You can get more details about offer here.



For the same offer I got mine GamePad recently. I also enabled my CrazyFlight game to support GamePad. Here is demo.

Exposing C++ ENUM to QML

I was working on update of one of my game and I while re-factoring I was required to expose C++ Enum to QML code.

Following is how header looks like with definition of Enum GamePadButton. I am registering Enum with Qt's Metaobject system using Q_ENUMS macro.
class GamePadObserver: public QObject {
 Q_OBJECT
 Q_ENUMS(GamePadButton)
public:

 enum GamePadButton{
   A_BUTTON=0,
   B_BUTTON,
   C_BUTTON,
   X_BUTTON,
   Y_BUTTON,
   Z_BUTTON,
                 ...
   NO_BUTTON
 };

public:
 GamePadObserver(QObject* parent = 0);
 virtual ~GamePadObserver();
        ...
};
Now to be able to see this Enum to QML code we need to register GamePadObserver class to Qt Metaobject system.If you want to be able to create instance of class then you can use qmlRegisterType() macro.

In my case I dont want to create instace of GamePadObserver in QML, I just want to expose it enum to QML and for that purpose we can use qmlRegisterUncreatableType macro. Its useful for exposing enum and attached property. Following how we can use this macro.
#include 

int main(int argc, char **argv)
{
 qmlRegisterUncreatableType("GamePadObserver", 1, 0,"GamePadObserver", "");
        ...
}
Now we can use Enum from QML, following how we can do that.
...
import GamePadObserver 1.0
...

Rectangle {
    id:main
    ...

    Connections{
        target: GamePad
        onButtonPressed: {
  
            if( button == GamePadObserver.X_BUTTON
            || button == GamePadObserver.Y_BUTTON 
            || button == GamePadObserver.A_BUTTON 
            || button == GamePadObserver.B_BUTTON ) {
                ...
            } 
        }        
    }

}

Saturday, November 23, 2013

Using NSOperationQueue in iOS SDK

Sometimes we need to perform some task in different thread or in background. NSOperationQueue is quite handy for such purpose in iOS.

You can easily add operation you need to perform in queue just like any other object and NSOperationQueue queue will take care of its execution. You can also add operation in NSOperationQueue by specifying code block, and in many situations that's quite useful.

 Following code sample shows how we can create NSOperationQueue.
- (id)init
{
    if ((self = [super init]))
    {
        _operationQueue = [[NSOperationQueue alloc] init];
        //[_operationQueue setMaxConcurrentOperationCount:1];
    }
    return self;
}

- (void)dealloc
{
    [_operationQueue release];
    [super dealloc];
}

Below code shows how we can add operation to NSOperationQueue by code block.
- (void)requestFinished:(ASIHTTPRequest *)request {
        
    [_operationQueue addOperationWithBlock:^{
        
        //perform some useful task
        
    }];
}
   -(void) processEntries:(NSArray*) entries
   {
   }

   [_operationQueue addOperationWithBlock:^{
     [self processEntries:entries];
   }];

Saturday, November 9, 2013

Collision detection with Unity3D

So by now I know how to do animation and object creation in Unity3D. Now I started to learn how to get collision detection working with Unity3D.

In Unity3D collision is detected by using various collider objects. Box collider should work for most 2D games. There are some other shapes collider also available, but that I am not going to discuss here.

So to start, I created a basic setup, one egg and one cube which will act as wall and all I want it notification when egg collides the wall.



Now we have scene setup, so for egg to be able to detect collision, go to Box Collider in inspector, and select Is Trigger option. By selecting Is Trigger, egg can move through wall but still gives us notification of collision. In this post I am going to work with Trigger only.



For collision detection to work in Unity3D, atlest one of object needs to be rigid body. so lets add rigid body to egg.



Now we are ready, lets add script which makes egg move. script looks like below.
using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
 
 private int frame;
 
 // Use this for initialization
 void Start () {
  frame = 0;
 }
 
 // Update is called once per frame
 void Update () {
 
  //skipping some frame to make animation look smooth
  if( Time.frameCount % 10 != 0 ) {
   return;
  }
  
  //makeing it move
  transform.position += new Vector3((-5.0f * Time.deltaTime), 0.0f, 0.0f);
  
  //sprite animation
  frame++;
  if( frame > 8 ) {
   frame = 0;
  }
  renderer.material.mainTextureOffset = new Vector2(frame*0.125f,0);
 }
 
}
Now if you run the game, you can see egg moving but to be able to detect collision we need to add following functions to the script.
 
        void OnTriggerEnter(Collider other)
 { 
  Debug.Log("Collision enterd:" + other.gameObject.name );
 }
  
 void OnTriggerExit(Collider other)
 {
  Debug.Log("Collision exited:" + other.gameObject.name );
 }

Now if you run the game, you can see logs printed when egg pass through wall. Here is how it works.

We can also detect collision with empty game object as well.Just add empty game object to scene and add Box Collider to it. That's It should work just the same.

 

Thursday, October 31, 2013

Dynamic object creation in Unity3D

While creating game, we usually need to create and destroy object dynamically. Well with Unity3D its easy to create and destroy object dynamically but we need to perform few step carefully before we are able to do so.

First to be able to create a object dynamically we need to create a proper object with material applied over it.

Lets create a object which we need to create dynamically. I want to create an egg when I press some button, lets start with that.

- import image for egg sprite


- create materiel from egg sprite



- create a object with egg material applied over it





Now we have object that we need to create dynamically. But to create object dynamically we need to create a Prefab and apply object to that prefab.

 - Create a Prefab




 - Apply object to prefab



 Now we have prefab so we don't need that object any more, we can delete it so its not visible in scene anymore.

We are now ready to create object, but we need creation script. We can create a empty game object and attach creation script to it. But in this post, I am going to create a button object ( a Chicken), and when I click that button object I will create and destroy the prefab.

So let's create a script and attach it to button object.




Following is script, which create and destroy object. We need to make eggPreFab variable a Public variable so its visible outside and we can assign prefab from Unity3D interface.

using UnityEngine;
using System.Collections;

public class creation : MonoBehaviour {

 // Use this for initialization
 public GameObject eggPreFab;
 Object eggInstace = null;
 
 void Start () { 
 }
 
 // Update is called once per frame
 void Update () {  
  if( Input.anyKeyDown ){
   if( eggInstace == null )
    eggInstace = Instantiate(eggPreFab);
   else 
    Destroy(eggInstace);
  }
 }
}

Now we are almost done, but we need to assign prefab object to eggPreFab variable. To do so click button object, on script section you will see eggPreFab varible. Drop our egg prefab from asset section on this variable.



We are done, try running game. You should be able to create and destroy object now.


Sunday, October 20, 2013

Beginning 2D sprite animation with Unity3D

Now that Unity is free and you can develop app for Windows phone, BB10, Android and iOS using it. I thought to give it a try.

To learn it, I started with creating simple 2D game, in which I need to switch images on object when someone touches it. It too some time to get it done first time, but finally now I know how it can be done.

So let's get started.

First we need to setup Main camera.

- I seen some videos and in one of video, author suggested to set camera at x=0,y-0, z = -10
and rotation to x=0, y=180, z=0.  I decided to use the same.

- Set camera size as 1 as its easier to deal with

- Set projection to Orthographic for 2D games.

Setup light for 2D game

We dont need to use lighting for 2D game, so we will light up whole scene by following.

Edit-> Render setting ->Ambient Light



Change Ambient light to White, It should light up whole scene

Create a cube to represent 2D object

Now we should add some object to scene.

Actually I want to render 2D image on scene, but that's not possible so we will add some 3D object and apply material over it (which is our image). And render that object to represent our image.

To do that we can add cube to scene, with proper size, Ideally we just need a plane which only one face to represent image, as that will same memory, but to learn how it works, cube will do.

Add cube to scene by Create in Hierarchy and then cube.


set position as x = 0, y = 0, z = 10

You should see a Cube in your view now.

Import image as asset

Now drag image which we need to draw to asset window,
If you want to draw animation than Image needs to be sprite sheet, I created one myself with all images i needed to be displayed.

Create material with image and apply on the Cube

We can not add image diractly to Cube, we need to create material first.
To create material,

 Project window, create ->material


Now drag image over material's texture window.



Now you can apply this material to cube, by dragging material and dropping it on cube.



Now you can see the image, but you see whole sprite sheet.  We don't want that, we need to show only one portion of sprite sheet.



Let's click material again and you will see it has tiling and offset property.

So our sprite sheet has two sprite, thus tiling will be 0.5. Let's set tiling to 0.5. Now we see proper image on cube.



If you play with offset, you can see by manipulating it we can show different sprite on cube.

By setting offset to 0.5 you can show different image on cube. We will be using this property when we add support for event, on which we will show different image.



Its showing proper image but its also showing white background, but my image is transparent. We can add transparency by selecting proper Shader.

Click material, from Shader select -> Transparent ->cutout->Diffuse



You can play with Alpha cutoff value to adjust transparency. Now it should look like below.



Handle touch event so we can animate image on touch

Now we have our 2D object, we wan to add event on it. For that we need to add script to it.

Click on object -> from Inspector window -> select Add Component -> New Script

Name it and select scripting language of your choice, I selecte c# as scripting language.

Open script, and add following code in Update method.

 void Update () {  
  if( Input.anyKey ){
   gameObject.renderer.material.mainTextureOffset = new Vector2(0.5f,0); 
  } else {
   gameObject.renderer.material.mainTextureOffset = new Vector2(0.0f,0);
  }
 }

This script will check if any key is pressed. If pressed we are setting material offset to 0.5 else 0. To display different part of sprite sheet.

OK, We are done now. Here is how it works.


Saturday, September 28, 2013

Dealing with device for Ubuntu Touch development

Lately I am working on Ubuntu Touch Calendar application. Recently I also got device and now I wanted to run and test application on real device.

So it seems there are two type of Ubuntu Touch image,
one cdimage-touch, which is read-write image but it does not support over the air update and
other ubuntu-system, which is read only, but which can be updated over the air

For development purpose its better to use cdimage-touch, as you can transfer file from computer to device and test it as its read-write. Its not possible with ubuntu-system image. But if you just want to see how Ubuntu Touch looks and test applications then ubuntu-system image is good.

You can follow steps mentioned here, to install appropriate image on device.

Now we have proper image on device, then following are some command which can be used to install the application and transfer file form computer to device.

adb push can be used to tranfer file from computer to device, just like copy
adb push tests/autopilot/calendar_app/tests/test_calendar.py /usr/lib/python2.7/dist-packages/calendar_app/tests/
BTW, " /usr/lib/python2.7/dist-packages/" is the path where test case are installed by default on device. And above command is coping updated test case file to device.

If you want to run test on device then you can use following command on your desktop terminal.
phablet-test-run calendar_app
Same way we can transfer QML files as well. Like below.
adb push YearView.qml /usr/share/calendar-app/YearView.qml
It seems "/usr/share/" is path where applications are installed.

If you want to install complete application built from source code, then you can run following command to build application from source. You need to run this command from directory where debian folder is residing.
debuild -uc -us -b
This will generate .deb file in parent folder. You can copy this file to device via adb push. Then you can goto device's shell by adb shell command.
adb push DEB_FILENAME.deb /tmp
adb shell
And finally install .deb by running following command on device's shell.
cd /tmp
dpkg -i DEB_FILENAME.deb
Now you should be able to run latest installed app.

Sunday, September 22, 2013

Un-boxing Nexus 4

I recently received Nexus 4. Here are few snaps.


Box contains USB cable, power adapter in which you can plug in USB cord to charge the device and user manual.


Device is quite nice looking, but I am going to use it for testing Ubuntu Touch.


After firing some command from here, I was able to boot Ubuntu Touch on it.