数学:缓入,使用带有时间限制的Hermite曲线缓和位移 [英] Math: Ease In, ease Out a displacement using Hermite curve with time constraint

查看:99
本文介绍了数学:缓入,使用带有时间限制的Hermite曲线缓和位移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一种方法,该方法使用开始时的加速度和结束时的减速度(缓和/缓入),并在仅有的约束条件下随时间从0插值到x(一维对象的位置) 提供了总时间,以及加速和减速的持续时间.该运动应复制惯性效果,并且我正在考虑对于非线性部分使用 Hermite曲线

I'm trying to write a method that interpolates from 0 to x (position of an object in one dimension) over time using acceleration at the beginning and deceleration at the end (ease out / ease in) with the only constraints that the total time is provided, as well as the duration of the acceleration and deceleration. the motion should replicate the inertia effect and I'm considering a Hermite curve for the non-linear portions.

double Interpolate(
    double timeToAccel, double timeCruising, double timeToDecel,
    double finalPosition,
    double currentTime)
{
    //...
}

有人可以指出我这样做的一部分代码吗?我不知道如何对Hermite曲线进行积分,因此不知道我将在加速部分或减速部分中移动多少,从而我也无法弄清楚线性运动的速度是多少.部分.

Can someone point me out to a portion of code that does that? I don't know how to integrate the Hermite curve, hence don't know how much I'll move in the accelerating portion or in the decelerating portion, and in turn I can't figure out what will be the speed in the linear portion.

谢谢.

一些参考来说明我的问题.

编辑:

  • 开始和结束速度为空,并且当前时间也是该方法中参数的一部分,我已经更新了签名.
  • 基本上,这个想法是想像在距离d上以恒定速度运动,这给出了总持续时间.然后,我们在保持相同的持续时间的同时添加加速和减速阶段,因此我们有一个未知的新巡航速度来确定(因为在Hermite阶段中移动的次数少于已替换的线性阶段中的移动次数).与相同持续时间的线性移动相比,在Hermite阶段中丢失的移动量可能是曲线的顶部和底部面积之比,这只是一个非专家的想法.
  • start and end speeds are null, and the current time is also part of the parameters in the method, I've updated the signature.
  • basically the idea is to imagine a move at constant speed on a distance d, this gives a total duration. Then we add the acceleration and deceleration phases, while maintaining the same duration, hence we have an unknown new cruise speed to determinate (because we move less in the Hermite phases than in the linear phases they have replaced). Maybe the amount of move lost in the Hermite phases, compared to a linear move of the same duration is the ratio between the top and bottom area in the curves, just an idea from a non expert.

编辑:Roman和Bob10提供了完整的解决方案.我实现了Roman的代码.谢谢你们俩,伙计们!感谢您的全力支持和详尽的解决方案,为我省去了漫长的搜索和试用期.

Edit: Roman and Bob10 have provided full working solutions. I implemented the code from Roman. Thanks to you both, guys! I appreciate your perfect support and your detailed solutions, you saved me long searches and trials.

推荐答案

首先,让我们制作一个三次方Hermite样条函数:

First, let's make a cubic hermite spline function:

/*
  t  - in interval <0..1>
  p0 - Start position
  p1 - End position
  m0 - Start tangent
  m1 - End tangent
*/
double CubicHermite(double t, double p0, double p1, double m0, double m1) {
   t2 = t*t;
   t3 = t2*t;
   return (2*t3 - 3*t2 + 1)*p0 + (t3-2*t2+t)*m0 + (-2*t3+3*t2)*p1 + (t3-t2)*m1;
}

现在您的任务是为缓入和缓出部分计算p0,p1,m0和m1.让我们添加一些变量来使数学更容易编写:

Now your task is to calculate the p0, p1, m0 and m1 for both ease-in and ease-out portions. Let us add a few variables to make the math a bit easier to write:

double Interpolate(
    double timeToAccel, double timeCruising, double timeToDecel,
    double finalPosition,
    double currentTime) {

    double t1 = timeToAccel;
    double t2 = timeCruising;
    double t3 = timeToDecel;
    double x = finalPosition;
    double t = currentTime;

我们需要指定对象在停止加速并开始减速时应位于的位置.您可以根据自己的喜好指定它们,并且仍然可以使运动平稳,但是,我们希望有一个自然"的解决方案.

We need to specify where should the object be when it stops accelerating and starts decelerating. You can specify these however you please and still produce a smooth movement, however, we would like a somewhat "natural" solution.

让我们假设巡航速度为v.在巡航过程中,物体行进距离x2 = v * t2.现在,当对象从0加速到速度v时,其行进距离x1 = v * t1 / 2.减速x3 = v * t3 / 2相同.放在一起:

Let's assume that the cruising speed is v. During crusing, the object travels distance x2 = v * t2. Now, when the object accelerates from 0 to speed v, it travels distance x1 = v * t1 / 2. Same for deceleration x3 = v * t3 / 2. Put all together:

x1 + x2 + x3 = x

x1 + x2 + x3 = x

v * t1/2 + v * t2 + v * t3/2 = x

v * t1 / 2 + v * t2 + v * t3 / 2 = x

由此我们可以计算出速度和距离:

From that we can calculate our speed and the distances:

    double v = x / (t1/2 + t2 + t3/2);
    double x1 = v * t1 / 2;
    double x2 = v * t2;
    double x3 = v * t3 / 2;

现在我们知道了一切,我们将其输入到三次Hermite样条插值器中

And now that we know everything, we just feed it into our cubic hermite spline interpolator

    if(t <= t1) {
       // Acceleration
       return CubicHermite(t/t1, 0, x1, 0, v*t1);
    } else if(t <= t1+t2) {
       // Cruising
       return x1 + x2 * (t-t1) / t2;
    } else {
       // Deceleration
       return CubicHermite((t-t1-t2)/t3, x1+x2, x, v*t3, 0);
    }
}

我在Excel中对此进行了测试,这是等效的VBA代码.对于边界条件,有一些除以零的方法,我将其作为练习留给读者解决

I tested this in Excel, here's the equivalent VBA code to play with. There are some divisions by zero for boundary conditions, I leave fix to this as an excercise to the reader


Public Function CubicHermite(t As Double, p0 As Double, p1 As Double, _
m0 As Double, m1 As Double) As Double
   t2 = t * t
   t3 = t2 * t
   CubicHermite = (2 * t3 - 3 * t2 + 1) * p0 + _
(t3 - 2 * t2 + t) * m0 + (-2 * t3 + 3 * t2) * p1 + (t3 - t2) * m1
End Function

Public Function Interpolate(t1 As Double, t2 As Double, t3 As Double, _
x As Double, t As Double) As Double
    Dim x1 As Double, x2 As Double, x3 As Double

    v = x / (t1 / 2 + t2 + t3 / 2)
    x1 = v * t1 / 2
    x2 = v * t2
    x3 = v * t3 / 2

    If (t <= t1) Then
       Interpolate = CubicHermite(t / t1, 0, x1, 0, v*t1)
    ElseIf t <= t1 + t2 Then
       Interpolate = x1 + x2 * (t - t1) / t2
    Else
       Interpolate = CubicHermite((t-t1-t2)/t3, x1+x2, x, v*t3, 0)
    End If
End Function

这篇关于数学:缓入,使用带有时间限制的Hermite曲线缓和位移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆