I've created a movieclip spawner class and animator class which animate movieclips along a set of x,y points. I've used Greensock's TweenMax to accomplish the animation and I have set the speed to be 1 second between each point. The issue is that when the distance between two points is small it looks too slow.
What I would like to do is somehow calculate the distance between the two points and output a speed in the range of 0 to 1 seconds so that the speed is to be correct for the shorter distances as well as the longer distances.
Any help or advice would be greatly appreciated.
Thankyou,
eb_dev
I second what Matti said, but it can be made faster - for a cost of about 48 to 64 bits of memory (WHOAH so much!!! ...)
var dX:Number = x1 - x2;
var dY:Number = y1 - y2;
var dist:Number = Math.sqrt(dX * dX + dY * dY);
var animTime = dist / state.stageWidth;
And to explain that on eb Dev's request, the square root there is a part of Pythagorean theorem:
a^2 + b^2 = c^2
Lines on X and Y axes can be seen as two legs of a right-angled triangle, and the hypotenuse would therefore be the distance between two specific points (which we need).
First we subtract the points (x1 - x2, y1 - y2). We still have two points, however, the right-angle part of the triangle is now at 0, 0 - the point where X and Y axes cross. To be able to apply this to Pythagorean theorem, let's see what size are a and b going to be - the length of leg a is the distance between 0, 0 and point 1. Subtracting 0, 0 will still leave us with just point 1 - however, we know that it will be somewhere on X axis - therefore its Y will be 0. We can say that the length of a equals X part of point 1.
Same goes for b leg and point 2. This time, however, we get just Y part of it.
And to calculate c, we will first sum the second powers of a and b (a^2 + b^2). We now have c^2, so to get the c - hypotenuse, we will calculate it's square root (that is Math.sqrt() in AS3.0).
c is the distance.
Hope this explains this.