body attempting to follow user defined path

I can't be the first person that is after this behavior, but I did try searching and didn't find anything easily.

I would like to allow the user to first draw a path that a body will attempt to follow, then start the simulation. I would like the body to be affected by other bodies, but all the while attempting to get back to, or stay on, the path. I would like this behavior to act as you might expect for a character in a game, in other words I don't want it to snap back to the path immediately, or move faster than some defined max speed.

Any ideas on how to achieve this would be much appreciated, thanks! 🙂
You can use a steering behavior.
http://www.red3d.com/cwr/steer/

It simply consists of applying forces to your body in the correct way. Every frame you will compute a force based on :
- the body current position
- the target current position (a point moving on your path)
- the body current velocity
- a maximum speed constant
- maybe another constant that describes how fast the body can reach the maximum speed and therefore how much the body can be affected by other objects (I call that control)

The following is a repost of my answer in this thread :
posting.php?mode=edit&f=3&p=26224

Code:
//----------------------------------------------------------------------------------------
// dt              : frame delta time
// targetPosition  : the position to aim
// maxSpeed        : the maximum missile speed
// control         : a value bewteen [0,1]. 1 means reach the desired velocity in one frame. 0 means never reach the desired velocity.
//----------------------------------------------------------------------------------------
void Missile::UpdateAiming(float dt, const b2Vec2& targetPosition, float maxSpeed, float control)
{
    b2Vec2 missilePosition = m_body->GetPosition();
    b2Vec2 diff = targetPosition - missilePosition;
    float dist = diff.Length();
    if (dist > 0)
    {
        // compute the aiming direction
        b2Vec2 direction = diff / dist;

        // get the current missile velocity because we will apply a force to compensate this.
        b2Vec2 currentVelocity = m_body->GetLinearVelocity();

        // the missile ideal velocity is the direction to the target multiplied by the max speed
        b2Vec2 desireVelocity = direction * maxSpeed;

        // compensate the current missile velocity by the desired velocity, based on the control factor
        b2Vec2 finalVelocity = control * (desireVelocity - currentVelocity);

        // transform our velocity into an impulse (get rid of the time and mass factor)
        b2Vec2 finalForce = finalVelocity * (m_body->GetMass() / dt);

        // finaly apply the force
        m_body->ApplyForce(finalForce, b2Vec2_zero);
    }
}


I splitted the code to comment it in the post, but you could write that in 5 lines.
I use those values on my game :
maxSpeed = 5
control = 0.05
Thanks noon! That looks like exactly what I was searching for 🙂 I'll give it a shot.

Original thread: https://box2d.org/forum/viewtopic.php?t=5910