0
votes

I need to draw line which will be box2d body and will be paint by CCSprite object. Now I could draw very nice line (visible only during debug draw on) but I don't know, from can I get position all points using during draw. If code looks like below, CCSprite image appear only when user draw very slow. When user is drawing fast we can see only few image points. I think I should get points form SetAsEdge functions and for each of one draw CCSprite, but I don't know how to get this points.

            CGPoint start = [touch locationInView: [touch view]];
            start = [[CCDirector sharedDirector] convertToGL: start];
            CGPoint end = [touch previousLocationInView:[touch view]];
            end = [[CCDirector sharedDirector] convertToGL:end];
            float distance = ccpDistance(start, end);
            if (distance > 1)
            {
                int d = (int)distance;

                b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
                b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
                CCSprite *obj = [CCSprite spriteWithFile:@"image.png"];      
                b2BodyDef bd;
                bd.userData = obj;
                bd.type = b2_staticBody;
                bd.position.Set(0, 0);
                obj.position = ccp(start.x,start.y);
                [self addChild:obj z:1];

                b2Body* body = _world->CreateBody(&bd);
                b2PolygonShape shape;
                shape.SetAsEdge(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
                body->CreateFixture(&shape, 0.0f);

            }
1

1 Answers

0
votes

Ok I found the solution, maybe this will be useful for someone else.

        CGPoint start = [touch locationInView: [touch view]];
        start = [[CCDirector sharedDirector] convertToGL: start];
        CGPoint end = [touch previousLocationInView:[touch view]];
        end = [[CCDirector sharedDirector] convertToGL:end];
        float distance = ccpDistance(start, end);
        if (distance > 1)
        {
            int d = (int)distance;

            b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
            b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);     
            b2BodyDef bd;            
            bd.type = b2_staticBody;
            bd.position.Set(0, 0);


            b2Body* body = _world->CreateBody(&bd);
            b2PolygonShape shape;
            shape.SetAsEdge(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
            body->CreateFixture(&shape, 0.0f);

            CGPoint diff = ccpSub(start, end);
                float rads = atan2f( diff.y, diff.x);
                float degs = -CC_RADIANS_TO_DEGREES(rads);
                float dist = ccpDistance(end, start);
                CCSprite *obj = [CCSprite spriteWithFile:@"image.png"];
                [obj setAnchorPoint:ccp(0.0f, 0.5f)];
                [obj setPosition:end];
                [obj setScaleX:dist/obj.boundingBox.size.width];
                [obj setRotation: degs];
                [self addChild:obj];
       }