Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm trying to write an interpolator for a translate animation, and I'm stuck. The animation passes a single value to the function. This value maps a value representing the elapsed fraction of an animation to a value that represents the interpolated fraction. The value starts at 0 and goes to 1 when the animation completes. So for instance, if I wanted a linear translation (constant velocity) my function would look like:

function(input) {
  return input
}

What I need is for the velocity to remain constant for half the animation, then decelerate rapidly to zero. What this essentially means is that the input values I return must be the same as the output values for half the animation (until 0.5), then the values must increase from 0.5 to 1.0 at a slower rate of change between calls than the input value change between calls.


EDIT (straight from the Android docs):

The following table represents the approximate values that are calculated by example interpolators for an animation that lasts 1000ms:

enter image description here

As the table shows, the LinearInterpolator changes the values at the same speed, .2 for every 200ms that passes. The AccelerateDecelerateInterpolator changes the values faster than LinearInterpolator between 200ms and 600ms and slower between 600ms and 1000ms.


EDIT 2:

I thought I should provide an example of something that works, just not the way I want it to. The function for a decelerate interpolation provided with the Android framework is exactly:

function(input) {
  return (1 - (1 - input) * (1 - input))
}
share|improve this question
    
I'm having trouble parsing what you're trying to describe. Could you include a sketch of what the graph of such a function looks like? –  Sammy Black Apr 25 '13 at 6:07
    
The average rate of change must be the same (since $f(0) = 0$ and $f(1) = 1$), so unless I misunderstand (a solid possibility), what you are describing is impossible? –  copper.hat Apr 25 '13 at 6:24
    
@SammyBlack, I added a table with example values for the function. –  Christopher Perry Apr 25 '13 at 6:38
    
@copper.hat, It's definitely possible, it's used in Android and this is the way it works. –  Christopher Perry Apr 25 '13 at 6:39
    
@ChristopherPerry: Perhaps I misunderstood. If the output equals the input on $[0,\frac{1}{2}]$, then on $(\frac{1}{2},1]$ the rate of change cannot be less than $1$ and still end up at $1$. –  copper.hat Apr 25 '13 at 6:51
show 5 more comments

2 Answers

Let $s(t)$ denote the distance moved at time $t$. Choose some value $h$ with $0 \le h \le 1$. Then the required function is:

$$s(t) = \frac{2t}{1+h} \quad \text{for } 0 \le t \le h $$

$$s(t) = \frac{t^2 - 2t + h^2}{h^2 - 1} \quad \text{for } h \le t \le 1 $$

The first part of the curve (where $0 \le t \le h$) is a straight line, obviously.

The second part (where $h \le t \le 1$) is a piece of parabola. At $t=h$, the line and the parabola join smoothly.

The speed of the particle varies as follows:

From $t=0$ to $t=h$, the speed is constant, $2/(1+h)$.

From $t=h$ to $t=1$, the speed gradually decreases.

At $t=1$, the speed is zero.

You can adjust $h$ to get the behaviour you want. Any value with $0 \le h \le 1$ will work.

If you choose $h=0$, you get the Android function.

If you choose $h=1$, you get purely linear motion.

Here's what the composite curve looks like when we choose $h=0.5$: enter image description here

share|improve this answer
add comment

This answers a problem motivated by your description, but it may not be the problem you want to solve. We assume constant velocity $k$ for $0\le t\le 0.5$. Then we have constant deceleration, so that at time $t=1$ velocity reaches $0$.

Under these conditions, you may want the net displacement at time $t$.

Our velocity at $t=0.5$ is $k$. It has to reach $0$ at time $1$, so the velocity at time $t$, for $0.5\le t\le 1$, is $k-\frac{k}{1-0.5}(t-0.5)=2k-2kt$.

Our net displacement $s(t)$ at time $t$ is $kt$ for $0\le t\le 0.5$.

For $0.5\lt t\le 1$, the net displacement is $(0.5)k +\int_{0.5}^t (2k-2kt)\,dt$. This simplifies to $2kt-kt^2-0.25k$.

Thus a formula for the net displacement $s(t)$ at time $t$ is $s(t)=kt$ for $0\le t\le 0.5$, and $s(t)=2kt -kt^2-0.25k$ for $0.5\lt t\le 1$.

share|improve this answer
    
How did you come up with k−2k(t−0.5) ? –  Christopher Perry Apr 25 '13 at 6:53
1  
Sorry about that! I should have written it equivalently as $k-\frac{k}{1-0.5}(t-0.5)$. It is the term that ensures that when $t=1$, the velocity dips to $k-k=0$. So it is linear interpolation for velocity. –  André Nicolas Apr 25 '13 at 7:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.