Beziere Curve movement

Discuss issues specific to using Box2D on the iPhone
betece
Posts: 5
Joined: Fri Oct 04, 2013 2:23 am

Beziere Curve movement

Postby betece » Sun Mar 30, 2014 9:28 am

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.

pTymN
Posts: 518
Joined: Tue Sep 25, 2007 2:22 pm

Re: Beziere Curve movement

Postby pTymN » Thu Jul 24, 2014 6:27 am

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);
            }
      }


Return to “iPhone”



Who is online

Users browsing this forum: No registered users and 2 guests