Top-down conveyor belt

I'm having trouble modeling a conveyor belt for a top-down environment. So, if an object moves onto the belt, it should be moved in the direction of the belt, but all other forces should act on the object as usual.

I can't simply use SetLinearVelocity on the body, because that would ignore any previous velocity the object had. I.e., if the object enters the belt at a high speed, it should retain that high speed, but gradually (due to friction) slow down to the constant speed of the conveyor belt.

This topic seems interesting, but out of date and only meant for side-view conveyor belts.
viewtopic.php?f=3&t=2861

Perhaps this is a more general application of b2FrictionJoint? Instead of friction causing an object to come to rest, it causes the object to approach a certain constant velocity.

Any suggestion are appreciated. I can draw up some diagrams if it's unclear what I'm looking for.
Well, I have it working pretty well after some tweaking using the info in this page: http://www.iforce2d.net/b2dtut/constant-speed
Instead of setting the velocity directly, I use impulses.

For those curious, here's the relevant code on an entity's tick method. Basically, the conveyor belt is a sensor that sets the conveyance vector on the entity when it enters the belt.

Code: Select all

-(void)tick:(NSTimeInterval)time
{
    if (!(self.conveyance == VECZERO))
    {
        b2Body * body = self.principleBody;
        vec2 a = body->GetLinearVelocity();
        vec2 b = self.conveyance;
       
        //lc is the length of the projection of vel on the conveyance vector
        //i.e. how much it is already being conveyed
        float lc = dotProduct(a, b) / b.Length();
       
       
        //if it's not currently conveyed enough
        if (lc < b.Length())
        {
            vec2 desiredVel = b;
            desiredVel.Normalize();
            desiredVel *= (b.Length() - fmax(lc, 0)); //throw out a negative lc, bc moving against conveyance should be possible
            desiredVel *= self.mass;
           
            self.principleBody->ApplyLinearImpulse(desiredVel, self.principleBody->GetWorldCenter());           
        }
    }
}

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