[英]How to grab a b2Body and move it around the screen? (cocos2d,box2d,iphone)
[英]Fixing dynamic b2body on groundbody in 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);
繩索體:
對於(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)至米:(CGPoint)point {返回b2Vec2(point.x / PTM_RATIO,point.y / PTM_RATIO);
}
請幫忙 ..
// 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 。
現在我無法完全理解您想要做什么,但是我認為這可能會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.