b2Body动画? [英] b2Body Animation?

查看:186
本文介绍了b2Body动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我打开的意见和建议!

谢谢!

EDIT1 :好,所以我想我的b2Bodys永久下面我CCSprite。
有一个与$ C $问题c您给我看。

在我的.h我这样做:

b2World *世界;
    b2Body *身体;

然后我修改的方法了一下,现在看起来是这样的:

 的(身体=世界 - > GetBodyList(!);身体=零;身体=身体 - >的GetNext())
    {
        CCSprite * bodyNode =身体 - > GetUserData();
        如果(bodyNode!=无)
        {
            //这个身体的位置和旋转传输到精灵
            bodyNode.position = [助手toPixels:贴体>为getPosition()];
            飘角=身体 - > GetAngle();
            bodyNode.rotation = - (CC_RADIANS_TO_DEGREES(角));            //这会做完全相反的
            b2Vec2 POS = [助手toMeters:bodyNode.position];
            身体 - >的setTransform(POS,CC_DEGREES_TO_RADIANS(bodyNode.rotation));
            身体 - > SetLinearVelocity(b2Vec2(0.0,0.0));
            身体 - > SetAngularVelocity(0.0);
        }
    }

所以我用实例变量,而不是实例变量。这可以吗?
此外,我得到2个错误:


  1. 我得到不能与在贴体的类型无效的值初始化类型'CCSprite'的变量> GetUserData行


  2. 我也得到:未声明的标识符助手的用途。我把那些2 helper方法到类,但我只是不知道什么是助手。


EDIT2 :这个怎么样?我决定保留方法我的类,而不是专门为它创建一个新的。

  //为每个机构,获得其分配BodyNode和更新精灵的位置
    对于(体=世界 - > GetBodyList(!);身体=零;身体=身体 - >的GetNext())
    {
        CCSprite *精灵=(CCSprite *) - 身体 - GT; GetUserData();
        如果(精灵!=无)
        {
            //这个身体的位置和旋转传输到精灵
            sprite.position = [自我toPixels:贴体>为getPosition()];
            飘角=身体 - > GetAngle();
            sprite.rotation = - (CC_RADIANS_TO_DEGREES(角));            //这会做完全相反的
            b2Vec2 POS = [自我toMeters:sprite.position];
            身体 - >的setTransform(POS,CC_DEGREES_TO_RADIANS(sprite.rotation));
            身体 - > SetLinearVelocity(b2Vec2(0.0,0.0));
            身体 - > SetAngularVelocity(0.0);
        }
    }
}//方便的方法,以一个CGPoint转换为b2Vec2
- (b2Vec2)toMeters:(CGPoint)点
{
    返回b2Vec2(point.x / CTM_RATIO,point.y / CTM_RATIO);
}//便捷方法一b2Vec2转换为CGPoint
- (CGPoint)toPixels:(b2Vec2)VEC
{
    返回ccpMult(CGPointMake(vec.x,vec.y),CTM_RATIO);
}


解决方案

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

所有你需要做的是确定何时以及精灵控制身体:

  //为每个机构,获得其分配BodyNode和更新精灵的位置
对于(b2Body *体=世界 - > GetBodyList(!);身体=零;身体=身体 - >的GetNext())
{
    BodyNode * bodyNode =身体 - > GetUserData();
    如果(bodyNode!=无)
    {
        //这个身体的位置和旋转传输到精灵
        bodyNode.position = [助手toPixels:贴体>为getPosition()];
        飘角=身体 - > GetAngle();
        bodyNode.rotation = - (CC_RADIANS_TO_DEGREES(角));        //这会做完全相反的
        b2Vec2 POS = [助手toMeters:bodyNode.position];
        身体 - >的setTransform(POS,CC_DEGREES_TO_RADIANS(bodyNode.rotation));
        身体 - > SetLinearVelocity(b2Vec2(0.0,0.0));
        身体 - > SetAngularVelocity(0.0);
    }
}

helper方法定义如下:

  //方便的方法,以一个CGPoint转换为b2Vec2
+(b2Vec2)toMeters:(CGPoint)点
{
    返回b2Vec2(point.x / PTM_RATIO,point.y / PTM_RATIO);
}//便捷方法一b2Vec2转换为CGPoint
+(CGPoint)toPixels:(b2Vec2)VEC
{
    返回ccpMult(CGPointMake(vec.x,vec.y),PTM_RATIO);
}

I am using Box2D with Cocos2D in my game. I am aware that Cocos2D has a animation API named CCMoveTo. I want to do the same thing in Box2D with b2Bodies. I want to be able to set a destination point and a animation time from the point it is initially at. Does anyone know if this is possible or not? I am just trying to keep this as simple as possible.

I am open to ideas and suggestions!

Thanks!

Edit1: Ok so I want to make my b2Bodys following my CCSprite permanently. There is a problem with the code you showed me.

In my .h I am doing this:

b2World *world; b2Body *body;

Then I modified the method a bit and now it looks like this:

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

So I used the ivars instead of the instance variable. Is that OK? Also I get 2 errors:

  1. I get Cannot Initialize variable of type 'CCSprite' with an value of type 'void' in the body->GetUserData line

  2. I also get: Use of undeclared identifier 'Helper'. I put those 2 helper methods into my class but I am just not sure what Helper is.

Edit2: How does this look? I decided to keep the methods in my class and not create a new one specifically for it.

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

解决方案

You can reverse the relationship of CCSprite and b2Body, either temporarily or permanently. Normally in your update method the CCSprite will be set to the position of the b2Body. If you have a CCSprite that uses CCMoveTo to move to a specific position, you could instead assign the b2Body the position and rotation of the sprite.

All you need to do is to determine when and which sprite to control the body:

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

The Helper methods are defined as follows:

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

这篇关于b2Body动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆