Beziere Curve movement

Hi Box2d forum,

i have a question about moving an object in a beziere curve. At the moment the object can move in a curve way, but i want to set the speed or the duration how long the object need to move along the beziere curve path. Here is the update method of the object:

Code: Select all

static inline CGFloat bezierat( float a, float b, float c, float d, ccTime t )
{
   return (powf(1-t,3) * a +
         3*t*(powf(1-t,2))*b +
         3*powf(t,2)*(1-t)*c +
         powf(t,3)*d );
}

-(void)update:(ccTime)delta{

    CGFloat xa = 0;
    CGFloat xb = _config.controlPoint_1.x;
    CGFloat xc = _config.controlPoint_2.x;
    CGFloat xd = _config.endPosition.x;
   
    CGFloat ya = 0;
    CGFloat yb = _config.controlPoint_1.y;
    CGFloat yc = _config.controlPoint_2.y;
    CGFloat yd = _config.endPosition.y;
   
    CGFloat x = bezierat(xa, xb, xc, xd, delta);
    CGFloat y = bezierat(ya, yb, yc, yd, delta);
   
    x /= PTM_RATIO;
    y /= PTM_RATIO;

    _physicalBody->SetLinearVelocity(b2Vec2(x,y));

}

As you can see i use the "SetLinearVelocity" to move the object. The problem is when i increase x and y the object is to fast and doesnt follow the correct bezier curve path. So my question is how can i add x and y to the object to move it without having this problem. Or does anyone has an example of how box2d object follow bezier curve paths.

I hope anyone can understand me.

Thank you in advance.
You need to take an account of the old position to determine the offset between positions in order to decide the velocity needed.

Code: Select all

      TSBody* gameObject = (TSBody*)body->GetUserData();
      if(gameObject)
        {
         gameObject->update(time);
           
            const b2Transform& transform = body->GetTransform();
           
            b2Vec2 currentPosition = transform.p;
           
            assert(!glm::isnan(currentPosition.x));
            assert(!glm::isnan(currentPosition.y));
            TSVec3 futureVelocity;
            TSVec3 futurePosition = gameObject->position.at(time, futureVelocity);
           
            b2Vec2 newVelocity = 60.0 * b2Vec2(futurePosition.x - currentPosition.x,
                                               futurePosition.y - currentPosition.y);
           
            body->SetLinearVelocity(newVelocity);
           
            if(!body->IsFixedRotation())
            {
                TSFloat currentAngle = transform.q.GetAngle();
                TSFloat futureAngle = gameObject->angle.at(time);
                body->SetAngularVelocity(normalizeAngle(futureAngle, currentAngle) - currentAngle);
            }
      }

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