Chandan Shetty SP
Chandan Shetty SP

Reputation: 5117

How to convert bezier curve to box2d object?

I am drawing line using bezier curve and i need to convert that bezier curve into box2d object. Which object can i use in box2d? any suggestions?

Upvotes: 1

Views: 1688

Answers (1)

Ringo_D
Ringo_D

Reputation: 372

Try to understand it...

+(b2ChainShape)curveWithPoints:(CGPoint*)points Times:(int)times
{
    //points.count must be 3
    b2ChainShape shape;
    float step = 1/(float)times;
    float t = 0;
    b2Vec2 *p = new b2Vec2[times];
    b2Vec2 v1 = [CCMethod toMeter:points[0]];
    b2Vec2 v2 = [CCMethod toMeter:points[1]];
    b2Vec2 v3 = [CCMethod toMeter:points[2]];
    for(int i = 0;i < times;i++){
        b2Vec2 pa = v1;
        pa *= ( (t-1)*(t-1)*0.5 );
        b2Vec2 pb = v2;
        pb *= ( (-t)*t+t+0.5 );
        b2Vec2 pc = v3;
        pc *= ( t*t*0.5 );
        p[i] = pa+pb+pc;
        t+=step;
    }
    shape.CreateChain(p, times);
    return shape;
}

The only thing you need to do next is to create body and fixture with this shape. I hope it is hopeful to you ...

Upvotes: 2

Related Questions