开发者

Algorithm to control acceleration until a position is reached

I have a point that moves (in one dimension), and I need it to move smoothly. So I think that it's velocity has to be a continuous function and I need to control the acceleration and then calculate it's velocity and position.

The algorithm 开发者_运维知识库doesn't seem something obvious to me, but I guess this must be a common problem, I just can't find the solution.

Notes:

  • The final destination of the object may change while it's moving and the movement needs to be smooth anyway.
  • I guess that a naive implementation would produce bouncing, and I need to avoid that.


This is a perfect candidate for using a "critically damped spring".

Conceptually you attach the point to the target point with a spring, or piece of elastic. The spring is damped so that you get no 'bouncing'. You can control how fast the system reacts by changing a constant called the "SpringConstant". This is essentially how strong the piece of elastic is.

Basically you apply two forces to the position, then integrate this over time. The first force is that applied by the spring, Fs = SpringConstant * DistanceToTarget. The second is the damping force, Fd = -CurrentVelocity * 2 * sqrt( SpringConstant ).

The CurrentVelocity forms part of the state of the system, and can be initialised to zero.

In each step, you multiply the sum of these two forces by the time step. This gives you the change of the value of the CurrentVelocity. Multiply this by the time step again and it will give you the displacement.

We add this to the actual position of the point.

In C++ code:

float CriticallyDampedSpring( float a_Target,
                              float a_Current,
                              float & a_Velocity,
                              float a_TimeStep )
{
    float currentToTarget = a_Target - a_Current;
    float springForce = currentToTarget * SPRING_CONSTANT;
    float dampingForce = -a_Velocity * 2 * sqrt( SPRING_CONSTANT );
    float force = springForce + dampingForce;
    a_Velocity += force * a_TimeStep;
    float displacement = a_Velocity * a_TimeStep;
    return a_Current + displacement;
}

In systems I was working with a value of around 5 was a good point to start experimenting with the value of the spring constant. Set it too high will result in too fast a reaction, and too low the point will react too slowly.

Note, you might be best to make a class that keeps the velocity state rather than have to pass it into the function over and over.

I hope this is helpful, good luck :)

EDIT: In case it's useful for others, it's easy to apply this to 2 or 3 dimensions. In this case you can just apply the CriticallyDampedSpring independently once for each dimension. Depending on the motion you want you might find it better to work in polar coordinates (for 2D), or spherical coordinates (for 3D).


I'd do something like Alex Deem's answer for trajectory planning, but with limits on force and velocity:

In pseudocode:

xtarget:  target position
vtarget:  target velocity*
x: object position
v: object velocity
dt: timestep

F = Ki * (xtarget-x) + Kp * (vtarget-v);
F = clipMagnitude(F, Fmax);
v = v + F * dt;
v = clipMagnitude(v, vmax);
x = x + v * dt;

clipMagnitude(y, ymax):
   r = magnitude(y) / ymax
   if (r <= 1)
       return y;
   else
       return y * (1/r);

where Ki and Kp are tuning constants, Fmax and vmax are maximum force and velocity. This should work for 1-D, 2-D, or 3-D situations (magnitude(y) = abs(y) in 1-D, otherwise use vector magnitude).


It's not quite clear exactly what you're after, but I'm going to assume the following:

  1. There is some maximum acceleration;
  2. You want the object to have stopped moving when it reaches the destination;
  3. Unlike velocity, you do not require acceleration to be continuous.

Let A be the maximum acceleration (by which I mean the acceleration is always between -A and A).

The equation you want is

v_f^2 = v_i^2 + 2 a d

where v_f = 0 is the final velocity, v_i is the initial (current) velocity, and d is the distance to the destination (when you switch from acceleration A to acceleration -A -- that is, from speeding up to slowing down; here I'm assuming d is positive).

Solving:

d = v_i^2 / (2A)

is the distance. (The negatives cancel).

If the current distance remaining is greater than d, speed up as quickly as possible. Otherwise, begin slowing down.

Let's say you update the object's position every t_step seconds. Then:

new_position = old_position + old_velocity * t_step + (1/2)a(t_step)^2
new_velocity = old_velocity + a * t_step.

If the destination is between new_position and old_position (i.e., the object reached its destination in between updates), simply set new_position = destination.


You need an easing formula, which you would call at a set interval, passing in the time elapsed, start point, end point and duration you want the animation to be.

Doing time-based calculations will account for slow clients and other random hiccups. Since it calculates on time elapsed vs. the time in which it has to compkete, it will account for slow intervals between calls when returning how far along your point should be in the animation.

The jquery.easing plugin has a ton of easing functions you can look at:

http://gsgd.co.uk/sandbox/jquery/easing/

I've found it best to pass in 0 and 1 as my start and end point, since it will return a floating point between the two, you can easily apply it to the real value you are modifying using multiplication.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜