0
我跟着本教程如何將Box2D添加到我現有的ViewBased應用程序。 http://www.cocoanetics.com/2010/05/physics-101-uikit-app-with-box2d-for-gravity/Box2D與UIKit - 身體物理
現在當我的身體/ 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;
你能告訴我們一些關於如何設置對象和重力的代碼嗎?你也可以看看我的代碼:https://github.com/nhahtdh/PS5/blob/master/Game/GameViewController.mm – nhahtdh
@nhahtdh剛剛添加了我的身體創建和世界創作 – ManOx
什麼是'體'在代碼來建立世界?我認爲你應該將groundBodyDef設置爲靜態身體類型。我不認爲我可以在這裏調試你的代碼,因爲有點難以想象。但是您可以嘗試在每個時間步驟中打印出對象的座標並找出實際發生的情況。 – nhahtdh