FLash AS3 - 如何将两个 xy 坐标之间的差异表示为 0 和 1 之间的数字?

发布于 2024-09-30 18:12:08 字数 242 浏览 7 评论 0原文

我创建了一个影片剪辑生成器类和动画师类,它们沿着一组 x,y 点对影片剪辑进行动画处理。我使用 Greensock 的 TweenMax 来完成动画,并将每个点之间的速度设置为 1 秒。问题是,当两点之间的距离很小时,它看起来太慢。

我想做的是以某种方式计算两点之间的距离并输出 0 到 1 秒范围内的速度,以便该速度对于较短距离和较长距离都是正确的。

任何帮助或建议将不胜感激。

谢谢你,

eb_dev

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅唱々樱花落 2024-10-07 18:12:08

我赞同 Matti 所说的,但它可以做得更快 - 需要大约 48 到 64 位内存的成本(哇啊这么多!!! ...)

var dX:Number = x1 - x2;
var dY:Number = y1 - y2;
var dist:Number = Math.sqrt(dX * dX + dY * dY);
var animTime = dist / state.stageWidth;

并解释一下 eb Dev 的要求,平方根是毕达哥拉斯定理的一部分:

a^2 + b^2 = c^2

X 轴和 Y 轴上的线可以看作直角三角形的两条边,因此斜边就是两条边之间的距离具体点(我们需要)。

首先我们减去点 (x1 - x2, y1 - y2)。我们仍然有两个点,但是,三角形的直角部分现在位于 0, 0 - X 轴和 Y 轴交叉的点。为了能够将其应用于毕达哥拉斯定理,让我们看看 ab 的大小 -腿的长度a0, 0和点1之间的距离。减去0, 0仍然是只留下点 1 - 但是,我们知道它将位于 X 轴上的某个位置 - 因此它的 Y 将为 0。我们可以说 a 的长度等于点 1 的 X 部分。

b 腿和点 2 也是如此。但是,这一次我们只得到它的 Y 部分。

为了计算c,我们首先将ab的二次幂相加 (a^2 + b^2)。我们现在有 c^2,因此为了得到 c - 斜边,我们将计算它的平方根(即 AS3.0 中的 Math.sqrt())。

c 是距离。

希望这能解释这一点。

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.

将军与妓 2024-10-07 18:12:08

我认为你想要较小距离的最小速度。

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);

长距离1.0<距离 / MIN_SPEED,持续时间将设置为 1.0[秒]
对于小距离 1.0 >距离 / MIN_SPEED,并且持续时间将设置为以 MIN_SPEED 定义的某个速度移动。

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.

自控 2024-10-07 18:12:08

您想要做的是计算两点之间的距离并让时间与该距离相关。您如何准确地定义该关系取决于。是一秒的动画,其中物体从舞台的一端移动到另一端或其他什么。这将是您的基线,所有动画都将与其相关。

因此,假设整个舞台宽度的动画为一秒,任何比这短的动画都与该一秒相关。

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

更新:
Math.sqrt 仅取点之间差值的平方根。您不一定需要这样做,因为您只对相对于舞台大小的距离感兴趣,而不是实际距离。它存在的原因是为了使距离恢复到与 stage.stageSize 相同的比例。请注意,(x1-x2)*(x1-x2)x1x2 坐标之间距离的平方,即。等于Math.pow(x1-x2,2)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文