Pergunta

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

Foi útil?

Solução

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.

Outras dicas

I think you want the min speed for smaller distance.

const MIN_SPEED:Number = ???// adjust this value
var distance:Number = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
var duration:Number = Math.min(1.0, distance / MIN_SPEED);

for long distance 1.0 < distance / MIN_SPEED, and duration will be set to 1.0[sec]
for small distance 1.0 > distance / MIN_SPEED, and duration will be set to move at some speed defined by MIN_SPEED.

What you want to do is calculate the distance between the two points and have the time be relative to that. How exactly you define the relation depends. Is a one seconds animation one where the thing moves from one end of the stage to the other or what. This will be your baseline and all the animations will be relative to it.

So let's say that an animation across the whole width of the stage is one second, and anything shorter than that is in relation to that one second.

dist = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
animTime = dist / state.stageWidth;

UPDATE: The Math.sqrt just takes the square root of the difference between the points. You don't necessarily need to do this since you are only interested in distance relative to the stage size not the actual distance. The reason it is there is to get the distance back in the same scale as the stage.stageSize. Notice that the (x1-x2)*(x1-x2) is squaring the distance between the x1 and x2 coordinates, ie. equal to Math.pow(x1-x2,2).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top