0

我需要画一条线,它是 box2d 的主体,将由 CCSprite 对象绘制。现在我可以画出非常漂亮的线(仅在调试绘制期间可见)但我不知道,我可以在绘制期间使用所有点的位置。如果代码如下所示,CCSprite 图像仅在用户绘制速度非常慢时出现。当用户快速绘图时,我们只能看到几个图像点。我想我应该从 SetAsEdge 函数中获得积分,并为每一个绘制 CCSprite,但我不知道如何获得这些积分。

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

            }
4

1 回答 1

0

好的,我找到了解决方案,也许这对其他人有用。

        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];
       }
于 2012-08-21T12:29:41.607 回答