0

我已经创建了两个b2body,我想围绕一个特定的点移动一个身体。为此,我创建了两个主体并b2revoluteJoint在这些主体之间创建了一个。但它不会产生预期的效果。

下面是我的代码:

-(id) init
{
    if( (self=[super init])) {

        self.touchEnabled = YES;

        CGSize winSize = [[CCDirector sharedDirector] winSize];

        b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
        bool allowBodiesToSleep = true;
        world = new b2World(gravity);
        world->SetAllowSleeping(allowBodiesToSleep);
        world->SetContinuousPhysics(true);

        _debugDraw = new GLESDebugDraw( PTM_RATIO );
        world->SetDebugDraw(_debugDraw);

        uint32 flags = 0;
        flags += b2Draw::e_shapeBit;
        flags += b2Draw::e_jointBit;
        flags += b2Draw::e_aabbBit;
        flags += b2Draw::e_pairBit;
        flags += b2Draw::e_centerOfMassBit;
        _debugDraw->SetFlags(flags);

        // Arm
        arm = [CCSprite spriteWithFile:@"arm.png"];
        [self addChild:arm];
        [arm setPosition:ccp(winSize.width/2, winSize.height/2)];

        b2BodyDef spriteBodyDef;
        spriteBodyDef.linearDamping = 1;
        spriteBodyDef.angularDamping = 1;
        spriteBodyDef.type = b2_dynamicBody;
        spriteBodyDef.position.Set(arm.position.x/PTM_RATIO, arm.position.y+1/PTM_RATIO);
        spriteBodyDef.userData = (__bridge void *)arm;
        spriteBody = world->CreateBody(&spriteBodyDef);
        b2PolygonShape spriteShape;
        spriteShape.SetAsBox(arm.contentSize.width/PTM_RATIO/2, arm.contentSize.height/PTM_RATIO/2);
        b2FixtureDef spriteShapeDef;
        spriteShapeDef.shape = &spriteShape;
        spriteBody->CreateFixture(&spriteShapeDef);

        monster = [CCSprite spriteWithFile:@"body.png"];
        [monster setPosition:ccp(winSize.width*0.37, winSize.height/2)];
        [self addChild:monster];

        b2BodyDef monsterBodyDef;
        monsterBodyDef.position.Set(monster.position.x/PTM_RATIO, monster.position.y/PTM_RATIO);
        monsterBodyDef.userData = (__bridge void *)monster;
        monsterBody = world->CreateBody(&monsterBodyDef);
        b2PolygonShape monsterShape;
        monsterShape.SetAsBox(monster.contentSize.width/PTM_RATIO/2, monster.contentSize.height/PTM_RATIO/2);
        b2FixtureDef monsterShapeDef;
        monsterShapeDef.shape = &monsterShape;
        monsterBody->CreateFixture(&monsterShapeDef);

        b2RevoluteJointDef armJointDef;
        armJointDef.Initialize(monsterBody, spriteBody, b2Vec2(monster.position.x/PTM_RATIO,monster.position.y/PTM_RATIO));
        armJointDef.enableMotor = false;
        armJointDef.enableLimit = true;
        armJointDef.localAnchorA = b2Vec2(0, 0);
        armJointDef.localAnchorB = b2Vec2(1, 0);
        armJointDef.motorSpeed  = -10;
        armJointDef.lowerAngle  = CC_DEGREES_TO_RADIANS(9);
        armJointDef.upperAngle  = CC_DEGREES_TO_RADIANS(75);
        armJointDef.maxMotorTorque = 100;

        armJoint = (b2RevoluteJoint*)world->CreateJoint(&armJointDef);

        b2BodyDef bodyDef;
        bodyDef.type = b2_dynamicBody;
        bodyDef.position.Set(monster.position.x/PTM_RATIO, monster.position.y/PTM_RATIO);
        //or make it static bodyDef.type = b2_staticBody;
        b2Body * body = world->CreateBody(&bodyDef);
        //create circle shape
        b2CircleShape  circle;
        circle.m_radius = 20.0/PTM_RATIO;
        //define fixture
        b2FixtureDef fixtureDef;
        fixtureDef.shape = &circle;
        fixtureDef.density = 1;
        fixtureDef.restitution = 0.7;
        fixtureDef.friction = 0.4;
        body->CreateFixture(&fixtureDef);

    }
    return self;
}

和触摸功能来移动身体:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (mouseJoint != nil) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);

    monster.visible = NO;

    if (locationWorld.x < spriteBody->GetWorldCenter().x + 50.0/PTM_RATIO)
    {
        b2MouseJointDef md;
        md.bodyA = monsterBody;
        md.bodyB = spriteBody;
        md.target = locationWorld;
        md.maxForce = 2;

        mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
    }
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
    spriteBody->SetTransform(locationWorld, angle);

}

我是 Box2d 的新手,所以如果有人做过这样的事情。请解释一下。谢谢 :)

4

1 回答 1

0

不知道你在这里尝试什么,但它似乎适用于box2d Joint。您可以尝试添加 aDistanceJoint以将动态主体附加到将成为中心的静态主体。

于 2013-11-01T11:36:30.267 回答