0

Box2Dを既存のViewBasedアプリケーションに追加する方法について、このチュートリアルに従いました。 http://www.cocoanetics.com/2010/05/physics-101-uikit-app-with-box2d-for-gravity/

これで、body / UIButtonが画面の半分より上にくると、上向きに飛んでいきます。中途半端になると下向きに飛んでいきます。また、物理学はあまり現実的ではないようです。チュートリアルで構築したのと同じ方法で(Xcode 4.0と互換性を持たせるためにいくつかの変更を加えて)自分の世界を構築しました。タイマーとボディの作成についても同じことが言えます。誰が何が悪いのか知っていますか?(詳細が必要な場合はお知らせください)

ボディ作成のコードは次のとおりです。

// Define the dynamic body.

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;

CGPoint p = physicalView.center;
CGPoint boxDimensions = CGPointMake(physicalView.bounds.size.width/PTM_RATIO/2.0,physicalView.bounds.size.height/PTM_RATIO/2.0);

bodyDef.position.Set(p.x/PTM_RATIO, (self.stage.frame.size.height - p.y)/PTM_RATIO);
bodyDef.userData = (__bridge void *)physicalView;

// Tell the physics world to create the body
b2Body *body = world->CreateBody(&bodyDef);

// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;

dynamicBox.SetAsBox(boxDimensions.x, boxDimensions.y);

// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 3.0f; // 0 is a ball, and 1 is a rock
fixtureDef.friction = 0.3f; // 0 is a lubercated ball, 1 is rough as sand paper
fixtureDef.restitution = 0.5f; // 0 is a lead ball, 1 is a super bouncy ball
body->CreateFixture(&fixtureDef);

これが世界を設定するための私のコードです:

CGSize screenSize = self.stage.bounds.size;

screenSize.width = 368;
// Define the gravity vector.
b2Vec2 gravity;
gravity.Set(0.0f, -9.81f);

// Do we want to let bodies sleep?
// This will speed up the physics simulation
bool doSleep = false;

// Construct a world object, which will hold and simulate the rigid bodies.
world = new b2World(gravity);
world->SetAllowSleeping(doSleep);

world->SetContinuousPhysics(true);

// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner

// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world->CreateBody(&groundBodyDef);

// Define the ground box shape.
b2EdgeShape groundBox;

// bottom
groundBox.Set(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox, 0);

// top
groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);

// left
groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
groundBody->CreateFixture(&groundBox, 0);

// right
groundBox.Set(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox, 0);

// a dynamic body reacts to forces right away
body->SetType(b2_dynamicBody);

// we abuse the tag property as pointer to the physical body
physicalView.tag = (int)body;
4

1 に答える 1

0

問題は、プログラムでビューにポップし、他のオブジェクトと衝突し始めた目に見えない UIView があったことです。

于 2012-06-05T17:30:27.297 に答える