b2Body动画?

时间:2011-11-10 02:50:33

标签: ios animation cocos2d-iphone box2d

我在游戏中使用Box2D和Cocos2D。我知道Cocos2D有一个名为CCMoveTo的动画API。我想在Box2D中用b2Bodies做同样的事情。我希望能够从最初的位置设置目标点和动画时间。有谁知道这是否可能? 我只想尽量保持这一点。

我愿意接受意见和建议!

谢谢!

Edit1 :好的,我想让我的b2Bodys永久跟随我的CCSprite。 您向我展示的代码存在问题。

在我的.h我这样做:

b2World *world; b2Body *body;

然后我稍微修改了一下这个方法,现在它看起来像这样:

for (body = world->GetBodyList(); body != nil; body = body->GetNext())
    {
        CCSprite *bodyNode = body->GetUserData();
        if (bodyNode != nil)
        {
            // this transfers the body's position and rotation to the sprite
            bodyNode.position = [Helper toPixels:body->GetPosition()];
            float angle = body->GetAngle();
            bodyNode.rotation = -(CC_RADIANS_TO_DEGREES(angle));

            // and this would do the exact opposite
            b2Vec2 pos = [Helper toMeters:bodyNode.position];
            body->SetTransform(pos, CC_DEGREES_TO_RADIANS(bodyNode.rotation));
            body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
            body->SetAngularVelocity(0.0f);
        }
    }

所以我使用了ivars而不是实例变量。这可以吗? 我也得到2个错误:

  1. 我得到了无法初始化'CCSprite'类型的变量,其值为'void',在body-> GetUserData行

  2. 我也得到:使用未声明的标识符'Helper'。我将这两个辅助方法放入我的班级,但我不确定Helper是什么。

  3. Edit2 :这看起来如何?我决定将这些方法保留在我的课程中,而不是专门为它创建一个新方法。

        // for each body, get its assigned BodyNode and update the sprite's position
        for (body = world->GetBodyList(); body != nil; body = body->GetNext())
        {
            CCSprite* sprite = (CCSprite*)body->GetUserData();
            if (sprite != nil)
            {
                // this transfers the body's position and rotation to the sprite
                sprite.position = [self toPixels:body->GetPosition()];
                float angle = body->GetAngle();
                sprite.rotation = -(CC_RADIANS_TO_DEGREES(angle));
    
                // and this would do the exact opposite
                b2Vec2 pos = [self toMeters:sprite.position];
                body->SetTransform(pos, CC_DEGREES_TO_RADIANS(sprite.rotation));
                body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
                body->SetAngularVelocity(0.0f);
            }
        }
    }
    
    // convenience method to convert a CGPoint to a b2Vec2
    -(b2Vec2)toMeters:(CGPoint)point
    {
        return b2Vec2(point.x / CTM_RATIO, point.y / CTM_RATIO);
    }
    
    // convenience method to convert a b2Vec2 to a CGPoint
    -(CGPoint)toPixels:(b2Vec2)vec
    {
        return ccpMult(CGPointMake(vec.x, vec.y), CTM_RATIO);
    }
    

1 个答案:

答案 0 :(得分:1)

您可以暂时或永久地反转CCSprite与b2Body的关系。通常在您的更新方法中,CCSprite将被设置为b2Body的位置。如果你有一个使用CCMoveTo移动到特定位置的CCSprite,你可以改为为b2Body分配精灵的位置和旋转。

您需要做的就是确定控制身体的时间和精灵:

// for each body, get its assigned BodyNode and update the sprite's position
for (b2Body* body = world->GetBodyList(); body != nil; body = body->GetNext())
{
    BodyNode* bodyNode = body->GetUserData();
    if (bodyNode != nil)
    {
        // this transfers the body's position and rotation to the sprite
        bodyNode.position = [Helper toPixels:body->GetPosition()];
        float angle = body->GetAngle();
        bodyNode.rotation = -(CC_RADIANS_TO_DEGREES(angle));

        // and this would do the exact opposite
        b2Vec2 pos = [Helper toMeters:bodyNode.position];
        body->SetTransform(pos, CC_DEGREES_TO_RADIANS(bodyNode.rotation));
        body->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
        body->SetAngularVelocity(0.0f);
    }
}

Helper方法定义如下:

// convenience method to convert a CGPoint to a b2Vec2
+(b2Vec2) toMeters:(CGPoint)point
{
    return b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO);
}

// convenience method to convert a b2Vec2 to a CGPoint
+(CGPoint) toPixels:(b2Vec2)vec
{
    return ccpMult(CGPointMake(vec.x, vec.y), PTM_RATIO);
}