简体   繁体   中英

Fixing dynamic b2body on groundbody in cocos2d iphone

I have started working with rope physics.Somehow i have created ropes using Revolute joints . I have implemented one rope attached with one circle shaped b2body ,now i want to fix my one end of rope on top of the screen but i have used dynamic body so i am having difficulties to fix it on top of the screen.I really need help on this , i stuck with this from last couple of days .

This is my dynamic body :

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;

joints :

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

Ropes bodies :

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
}

finally i joint my circle shaped dynamic body this way :

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 called here

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

}

please help ..

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

This code has been taken from raywenderlich's website where he tell how to make a box2d breakout game.here is the link http://www.raywenderlich.com/28604/how-to-create-a-breakout-game-with-box2d-and-cocos2d-2-x-tutorial-part-1 .

Now i wan't fully able to understand what you wanted to do but i think this might help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM