Tuesday, 1 February 2011

Custom Translate Animation for Android is almost ready

The original translate animation of android does not give information of the animated object's current location therefore I started to my project and it is almost finished. You can find the source code and algorithms of this functionality at my previous posts. I found some bugs in these code lines and I have already fixed but I still test them. I will share the trustworthy ones in next days. This project delayed for a long time because I moved to London and I am busy with the job search recently. But I will pay much more attention on the project in next days. You can watch the video of the animation. Attention!! the real animation works way smoother then the video. My PC is not strong enough to record and to emulate at the same time. See you..

Wednesday, 5 January 2011

Defining the route path between two points in 2D coordination system and Java implementation

I currently develop a 2D ball game for Android OS therefore this computing is important for the game to define the ball’s route in the game. In Android OS, there is a TranslateAnimation class to fulfil the Translate operation (changing the X, Y coordinates of an instance of a View class) but TranslateAnimation class does not supply the information of the animated object’s current x, y coordinate state. In my game, the ball’s place in time is important to take action according to its location. Therefore, I developed my own Extended TranslateAnimation class.

***The Algorithm***

I divide the animation operation in to two parts. First part is path resolving between the destination and the source points and the second part is animating the instance of a View class (animated object) and displaying it on the screen graphically. This article explains the path resolving operation which’s algorithm is below.

The figure below shows the representation of the destination(X1, Y1) and the source (X0, Y0) points. Red line (hypotenuse) shows the full path between points, green line shows the base and blue line shows the leg of the right triangle. The aim is, from the point 0 to point 1 if the animated object moves long as “d” on the redline (hypotenuse), what will be the new location on 2D plane( the “x, y” value of the very top and left had side of the plane is “0, 0” and it increases to the bottom right hand side). If it was calculated this new location’s coordinates, it would be set to the animated object’s location and the object would move to there.

clip_image001

The step by step algorithm is defined below for “d” distance.

1. Get little hypotenuse length (hypo=d)

2. Calculate the length of Hypotenuse (RealHypo)

3. Find Tan α = Blue Line/ Green Line

4. Find the α angle (use ArcTan)

5. Calculate the Cos α and Sin α values

Until here, we know the Cos α and Sin α values so we can calculate the base and leg length of the small right triangle which’s hypotenuse is “d” and top point is X0, Y0. So we just need to find the coordinate of the other tip of “d” (X’, Y’).

6. Find the difference between X0 – X’ = d*Cos α

7. Find the difference between Y0 – Y’ = d*Sin α

8. Subtract/Add these differences to X0 and Y0 According to the equations in step 5-6.

9. hypo=hypo+d

X’ = X0 - d*Cos α

Y’ = Y0 - d*Sin α

That’s all. One important note is Subtract/Add option changes according to the movement of the animated object therefore the direction calculation should be computed in step 7 as well.

Code

Full Java Code (VectoralPathResolver) is added below.

VectoralPathResolver.java

   1: package aliolci.CrazyBall;
   2: /*
   3:  * By Ali Olcay SAHIN 
   4:  * www.aliolci.com
   5:  * aliolci@gmail.com
   6:  * Thank you for using it.
   7:  * */
   8: import java.util.ArrayList;
   9: import java.util.List;
  10:  
  11: import android.graphics.PointF;
  12:  
  13: public class VectoralPathResolver {
  14:     private float FromXDelta, ToXDelta, FromYDelta, ToYDelta;
  15:     private float d =(float) 0.1;//sensitivity
  16:     private float realHypo;
  17:     private float hypo;
  18:     private double TanAngle;
  19:     private double cosValue;
  20:     private double sinValue;
  21:     private byte signX;
  22:     private byte signY;
  23:     public VectoralPathResolver(float fromXDelta, float toXDelta,
  24:             float fromYDelta, float toYDelta) {
  25:         FromXDelta = fromXDelta;
  26:         ToXDelta = toXDelta; 
  27:         FromYDelta = fromYDelta;
  28:         ToYDelta = toYDelta;
  29:         
  30:         realHypo = (float) Math.sqrt(Math.pow(FromXDelta-ToXDelta, 2)+Math.pow(FromYDelta-ToYDelta, 2));
  31:         hypo =0;
  32:         TanAngle = Math.atan((ToYDelta-FromYDelta)/(ToXDelta-FromXDelta));
  33:         cosValue =Math.cos(TanAngle);
  34:         sinValue =Math.sin(TanAngle);
  35:         signX = (byte) (Integer.signum((int) (ToXDelta-FromXDelta))<0?-1:1);
  36:         signY = (byte) (Integer.signum((int) (ToYDelta-FromYDelta))<0?-1:1);
  37:     }
  38:     public List<PointF> ResolveReturnFullPath()
  39:     {
  40:         List<PointF> path= new ArrayList<PointF>();
  41:         PointF f;
  42:         do
  43:         {
  44:             f=this.Next();
  45:             if(f!=null)
  46:                 path.add(f);
  47:         }
  48:         while(f!=null);
  49:         
  50:         return path;
  51:     }
  52:     public void setSensitivity(float sensitivity)
  53:     {
  54:         d=sensitivity;
  55:     }
  56:     public PointF Next()
  57:     {
  58:         if(realHypo>hypo)
  59:         {
  60:             hypo+=d;
  61:             float Xnew = (float)(cosValue*hypo*signX)+FromXDelta;
  62:             float Ynew =(float)(sinValue*hypo*signY)+FromYDelta;
  63:             return new PointF(Xnew,Ynew);
  64:         }
  65:         else
  66:             return null;
  67:     }
  68:     public PointF Next(int HowMuchEachTime)
  69:     {
  70:         if(realHypo>hypo)
  71:         {
  72:             hypo+=d*HowMuchEachTime;
  73:             float Xnew = (float)(cosValue*hypo*signX)+FromXDelta;
  74:             float Ynew =(float)(sinValue*hypo*signY)+FromYDelta;
  75:             return new PointF(Xnew,Ynew);
  76:         }
  77:         else
  78:             return null;
  79:     }
  80:     public void reset()
  81:     {
  82:         hypo=0;
  83:     }
  84:     public int getTotalSteps()
  85:     {
  86:         return (int)(realHypo/d);
  87:     }
  88: }

Tuesday, 4 January 2011

Momentus XT kicks a*s!

Finally I got my Seagate Momentus XT 500GB hybrid HDD (my candy apple) as a birthday gift from my sister (it is just a SE guy's birthday gift : ) ). And as a result of my birthday date which was on 1st of January, I stepped in 2011 while formating my new HDD and installing Windows 7, Visual Studio 2010, MS SQL, oh oh the drivers of course, MS Office, photoshop..... (it goes on) to my PC. First of all, I must say that I love using PC :)). Because I read lots of troubles about this HDD who uses MAC or Linux systems on internet. The second is, this baby rocks :)) especially with FW sd24 update. Even it is an hybrid (4GB SDD), it is very fast at booting up W7 and heavy applications such as Eclipse 3.6 hellios, Visual Studio and Photoshop. I am really satisfied with this product and suggest it every body who wants to use SDD but not have enough money (or is not stupid enough; 250GB for almost $500 Waaa???) to buy full SDD. You renounce the writing speed but application booting up performance is spectacular.

Thursday, 23 December 2010

Custom Timer for Android OS (with Cross Thread Messaging to Handle the UI updates )

Code below shows MyTimer class which implements the Runnable interface in order to be run as an individual thread. This class also supports MyTimerListener to inform the classes which are registered to this class, onTick event.

MyTimerClass

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MyTimer implements Runnable {

long interval;
List<MyTimerListener> l = new ArrayList<MyTimerListener>();
@Override
public void run() {
while(true)
{
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator<MyTimerListener> it = l.iterator();
while(it.hasNext())
{
((MyTimerListener)it.next()).onTick();
}
}

}
MyTimer(long Interval)
{
interval=Interval;
}
public void addListener(MyTimerListener mtl)
{
if(!l.contains(mtl))
l.add(mtl);
}
public void removeListener(MyTimerListener mtl)
{
if(l.contains(mtl))
l.remove(mtl);
}
public void start()
{
Thread t = new Thread(this);
t.start();
}

}



MyTimerListerner interface responsible for handling the onTick event.


public interface MyTimerListener {
void onTick();
}



Implementation of MyTimer


android.os.Handler;

public class AttachedClass implements MyTimerListener {

private MyTimer mt = new MyTimer(1);
private Handler mHandler = new Handler();// Main thread attaaches this handler itself while it creates the instance of AttachedClass instance
public void startAttachedClass()
{
mt.start();// start method creates a new thread and runs mt in it.
}
@Override
public void onTick() {
Runnable r = new Runnable() {
public void run() {
TextView1.setText("Text has setted");
}
};
mHandler.post(r);// onTick event called by new thread which has been created in mt.start(). but TextView1 belonges to the main thread. In order to make handled setText method by new thread, Handler's post method has used which requires a Runnable(method) which will be run in main thread.

}
}
}


You can leave your questions to the comment section. Than you..

Monday, 20 December 2010

Android TranslateAnimation sucks!

I really searched a lot on getting animated object's current location on the screen during the translate animation but I just found an android forum complaint title on it. And an Android officer was saying that It is not available to get this info during the animation and it could be added in the newer versions of android sdk. But this sucks and an important lack if you are developing a game in 2D. Animations are important and there are just 3 event listeners in IAnimationListener which listens 3 events of the animation; Start, End, Repeat. What about the during? I want to gather the info about my object which has being effected. Therefore, there is just 1 way for me, I will develop my own animation class rather to use TranslateAnimation. I will share it from here shortly.

Wednesday, 8 December 2010

Damn developer.android.com is not working

The is the second time in last 3 days I am trying to connect to the Android Developer website (developer.android.com) and it is not responding again. It is a massive shame to Google. I hated it damnn!! the the worst is because of the Android Developer website is so good, there is no alternative answers to Android problems in anonymous website in the internet.

Edit: It has been Turkish Telecom's fault some wrong google IPs are stille banned because of Youtube Ban in Turkey. Grghhhhhhh....

Monday, 15 November 2010

My Windows Phone 7 Application: Ringring

My very first WP7 application, I have developed an alarm clock it is my custom design :). I also designed my custom slider control which cannot be found anywhere. This application is for understanding WP7 application development I will come with new ones in the upcoming days.