b2Body Animation?

若如初见. 提交于 2019-12-11 18:17:11

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/8074450/b2body-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!