0

I followed this tutorial on how to add Box2D to my existing ViewBased Application. http://www.cocoanetics.com/2010/05/physics-101-uikit-app-with-box2d-for-gravity/

Now when my body/UIButton is above half way my screen, it flies upward. and when it's half way downward, it flies downward. Also the physics are not seeming to be very realistic. I built my world the same way it is (with a couple of modifications to make it Xcode 4.0 compatible) built in the tutorial. Same goes for the timer, and the body creation. Anyone know what's wrong? (let me know if you need more detail)

Here is my code for body creation:

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

Here is my code for setting up the world:

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;
ManOx
  • 1,935
  • 5
  • 23
  • 37
  • Can you show us some code on how you set up the object and gravity? You can also take a look at my code: https://github.com/nhahtdh/PS5/blob/master/Game/GameViewController.mm – nhahtdh Jun 03 '12 at 04:54
  • @nhahtdh Just added my Body creation and World creation – ManOx Jun 03 '12 at 04:57
  • 1
    What is `body` in the code to set up the world? And I think you should set the groundBodyDef to static body type. I don't think I can debug your code here since it is a bit hard to imagine. but you can try print out the coordinate of the objects in each time step and find out what actually happens. – nhahtdh Jun 03 '12 at 05:34
  • @nhahtdh for every object that is on my screen, it gets turned into a body so that it can be added to the world. – ManOx Jun 03 '12 at 06:51
  • what's the value of PTM_RATIO? – Basel Jun 03 '12 at 07:10
  • It might be that you are drawing the world in the upper/bottom half of the screen. Please check this: http://www.cocos2d-iphone.org/forum/topic/7739 – Basel Jun 03 '12 at 10:50

1 Answers1

0

The problem was I had an invisible UIView that programically pops into the view and started colliding with the other objects.

ManOx
  • 1,935
  • 5
  • 23
  • 37