b2Bodyanimation?

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

我很乐意提供意见和build议!

谢谢!

编辑1 :好,所以我想让我的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而不是实例variables。 这可以吗? 另外我得到2个错误:

  1. 我不能初始化types'CCSprite'的variables在主体 – > GetUserData行中的types为'void'的值

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

编辑2 :这看起来如何? 我决定在课堂上保留这些方法,而不是专门为它创build一个新的方法。

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

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

所有你需要做的是确定何时和哪个精灵来控制身体:

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