Ios 人体动画?
我在游戏中使用了Box2D和Cocos2D。我知道Cocos2D有一个名为CCMoveTo的动画API。我想在Box2D中对B2Body做同样的事情。我希望能够设置一个目标点和一个动画时间,从它最初的位置开始。有人知道这是否可能吗? 我只是尽量让这件事尽可能简单 我愿意听取意见和建议 谢谢 Edit1:好的,我想让我的B2Body永远跟随我的CCSprite。 你给我看的代码有问题 在我的.h中,我正在这样做:Ios 人体动画?,ios,animation,cocos2d-iphone,box2d,Ios,Animation,Cocos2d Iphone,Box2d,我在游戏中使用了Box2D和Cocos2D。我知道Cocos2D有一个名为CCMoveTo的动画API。我想在Box2D中对B2Body做同样的事情。我希望能够设置一个目标点和一个动画时间,从它最初的位置开始。有人知道这是否可能吗? 我只是尽量让这件事尽可能简单 我愿意听取意见和建议 谢谢 Edit1:好的,我想让我的B2Body永远跟随我的CCSprite。 你给我看的代码有问题 在我的.h中,我正在这样做: b2World*世界; b2Body*body 然后我对方法做了一些修改,现在看起来
b2World*世界;
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而不是instance变量。可以吗?
我还发现了两个错误:
在body->GetUserData行中,我无法使用“void”类型的值初始化“CCSprite”类型的变量
我还得到:使用未声明的标识符“Helper”。我把这两个helper方法放到了我的类中,但我不确定helper是什么
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);
}
您可以临时或永久反转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);
}
}
助手方法的定义如下:
// 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);
}
谢谢,这是非常有用的信息,但我目前有一个问题。你能检查我的Edit1吗?对不起,缺少一个cast:CCSprite*sprite=(CCSprite*)body->GetUserData();您还必须使用body->SetUserData(sprite)将sprite存储为body的userdata;Helper是一个类,所以任何class@interface Helper都可以。谢谢你的帮助。Edit2是我的最终代码,它看起来怎么样?