0

I need to calculate the position of a view as it travels from point A to point B. There will be an input value that will change linearly, and based on that input value I need to calculate the position of my view. By the end both the input value and my view will have moved exactly the same amount. The view only moves along one axis.

The tricky part is I want to ease the animation in and out, so at the begin and end of the movement it's moving slower than the input value, and during the middle it will need to move faster than it, in order to makeup the lost travel distance from the slow start and end.

So basically the formula I'm trying to write will have a velocity curve that looks something like this. Acceleration Curve

So my signature looks something like this:

func smoothedPosition(position: CGFloat, minPosition: CGFloat, maxPosition: CGFloat) -> CGFloat {

And the results need to even out over time so it should be something like

where smoothedPosition(position: 100, minPosition: 100, maxPosition: 200) expect 100

where smoothedPosition(position: 101, minPosition: 100, maxPosition: 200)) expect something between 100 and 101

where smoothedPosition(position: 150, minPosition: 100, maxPosition: 200)) expect 150

where smoothedPosition(position: 199, minPosition: 100, maxPosition: 200)) expect something between 199 and 200

where smoothedPosition(position: 200, minPosition: 100, maxPosition: 200)) expect 200

I'm sure there's some sort of equation to do exactly this, but maths was never my strong point. Can somebody point me in the right direction.

Mark Bridges
  • 8,228
  • 4
  • 50
  • 65
  • So is that a graph of acceleration or is really velocity? – ThomasMcLeod Jan 26 '17 at 16:11
  • Come to think of it, it's velocity. – Mark Bridges Jan 26 '17 at 16:14
  • You may be getting a bit confused by thinking of it in terms of velocity. Velocity is the time derivative of distance, and while it's possible to write a function like this I terms of velocity, really what you need is a distance-in-distance-out function, if I understand the problem correctly. – ThomasMcLeod Jan 26 '17 at 16:30

1 Answers1

0

What you probably want to do is use a negative cosine function from distance in to distance out. Scale the input distance so that it goes from 0 to 180 degrees. Then the function will be something like (C++)

double f(double inDist)
{
    return 1 - cos(inDist) / 2;
}

This function returns a number from 0 to 1. This is your scale factor to multiple against the output distance.

ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80