在cocos2d iphone中修复动态b2体在地体上

时间:2013-06-28 13:04:53

标签: iphone cocos2d-iphone

我已经开始使用绳索物理学。我已经使用Revolute关节创建了绳索。我已经实现了一根附有一个圆形b2body的绳子,现在我想将我的一端绳子固定在屏幕上方,但我使用了动态的身体,所以我很难将它固定在屏幕上。我真的需要对此有所帮助,我坚持了这几天。

这是我的动态身体:

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
b2Vec2 startPos = [self toMeters:ccp(s.width/2 ,s.height)];
bodyDef.position = startPos;
b2FixtureDef fixtureDef;
fixtureDef.density = 0.1;
b2PolygonShape polygonShape;
polygonShape.SetAsBox(linkWidth,linkHeight);
fixtureDef.shape = &polygonShape;

关节:

 b2Body* mylink = world->CreateBody( &bodyDef1 );
 mylink->CreateFixture( &fixtureDef1 );

 b2RevoluteJointDef revoluteJointDef;
 revoluteJointDef.bodyA = mylink;        
 revoluteJointDef.bodyB = link;

revoluteJointDef.localAnchorA.Set( 0,  linkHeight);
revoluteJointDef.localAnchorB.Set( 0, -linkHeight);

绳索体:

for(int i = 0; i< 10; i ++){

    b2Body* newLink = world->CreateBody( &bodyDef );
    newLink->CreateFixture( &fixtureDef );
    PhysicsSprite* segmentSprite = [PhysicsSprite spriteWithFile:@"rope_seg_new2.png"];
    [self addChild:segmentSprite];
    [segmentSprite setPhysicsBody:link];

    revoluteJointDef.bodyA = link;
    revoluteJointDef.bodyB = newLink;
    world->CreateJoint( &revoluteJointDef );

    link = newLink;  //prepare for next iteration
}

最后,我以这种方式联合我的圆形动态身体:

PhysicsSprite* circleBodySprite = [PhysicsSprite spriteWithFile:@"medal1.png"];

[self addChild:circleBodySprite z:1];

b2CircleShape circleShape;
circleShape.m_radius = circleBodySprite.contentSize.width/2 / PTM_RATIO;
fixtureDef.shape  = &circleShape;
fixtureDef.density =0.5;   
b2Body* chainBase =world->CreateBody( &bodyDef );
chainBase->CreateFixture( &fixtureDef );
[circleBodySprite setPhysicsBody:chainBase];
balloon = chainBase;


//another revolute joint to connect the chain(of ropes ) to the circle 

revoluteJointDef.bodyA = link;        //the last added link of the chain
revoluteJointDef.bodyB = chainBase;

//the regular position for chain link joints, as above

revoluteJointDef.localAnchorA.Set(0,linkWidth);

//a little in from the edge of the circle

revoluteJointDef.localAnchorB.Set(0,linkWidth);

world->CreateJoint( &revoluteJointDef );

// toMeters在这里调用

- (b2Vec2)toMeters:(CGPoint)点 {     返回b2Vec2(point.x / PTM_RATIO,point.y / PTM_RATIO);

}

请帮助..

1 个答案:

答案 0 :(得分:0)

// Restrict paddle along the x axis
b2PrismaticJointDef jointDef;
b2Vec2 worldAxis(1.0f, 0.0f);
jointDef.collideConnected = true;
jointDef.Initialize(_paddleBody, _groundBody, 
_paddleBody->GetWorldCenter(), worldAxis);
_world->CreateJoint(&jointDef);

此代码取自raywenderlich的网站,他告诉我们如何制作一个box2d突破游戏。这里有链接  http://www.raywenderlich.com/28604/how-to-create-a-breakout-game-with-box2d-and-cocos2d-2-x-tutorial-part-1

现在我不能完全理解你想做什么,但我认为这可能会有所帮助。