开发者

Joint in Box2d with cocos2d

I'm new in box2d and I tried to create joint between two body. I wrote a joint like

b2Re开发者_如何学JAVAvoluteJointDef jointDef;

        jointDef.bodyA=worm_head;
        jointDef.bodyB=worm_body;

        jointDef.lowerAngle = -0.25f * b2_pi; // -45 degrees

        jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees
        jointDef.enableLimit=true;
        jointDef.maxMotorTorque = 10.0f;

        jointDef.motorSpeed = 10.0f;

        jointDef.enableMotor = true;
        joint=(b2DistanceJoint*)_world->CreateJoint(&jointDef);

but body is not moving when head is moving.

my tick method is

- (void)tick:(ccTime) dt {

    //we update the position of the b2body based on the sprite position
    for (b2Body* body = _world->GetBodyList(); body != nil; body = body->GetNext())
    {
        if (body->GetUserData() != NULL) {
            CCSprite *spritedata = (CCSprite *)body->GetUserData();

            if(spritedata.tag==1)
            {
                b2Vec2 b2Position = b2Vec2(SCREEN_TO_WORLD(spritedata.position.x),
                                   SCREEN_TO_WORLD(spritedata.position.y));
                float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(spritedata.rotation);
                body->SetTransform(b2Position,b2Angle);
            }
            else {
                spritedata.position = ccp(body->GetPosition().x * PTM_RATIO,
                                       body->GetPosition().y * PTM_RATIO);
                spritedata.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle());
            }

        }

    }
}

Why is not moving ? How should I change my code ?


In b2RevoluteJointDef , one body is static body and another is dynamic body. My problem is using two dynamic problem. Now, it solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜