1

ロープの物理学を使い始めました。どういうわけか、回転ジョイントを使用してロープを作成しました。1 つの円形の b2body に取り付けられた 1 つのロープを実装しました。ロープの一端を画面の上に固定したいのですが、ダイナミック ボディを使用しているため、画面の上に固定するのが困難です。本当に必要です。これを助けてください、私はここ数日からこれに固執しました。

これは私の動的な体です:

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)point { return b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO);

}

助けてください ..

4

1 に答える 1