在 Lua 中补间一个值

发布于 2024-07-20 14:26:03 字数 145 浏览 6 评论 0原文

这件事我该怎么办? 我想在 x 时间内将一个值从一个值补间为另一个值。 同时还要考虑到在开始和结束时有一个“轻松”的感觉会很好。

我知道,我真的不应该问,但我自己试过了,但我被困住了。 请假设要引起延迟,您需要调用函数 wait(time)。

How'd I go about this one? I want to tween a value from one to another in x time. While also taking into account that it'd be nice to have an 'ease' at the start and end.

I know, I shouldn't ask really, but I've tried myself, and I'm stuck.
Please assume that to cause a delay, you need to call function wait(time).

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

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

发布评论

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

评论(2

笔落惊风雨 2024-07-27 14:26:03

一种可能适合您的简单方法是沿着单位圆进行插值:
单位圆

为此,您只需评估沿圆的点,这确保了相当平滑的运动以及缓入和缓出。 您可以通过更改改变角度的速度来控制插值的速度。

假设您正在进行一维插值(即简单的标量插值,例如从 3.5 到 6.9 或其他),使用从 -π/2 到 π/2 的 Y 值可能会很方便。 这些是由正弦函数给出的,你需要做的就是应用合适的缩放:

angle = -math.pi / 2
start = 3.5
end = 6.9
radius = (end - start) / 2
value = start + radius + radius * math.sin(angle)

我不能 100% 确定这是否是合法的 Lua,没有测试它。 如果没有,转换可能很简单。

One simple approach that might work for you is to interpolate along the unit circle:
Unit circle

To do this, you simply evaluate points along the circle, which ensures a fairly smooth movement, and ease-in as well as ease-out. You can control the speed of the interpolation by changing how quickly you alter the angle.

Assuming you're doing 1-dimensional interpolation (i.e. a simple scalar interpolation, like from 3.5 to 6.9 or whatever), it might be handy to use Y-values from -π/2 to π/2. These are given by the sine function, all you need to do is apply suitable scaling:

angle = -math.pi / 2
start = 3.5
end = 6.9
radius = (end - start) / 2
value = start + radius + radius * math.sin(angle)

I'm not 100% sure if this is legal Lua, didn't test it. If not, it's probably trivial to convert.

戏剧牡丹亭 2024-07-27 14:26:03

您可以查看Tweener ActionScript 库来获取灵感。

例如,您可以借用必要的方程 从这里开始

如果您需要进一步的帮助,请询问。

You may look at Tweener ActionScript library for inspiration.

For instance, you may borrow necessary equations from here.

If you need further help, please ask.

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