# How to achieve uniform speed of movement on a bezier curve?

I’m trying to move an image along Bezier curve. This is how I do it:

```
- (void)startFly
{
[self runAction:[CCSequence actions:
[CCBezierBy actionWithDuration:timeFlying bezier:[self getPathWithDirection:currentDirection]],
[CCCallFuncN actionWithTarget:self selector:@selector(endFly)],
nil]];
}
```

My issue is that the image moves not uniformly. In the beginning it’s moving slowly and then it accelerates gradually and at the end it’s moving really fast. What should I do to get rid of this acceleration?

sam hocevaron November 30, -0001 @ 12:00 AMIt is possible to approximate a solution to this problem for most parametric trajectories. The idea is the following: if you zoom deep enough on a curve, you cannot tell the curve itself from its tangent at that point.

By making this assumption, there is no need to precompute anything more than two vectors (three for cubic Bezier curves,

etc.).So for a curve

`M(t)`

we compute its tangent vector`dM/dt`

at point`t`

. The norm of this vector is`||dM/dt||`

and thus the distance traveled for a duration`Δt`

can be approximated as`||dM/dt||Δt`

. It follows that a distance`L`

is traveled for a duration`L/||dM/dt||`

.## Application: quadratic Bezier curve

If the control points of the Bezier curve are

`A`

,`B`

and`C`

, the trajectory can be expressed as:So the derivative is:

You just need to store vectors

`v1 = 2A - 4B + 2C`

and`v2 = -2A + 2B`

somewhere. Then, for a given`t`

, if you want to advance of a length`L`

, you do:## Cubic Bezier curves

The same reasoning applies to a curve with four control points

`A`

,`B`

,`C`

and`D`

:The derivative is:

We precompute

`v1 = -3A + 9B - 9C + 3D`

,`v2 = 6A - 12B + 6C`

and`v3 = -3A + 3B`

and the final formula is:## Accuracy issues

If you are running at a reasonable framerate,

`L`

(which should be computed according to the frame duration) will be sufficiently small for the approximation to work.However, you may experience inaccuracies in extreme cases. If

`L`

is too large, you can do the computation piecewise, for instance using 10 parts:Robert Fraseron November 30, -0001 @ 12:00 AMYou need to reparamaterize the curve. The easiest way to do this is to calculate the arc lengths of several segments of the curve and use these to figure out where you should sample from. For example, maybe at t=0.5 (halfway through), you should pass s=0.7 to the curve to get the “halfway” position. You need to store a list of arc lengths of various curve segments to do this.

There are probably better ways, but here’s some very simple C# code I wrote to do this in my game. It should be easy to port to Objective C:

Edit:It’s worth noting that this won’t give you the exact arc length, since it’s impossible to get the arc length of a cubic curve. All this does is estimate the length of the various segments. Depending on how long the curve is, you may need to increase the resolution to prevent it from changing speeds when it reaches a new segment. I usually use ~100, which I’ve never had a problem with.Jebbleson November 30, -0001 @ 12:00 AMI don’t know anything about cocos2, but a bezier curve is a kind of parametric equation, so you should be able to get your x and y values in terms of time.