Algorithm to control acceleration until a position is reached

前端 未结 4 1151
[愿得一人]
[愿得一人] 2021-02-02 02:42

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 accelerati

4条回答
  •  不思量自难忘°
    2021-02-02 03:09

    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.

提交回复
热议问题